You are on page 1of 23

EGN 3211 Engineering Analysis and Computation

Chapter 5: Statements and Control Flow C for Engineers and Scientists: An Interpretive Approach, Harry H. Cheng

University of Central Florida - John Edison Spring 2014

Statements and Control Flow

Statements in C general execute some operation and are terminated using the semicolon Statements in C may be sequential, conditional, or repetitive

Sequential statements are executed in the order they are written Conditional statements are executed based on some condition Repetitive statements are executed until a terminating condition is met

University of Central Florida - John Edison Spring 2014

if statement

The if statement is a conditional expression where if true the statement is executed The syntax of an if statement is as follows:
if( month == 1) // conditional expression
{ printf(January); // statement } // rest of month converstion

University of Central Florida - John Edison Spring 2014

if statement

Flow diagram of an if statement

University of Central Florida - John Edison Spring 2014

if-else statement

The if-else statement is a conditional expression where if true the ifstatement is executed and if false then the else-statement is executed The syntax of an if-else statement is as follows:
if( i >= 0 ) // conditional expression
{ printf(%d, sqrt(i)); // statement } else { printf(Answer is imaginary); }

University of Central Florida - John Edison Spring 2014

if-else statement

Flow diagram of an if-else statement

University of Central Florida - John Edison Spring 2014

else-if statement

The else-if statement is a conditional expression with multiple conditions The syntax of an else-if statement is as follows:
if( grade >= 90 ) // conditional expression printf(A); else if ( grade >= 80 ) printf(B); else if ( grade >= 70 ) printf(C); // rest of code

University of Central Florida - John Edison Spring 2014

switch statement

The switch statement is a conditional statement that matches a value The switch statement shown on the right checks the integer value month and prints out the corresponding month as a string The default case is executed if the input doesnt match any of the cases if an integer is input that is less than 1 or greater than 12 Notice that the break statement is used after each case to exit out of the switch statement If a break statement is not put there it will continue to execute through the next case until a break if found or it exits out of the switch statements

switch(month) { case 1: printf(January); break; case 2: printf(February); break; // rest of code default: printf(Invalid month); break; }

University of Central Florida - John Edison Spring 2014

switch statement

Flow diagram of a switch statement


University of Central Florida - John Edison Spring 2014

while loop

The while loop is a repetition statement that executes the expression(s) within the curly braces until the expression evaluates false The expression is checked after each iteration through the while loop The syntax of an while loop is as follows:
int num = 0; while( num < 10 ) { printf(num = %d\n, num++); }

University of Central Florida - John Edison Spring 2014

while loop

Flow diagram of a while loop


University of Central Florida - John Edison Spring 2014

do-while loop

The do-while loop is a while repetition statement that first executes the expression(s) within the curly braces before testing the while condition The syntax of an do-while loop is as follows:
int num = 100; do { printf(num = %d\n, num++); } while( num < 10 );

University of Central Florida - John Edison Spring 2014

do-while loop

Flow diagram of a do-while loop


University of Central Florida - John Edison Spring 2014

for loop

The for loop is a repetition statement that executes the expression(s) until the middle expression evaluates false The syntax of an for loop is as follows:
int num = 100; for(num=0;num<10;num++) { printf(num = %d\n, num); }

University of Central Florida - John Edison Spring 2014

for loop

Flow diagram of a for loop


University of Central Florida - John Edison Spring 2014

nested loops

A nested loop is a loop within another loop


for(int width = 1; width < 11; width++) { for(int height = 1; height < 11; height++) { printf(area = %d\n, (width*height)); } }

University of Central Florida - John Edison Spring 2014

break statement

The break statement provides a way to terminate a for, while, or do-while loop
int width = 0, height = 0, area = 0; while( true) {
height++, width++; area = width * height; if( area > 100) break; printf(the area when width = %d and height = %d\n, width, height); printf(area = %d\n, area);

}
University of Central Florida - John Edison Spring 2014

break statement

Flow diagram of a do-while loop with a break statement


University of Central Florida - John Edison Spring 2014

continue statement

The continue statement provides a way to skip to the next iteration of a for, while, or do-while loop
int width = 0, height = 0, area = 0; while( area < 100 ) { if(area < 1)
continue; else {
printf(the area when width = %d and height = %d\n, width, height); printf(area = %d\n, area);

// only print when the area is greater than 0

} height++, width++; area = width * height;

}
University of Central Florida - John Edison Spring 2014

continue statement

Flow diagram of a do-while loop with a continue statement


University of Central Florida - John Edison Spring 2014

goto statement

The goto statement causes an unconditional jump to the statement prefixed by the named label
int width = 0, height = 0, area = 0; while( true) {
height++, width++; area = width * height; if( area > 100) goto end_loop; printf(the area when width = %d and height = %d\n, width, height); printf(area = %d\n, area);

} end_loop: ;
University of Central Florida - John Edison Spring 2014

goto statement

Flow diagram of a goto statement


University of Central Florida - John Edison Spring 2014

Sample Programs

Leap Year (if-else)

if year is divisible by 400 then is_leap_year else if year is divisible by 100 then not_leap_year else if year is divisible by 4 then is_leap_year else not_leap_year

Fibbonacci Number (while) Print a square (for) Area of squares (length 1-10) (goto)

University of Central Florida - John Edison Spring 2014