-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter4-Q35.c
35 lines (30 loc) · 1.35 KB
/
Chapter4-Q35.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
Author is : Ibrahim Halil GEZER
4.35 A criticism of the break statement and the continue statement is that each is unstructured.
Actually, break statements and continue statements can always be replaced by structured statements,
although doing so can be awkward. Describe in general how you would remove any break
statement from a loop in a program and replace that statement with some structured equivalent.
[Hint: The break statement leaves a loop from within the body of the loop. The other way to leave
is by failing the loop-continuation test. Consider using in the loop-continuation test a second test
that indicates “early exit because of a ‘break’ condition.”] Use the technique you developed here to
remove the break statement from the program of Fig. 4.11.
,
*/
/* Fig. 4.11: fig04_11.c
2 Using the break statement in a for statement */
#include <stdio.h>
/* function main begins program execution */
int main( void )
{
int x; /* counter */
/* loop 10 times */
for ( x = 1; x <= 10; x++ ) {
/* if x is 5, terminate loop */
if ( x == 5 ) {
return 0 ; // I change break statement to return statement !!
} /* end if */
printf( "%d ", x ); /* display value of x */
} /*end for */
printf( "\nBroke out of loop at x == %d\n", x );
return 0; /* indicate program ended successfully */
} /* end function main */