PowerPoint Presentation
CSE 2421
The C Language – Part 4, Control Structures
Required Reading:
Pointers on C, Chapter 4 (skim is likely good enough), Section 5.1.9
1
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
Overview
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).
Loop Constructs
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.
for loop
#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\n", sum);
return 0;
}
for loop example
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 }).
while loop
#include
int main () {
int n = 1, sum = 0;
while (n <= 10) {
sum = sum + n;
n = n + 1;
}
printf("\n The sum of integers from 1 to 10 is %d\n", sum);
return 0;
}
while loop example
#include
int main()
{ char ch;
printf(“Please enter characters one at a time, followed by enter. When you want to stop, enter the # character followed by enter:\n”);
while ((ch = (char)getchar()) != ‘#’)
{
putchar(ch);
}
return 0;
}
/*What does this simple program do? */
while loop example with an embedded assignment expression
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.
do while
#include
int main()
{ int print_it = 0;
do {
printf(“Hello World!\n”);
} while (print_it != 0);
return 0;
}
/* Hello World! is printed once, even though print_it is zero before the loop body is executed. */
do while loop example
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.
1 line loop bodies
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 (see below) 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.
Break, Continue
goto plus a labeled statement
goto identifier; /* identifier is called a label here */
identifier: statement;
Don’t have to pre-define or declare the identifier
Identifiers must be unique
A statement label is meaningful only to a goto statement, otherwise it’s ignored
Both the keyword goto, and the label, must appear in a single function (i.e., you can’t 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).
Special case where goto is considered acceptable: exiting multiple levels of loops (a deeply nested loop).
Jump Statement
13
/* 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;
}
}
Break Example
#include
int main()
{ int i = 0;
for (i; i <= 10; i++){
if (i % 2 == 1) { /* don't print it */
continue;
} else
printf("%d\n", i);
}
return 0;
}
Continue Example
#include
int main() {
int i, j;
for ( i = 0; i < 10; i++ )
{
printf("Outer loop. i = %d\n",i);
for ( j = 0; j < 3; j++ )
{
printf("Inner loop. j = %d\n",j);
if ( i == 1 )
goto stop;
}
}
/* This message does not print: */
printf("Loop exited. i = %d\n", i );
stop:
printf( "Jumped to stop. i = %d\n", i );
return 0; }
Output:
?????
goto Example
#include
int main() {
int i, j;
for ( i = 0; i < 10; i++ )
{
printf("Outer loop. i =%d\n",i);
for ( j = 0; j < 3; j++ )
{
printf("Inner loop. j = %d\n",j);
if ( i == 1 )
goto stop;
}
}
/* This message does not print: */
printf("Loop exited. i = %d\n", i );
stop:
printf( "Jumped to stop. i = %d\n", i );
return 0; }
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
Goto Example
The switch statement is similar to a chain of if/else statements, but typically it executes significantly faster.
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.
A break statement inside a switch says “continue execution after the switch”. If it’s not there, execution goes to the next statement (which could be the 1st statement in the next case). This is called “fall through”.
Switch Statement
General form is:
switch(expression) {
case constant1:
statement;
. . . . .
break;
case constant2:
statement;
. . . . .
/* fall through */
case constant3:
statement;
. . . . .
break;
default:
statement;
. . . . .
break;
}
Syntax for if and else:
if (operator = = '+') {
result + = value;
}
else if (operator = = '-') {
result-= value;
}
else if (operator = = '*') {
result *= value;
}
else if (operator = = '/') {
if (value = = 0) {
printf(" Error:Divide by zero\ n");
printf(" operation ignored\ n");
}
else result /= value;
}
else {
printf(" Unknown operator %c\ n", operator);
}
Switch Statement
Syntax for switch:
switch (operator) {
case '+':
result + = value;
break;
case '-':
result-= value;
break;
case '*':
result *= value;
break;
case '/':
if (value = = 0) {
printf(" Error:Divide by zero\ n");
printf(" operation ignored\ n");
}
else result /= value;
break;
default:
printf(" Unknown operator %c\ n", operator);
break;
}
19
If, else-if, switch-case conditional statements
if (expression1) {
statement(s); }
else if (expression2) {
statement(s); }
else {
statement(s); }
if ( expression ) {
/* Execute these stmts if
expression != 0 */ }
else {
/* Execute these stmts if
expression == 0 */ }
switch ( expression ) {
case value1: /* Note - use : not ; */
Code to execute if
break;
case value2:
Code to execute if
break;
…
default:
Code to execute if
cases…
break;
}
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 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…
20
/* Suppose this code, with scores from 0 to 100 */
int score = 80;
if (score >= 60)
if (score >= 90)
printf(“Congratulations, you got an A!\n”);
else printf(“Unfortunately, that’s not passing!\n”);
Output?
Dangling else
Output:
Unfortunately, that’s not passing!
What happened? We only wanted this message for scores < 60.
Dangling else - output
int score = 80;
if (score >= 60)
if (score >= 90)
printf(“Congratulations, you got an A!\n”);
else printf(“Unfortunately, that’s not passing!\n”);
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.
Dangling else – output
int score = 80;
if (score >= 60) {
if (score >= 90)
printf(“Congratulations, you got an A!\n”);
}
else printf(“Unfortunately, that’s not passing!\n”);
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?
Dangling else – how to fix it
#include
int main() {
int age;
printf( “Please enter your age” );
scanf( “%d”, &age );
if ( age < 100 ) {
printf ("You are young!\n" );
}
else if ( age == 100 ) {
printf("You are old.\n" );
}
else {
printf("You are really old!\n" );
}
return 0;
}
else-if Example
switch ( x ) {
case 'a':
/* Do stuff when x == 'a' */
break;
case 'b':
case 'c':
case 'd':
/* Fall-through technique...
cases ‘b’,’c’,’d’ all use this code */
break;
default:
/* Handle cases when x is not ‘a’, ’b’, ’c’ or ‘d’.
ALWAYS have a default case even though it’s not
required */
break; /* this break is not necessary, but is legal
and creates “completeness” */
}
Switch Case
Every boolean test is an implicit comparison against zero (0).
However, zero is not a simple concept. It represents:
the integer 0 for all integer types
the floating point 0.0 (positive or negative)
the null character (‘\0’)
the null pointer
In order to make your intentions clear, explicitly show the comparison with zero for all scalars, floating-point numbers, and characters.
Boolean Issues
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 != '\0')
An exception is made for pointers, since 0 is the only language-level representation for the NULL pointer.
The symbol NULL is not part of the core language - you have to include a special header file to get it defined (stdlib.h or stddef.h). More on this later.
Boolean Issues
To write an INFINITE LOOP
for (;;) ...
while (1) ...
The former is more visually distinctive, but both forms are used.
Boolean issues
Used to link related expressions together
Evaluated from left to right
The value of the right-most expression is the value of the whole combined expression
Parentheses are necessary; if no parentheses surround an expression with commas, the commas are separators, and not the comma operator!
Example:
i = a, b, c; /*stores a into i – commas here are separators*/
i = (a, b, c); /*stores c into i – these commas are comma operators*/
For loop:
for (n=1, m=10; n<=m; n++, m--)
While:
while (c=getchar(), c!= ‘0’)
Exchanging values:
(t=x, x=y, y=t); /*Better not to use comma operator unless you want to use this as a boolean, for example, to control a loop or an if or if-else conditional*/
The Comma Operator
CSE 2421
The C Language – Enumerated Data Types
An enumerated data type is designed for variables that contain only a limited set of values.
These values are referenced by name(tag).
The compiler assigns each tag an integer value internally.
An enumerated type is one whose values are symbolic constants rather than literals.
Declaration example:
enum Container_Type {CUP, PINT, QUART, HALF_GALLON, GALLON};
Declaration of a variable of the above type:
enum Container_Type milk_bottle;
Enumerated Data Types
32
Variables declared with an enumerated type are actually stored as integers.
Internally, the symbolic names are treated as integer constants, and it is legal to assign them values, e.g.:
enum Container_Type {CUP=8, PINT=16, QUART=32, HALF_GALLON=64, GALLON=128};
Otherwise, by default, CUP =0, PINT=1, QUART=2, etc.
Caution: don’t mix them indiscriminately with integers – even though it is syntactically valid to do so.
milk_bottle = -623; /*A bad idea, and likely to lead to trouble*/
int a = PINT; /*Also a bad idea, and likely to lead to trouble*/
Enumerated Data Type (cont)
If there is only one declaration of variables of a particular enumerated type (i.e. no type name), both statements may be combined:
enum {CUP, PINT, QUART, HALF_GALLON, GALLON} milk_bottle, gas_can, medicine_bottle;
milk_bottle, gas_can, and medicine_bottle are now all instances of the enum type, and all these variables can be assigned CUP, PINT, etc.
No more variables of this type can be declared later because no type name was given to it
Nor can pointers to this variable type be declared
Enumerated Data Types
#include
main() {
enum month {jan = 1, feb=2, mar=3, apr=4, may=5, jun = 6, jul=7, aug=8, sep=9, oct=10, nov = 11,dec=12 } this_month;
this_month = feb;
printf(“month : %d\n”,this_month);
}
Output??
Enumerated Types Example
#include
main() {
enum month {jan = 1, feb=2, mar=3, apr=4, may=5, jun = 6, jul=7, aug=8, sep=9, oct=10, nov = 11,dec=12 } this_month;
this_month = feb;
printf(“month : %d\n”,this_month);
}
Output??
Output
month: 2
Enumerated Types Example
/docProps/thumbnail.jpeg