OSU CSE 2421
Required Reading:
Pointers on C, Chapter 4 (skim is likely good enough), Section 5.1.9
J.E.Jones
OSU CSE 2421
Loop constructs in C for loops
while loops
do while loops
break and continue
Conditional statements in C if
switch-case
Boolean issues
The comma operator
Enumerated data types
J. E. Jones
OSU CSE 2421
for (expression1; expression2; expression3) { statement(s); }
while (expression) {statement(s); }
do { statement(s);
} while (expression);
Statement(s) only execute when expression (expression2 in for loop) is non-zero (TRUE).
Notice the semi-colon locations.
do while {statement(s)} always execute at least once (not necessarily true for the other two constructs).
J. E. Jones
OSU CSE 2421
for (expression1; expression2; expression3) { statement(s); }
expression1 is the initialization and is evaluated only once before expression2 is evaluated the first time.
expression2 is the condition and is evaluated once before each execution of the body ({statement(s)}); if it evaluates to a non-zero value, the body is executed; otherwise, execution of the for-loop terminates, and execution continues with the statement following the for-loop.
expression3 is called the adjustment and is evaluated after all statements in the body are executed, and just before the condition is evaluated again.
J. E. Jones
OSU CSE 2421
#include
int main () {
}
int n, sum = 0; /*defines n and sum; initializes sum*/
/* n=n+1 could be n++ or n+=1 or ++n */
for (n = 1; n <= 10; n = n + 1) {sum = sum + n;}printf(“The sum of integers from 1 to 10 is %d
“, sum); return 0;J. E. Jones OSU CSE 2421 while (expression) {statement(s) } expression is evaluated; if it evaluates to non-zero (TRUE), {statement(s)} is/are executed, and expression is evaluated again… If expression evaluates to zero (FALSE), execution of the while loop terminates, and the statement following the loop is executed (the next statement after the }).J. E. Jones OSU CSE 2421 #include
int n = 1, sum = 0; while (n <= 10) {}sum=sum+n; n = n + 1;/*couldusesum+=ninstead */ /* could use ++n instead */printf(”
The sum of integers from 1 to 10 is %d
“, sum);return 0; }J. E. Jones OSU CSE 2421 #include
{ char ch;
printf(Please enter characters one at a time, followed by enter. When you want to stop, enter the # character followed by enter:
);
while ((ch = (char)getchar()) != #) {
putchar(ch); }
return(0); }
/*What does this simple program do? */ /*What doesnt it do? EOF??? */
J. E. Jones
OSU CSE 2421
do { statement(s);
} while (expression);
statement(s) are executed once, and then expression is evaluated; if it evaluates to non-zero (TRUE) {statement(s)} are executed again, until expression evaluates to zero (FALSE).
The result is that {statement(s)} will always be executed at least once, even if expression is false before the first execution.
J. E. Jones
OSU CSE 2421
#include
{ int print_it = 0;
do {
printf(Hello World!
);
} while (print_it != 0);
return(0); }
/* Hello World! is printed once, even though print_it is zero before the loop body is executed. */
J. E. Jones
OSU CSE 2421
If a loop statement is only going to contain one line you can leave off the enclosing {}
while(x < 10) printf(%d, x++);for(int i = 0; i < 10; ++i) printf(%d, i);Nonetheless, best practice is to use {} for clarity (or in case you need to add additional statements to the body of the loop later), but this shorthand is acceptable.J. E. Jones OSU CSE 2421 Keywords that can be very important to looping are break and continue. break is valid in a loop or a switch construct continue is only valid in a loop construct although you can have a loop construct inside a switch construct break exits the innermost loop or switch statement (seebelow) which encloses it regardless of loop conditions continue causes the loop to stop its current iteration (do the adjustment in the case of for loops) and begin to execute again from the top.J. E. Jones OSU CSE 2421 /* playing checkers */while (1) { take_turn(player1);} }if((has_won(player1) || (wants_to_quit(player1) == TRUE)){ break;}take_turn(player2);if((has_won(player2) || (wants_to_quit(player2) == TRUE)){break;J. E. Jones OSU CSE 2421 /* playing monopoly */for (player = 1; has_won() == 0; player++) { if (player > total_number_of_players) {
player = 1; }
if (is_bankrupt(player)) { continue;
}
take_turn(player); }
J. E. Jones
OSU CSE 2421
goto plus a labeled statement
goto identifier; /* identifier is called a label here */ identifier:statement;
Dont have to pre-define or declare the identifier
Identifiers must be unique
A statement label is meaningful only to a goto statement, otherwise its ignored
Both the keyword goto, and the label, must appear in a single function (i.e., you cant jump to a statement in a different function)
Identifier labels have their own name space so the names do not interfere with other identifiers (however, for clarity, identifiers that are the same as variable or function names should not be used!).
Best practice to use break, continue, and return statement in preference to goto whenever possible (i.e., virtually always).
Specialcasewheregotoisconsideredacceptable:exitingmultiplelevelsof loops (a deeply nested loop).
J. E. Jones
OSU CSE 2421
#include
int i, j;
for ( i = 0; i < 10; i++ ) {} }printf(“Outer loop. i = %d
“,i); for ( j = 0; j < 3; j++ ) {printf(“Inner loop. j = %d
“,j); if ( i == 1 ) goto stop;/* This message does not print: */printf(“Loop exited. i = %d
“, i );stop:printf( “Jumped to stop. i = %d
“, i );return (0); }J. E. Jones OSU CSE 2421 #include
Output:
Outer loop. i = 0 Inner loop. j = 0 Inner loop. j = 1 Inner loop, j = 2 Outer loop. i = 1 Inner loop, j = 0 Jumped to stop. i = 1
int i, j;
for ( i = 0; i < 10; i++ ) {stop:printf(“Outer loop. i = %d
“,i); for ( j = 0; j < 3; j++ ) {printf(“Inner loop. j = %d
“,j);if ( i == 1 ) goto stop; }}/* This message does not print: */ printf(“Loop exited. i = %d
“, i );printf( “Jumped to stop. i = %d
“, i );return (0); }J. E. Jones OSU CSE 2421 The switch statement is similar to a chain of if/else statements, but typically it executes significantly faster. General form is: switch(expression) { case constant1: The statement evaluates the expression (usually a variable) and then looks for a matching case label and jumps there; case labels must be unique, constant values. If not unique, it picks the first one. If no match is found, the default label is used.statement; ….. break; A break statement inside a switchsays, continue execution after theswitch. If its not there, executiongoes to the next statement (whichcould be the 1st statement in the next } case). This is called fall through.default:statement;case constant2: statement;…../* fall through */ case constant3:statement; ….. break;….. break;J. E. Jones OSU CSE 2421 Syntax for if and else: Syntax for switch: switch (operator) {if (operator = = ‘+’) { result + = value;case ‘+’:result + = value; break;}else if (operator = = ‘-‘) { result-= value;case ‘-‘:result-= value; break;}case ‘*’:result *= value; break;else if (operator = = ‘*’) { result *= value;}case ‘/’:if (value = = 0) {else if (operator = = ‘/’) { if (value = = 0) {printf(” Error:Divide by zero n”);}printf(” Unknown operator %c n”, operator);printf(” Error:Divide by zero n”);printf(” operation ignored n”); }printf(” operation ignored n”); }else result /= value;else result /= value;default:else {printf(” Unknown operator %c n”, operator);break;}}break;J. E. Jones OSU CSE 2421if ( expression ) {Code to execute if
else {
Code to execute if
/* Execute these stmts if expression != 0 */ }
break; case value2:
/* Execute these stmts if expression == 0 */ }
break;
if (expression1) { statement(s); }
break; }
else if (expression2) { statement(s); }
SWITCH NOTES:
Notice, no {} blocks within each case!
Notice the colon for each case and value.
value1, value2, etc. in a switch statement must be constant
else { statement(s); }
switch ( expression ) {
case value1: /* Note use : not ; */
default:
Code to execute if
values; expression should evaluate to some integer type
The default case is optional, but it is wise to include it as it
handles any unexpected cases.
Fall-through is allowed (if break is omitted)
Chooses first value that matches
J. E. Jones
OSU CSE 2421
/* Suppose this code, with scores from 0 to 100 */
int score = 80;
if (score >= 60)
if (score >= 90)
printf(Congratulations, you got an A!
); else printf(Unfortunately, thats not passing!
);
Output?
J. E. Jones
OSU CSE 2421
Output:
Unfortunately, thats not passing!
What happened? We only wanted this message for scores < 60.J. E. Jones OSU CSE 2421 int score = 80; if (score >= 60)
if (score >= 90)
printf(Congratulations, you got an A!
);
else printf(Unfortunately, thats not passing!
);
The problem is that the compiler does not pay attention to formatting, so even though the else is aligned (format-wise) with the first if, the compiler pairs it with the second if.
Rule: The compiler pairs any else with the closest (most recent) unmatched if which is not enclosed in a different block.
Unmatched here means that the if has not already been paired with an else.
J. E. Jones
OSU CSE 2421
int score = 80;
if (score >= 60) {
if (score >= 90)
printf(Congratulations, you got an A!
);
}
else printf(Unfortunately, thats not passing!
);
Now, the else will be paired with the first if, because the second one is enclosed in a different block.
What prints when score = 80 with the code above?
J. E. Jones
OSU CSE 2421
#include
}
int age;
printf( Please enter your age ); scanf( %d, &age );
if ( age < 100 ) {printf (“You are young!
” );}else if ( age == 100 ) {}else {} return 0;printf(“You are old.
” );printf(“You are really old!
” );J. E. Jones OSU CSE 2421 switch ( x ) { case ‘a’:case ‘c’: case ‘d’:/* Do stuff when x == ‘a’ */break; case ‘b:break; default:}/* code we use if x==bbut not c or d, thenfall through *//* Fall-through technique…cases b,c,d all use this code *//* Handle cases when x is not a, b, c or d. ALWAYS have a default case even though its notrequired */break; /* this break is not necessary, but is legaland creates completeness */J. E. Jones OSU CSE 2421 Every boolean test is an implicit comparison against zero (0). However, zero is not a simple concept. It represents: 1. the integer 0 for all integer types2. the floating point 0.0 (positive or negative)3. the null character ( )4. the null pointer (NULL) — #define NULL 0 In order to make your intentions clear, explicitly show the comparison with zero for all scalars, floating-point numbers, and characters.J. E. Jones OSU CSE 2421 int i; if (i) is better represented as if (i != 0) float x; if (!x) is better represented as if (x == 0.0) char c; if (c) is better represented as if (c != ‘