Decision Making and Loops Goals of this section Continue looking at decision structures - switch control structures -if-else-if control structures Introduce looping -while loop -do-while loop -simple for loop -nested for loops 85-132 Decision Making and Loops 8-1 if-else-if Control Structure /* trivial example of if-else-if control structure */ #include <stdio.h> void main(void) int i; printf( input an integer value, 0, 1, or 10:\n ); scanf( %d,&i); if(i == 0) printf( you picked zero ); else if(i == 1) printf( you picked one ); else if(i == 10) printf( you picked 10 ); else printf( you did not pick any of the cases\n ); printf( try again ); 85-132 Decision Making and Loops 8-2 85-132: Introduction to C-Programming 1
switch Control Structure #include <stdio.h> // I removed white space to save space void main(void) int i; printf( input an integer value, 0, 1 or 10: \n ); scanf( %d,&i); switch(i) case 0: printf( you picked 0 ); break; case 1: printf( you picked 1 ); break; case 10: printf( you picked 10 ); break; default: printf( you did not pick any of the cases\n ); printf( try again ); 85-132 Decision Making and Loops 8-3 while Loop loop is an iterative control structure which involves repeated execution of one or more statements syntax for while loop while (expression) false true example i=1; while (i<=5) printf ( Loop number %d\n,i); i=i+1; /* alternatively */ i=1; while (i <= 5) printf( Loop number %d\n,i); i++; //same as i=i+1 Loop number 2 Loop number 3 Loop number 4 Loop number 5 85-132 Decision Making and Loops 8-4 85-132: Introduction to C-Programming 2
variable =0 while Loop additional syntax of while loop while (variable) variable 0 example i=3; while (i) // while(i) same as while (i!=0) printf ( Loop number %d\n,i); i--; Loop number 3 Loop number 2 85-132 Decision Making and Loops 8-5 while Loop if the while loop s test expression is false initially, the loop is never executed if the while loop s test expression never becomes false, an infinite loop is created and the program will stop example i=3; while (i) printf ( Loop number %d\n,i); i=i++; /* prevention of an infinite loop*/ //put a counter in the loop i=3; icount=0; while (i && icount<51) printf( Loop number %d\n,i); i++; icount++; 85-132 Decision Making and Loops 8-6 85-132: Introduction to C-Programming 3
do-while Loop do-while loops are almost identical to while loops except they are always executed at least once even if the while test expression is false syntax of do-while loop do while (expression); NOTE that the while test expression occurs at the end of the block and if it is false the block executes only once /* example of do-while loop*/ i=1; do printf( Loop number %d\n,i); i++; while(i>5); 85-132 Decision Making and Loops 8-7 for Loop for loops are used when you know how many times a loop needs to be executed syntax of for loop for (initialization; test expression; increment expression) /* example for loop */ for(i=1; i<=100; i++) printf( loop number %d\n,i); Loop number 2 Loop number 99 00 85-132 Decision Making and Loops 8-8 85-132: Introduction to C-Programming 4
for Loop Changing the increment expression /* example for loop */ /* example for loop */ for(i=1; i<=100; i+=2) printf( loop number %d\n,i); for(i=100; i>=0; i-=5) printf( loop number %d\n,i); Loop number 3 Loop number 5 Loop number 97 Loop number 99 00 Loop number 95 Loop number 90 Loop number 5 Loop number 0 85-132 Decision Making and Loops 8-9 Nested for Loop /* example of a nested for loop */ #include <stdio.h> void main (void) int i,j; 3X for(i=1; i<=3; i++) for(j=1; j<=4; j++) 4X printf( i = %d, j = %d\n,i,j); i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 1, j = 4 i = 2, j = 1 i = 2, j = 2 i = 2, j = 3 i = 2, j = 4 i = 3, j = 1 i = 3, j = 2 i = 3, j = 3 i = 3, j = 4 85-132 Decision Making and Loops 8-10 85-132: Introduction to C-Programming 5
Your Tasks Read Sections 4.6 to 4.11 (Tan and D Orazio, 1999) Review this lecture Prepare for assignment 5 Make sure you review and keep up if you are not up-to-date and do not make an all out effort to catch up as of today, all I can wish you is good luck and we ll see you in the Appeals Committee 85-132 Decision Making and Loops 8-11 85-132: Introduction to C-Programming 6