Lecture 9 Repetition Structures II For and do-while loops CptS 121 Summer 2016 Armen Abnousi
Types of Control Structures Sequential All programs that we have written so far The statements inside a pair of Statements runs one by one, in the order they are written Selection Allows for the execution of alternatives Runs a set of statements if a certain condition (set by the programmer) is met, otherwise skips those operations Repetition Allows for repeating the execution of some statements until a condition is met (or for a certain time). Repetition of steps of a program is called a loop.
While loops We saw before that while loop is an example of repetition structures. while ( conditional expression ) /*make an effort to change the condition value*/
For loops for is another tool providing repetition control structure. for (initialization; condition; update) /*statements you want to repeat*/
For vs while - example Print the integers in range of [2, 5]: For loop: int counter = 0; for ( counter = 2; counter < 6; counter++) printf( %d, counter); While loop: int counter = 2; while ( counter < 6 ) printf( %d, counter); counter++;
For loop Factorial problem Write a program that given the integer n, computes n! int product = 1; int product = 1; for ( i = n; i > 1; i-- ) for ( i = 2; i <= n; i++) product *= i; product *= i;
For loops conditions and update rules The condition for the loop can be any conditional expression that we have seen before (anything that can evaluate to true or false) Remember any non-zero number means true and zero means false The update rule can be any computation that alters the loop control variable. Be careful about the infinite loops!
For loops using sentinel values The user wants to enter some numbers and wants us to compute the sum, until she enters -2 (sentinel value): sum = 0; printf( please enter a number: \n) for ( scanf( %c, &response); response!= -2; scanf( %d, &response) ) sum += response; printf( please enter another number:\n );
A note about the scanf() scanf is a function, and like any other function has a return value. The return value for scanf is int and represents the number of arguments it has successfully read scanf( %c, &mychar); => when user inputs a will return 1 scanf( %d%d, &int1, &int2); => when user inputs 23 51 will return 2 This can be used for finding errors: scanf( %d%d, &int1, &int2); => when user inputs 23 a will return 1
Do-while loops For and while loops both perform a condition evaluation before the first execution of loop body Sometimes a loop must execute at least once Do-while loops first execute the body of the loop and then check if the condition is satisfied (if it is will repeat the loop body, otherwise will exit the loop) Useful for input validation (if the input value is wrong, continue asking until the user gives a correct input)
Do-while loops do /* loop body */ while (expression);
Do-while - example Ask the user to enter a choice between a, b, c, or d: do printf( please enter a choice a.. d : ); scanf( %c, &mychar); while ( mychar < a d < mychar );
Do-while - example 2 Ask the user to enter a choice between 1, 2, 3, or 4: do printf( please enter a choice 1.. 4 : ); scanf( %d, &myint); while ( myint < 1 4 < myint ); Will this work? What can go wrong?
Do-while - example 2 Ask the user to enter a choice between 1, 2, 3, or 4: do printf( please enter a choice 1.. 4 : ); scanf( %d, &myint); while ( myint < 1 4 < myint ); Will this work? What can go wrong? What if you enter a character (letter)? That will stay in the buffer and scanf will continue trying to read that with %d placeholder! Infinite loop!
Do-while - example 2 Instead this will correct the code: char mychar = 'a'; int myint = 0, status = 0; do printf("please enter a choice 1.. 4 :"); status = scanf("%d", &myint); if (status!= 1) scanf("%c", &mychar); printf("invalid character %c", mychar); do scanf("%c", &mychar); while (mychar!= '\n'); while (myint < 1 4 < myint);
Do-while - example 2 Instead this will correct the code: char mychar = 'a'; int myint = 0, status = 0; do printf("please enter a choice 1.. 4 :"); status = scanf("%d", &myint); if (status!= 1) scanf("%c", &mychar); printf("invalid character %c", mychar); do scanf("%c", &mychar); while (mychar!= '\n'); while (myint < 1 4 < myint); If status is not 1 means user didn t input an int. Then read the first character and say it is invalid Read the whole line character by character until it reaches a new line
Nested loops Loops can be nested into each other (like if statements) Every time the outer loop executes it enters the inner loop and performs all the iterations for ( j = 0; j < 3; j++ ) for ( m = (int) A ; m < (int) E ; m++) Will print: printf( \n ); printf( %c, (char) m); ABCD ABCD ABCD
Nested loops for ( i = 5; i > 0; i-- ) for ( j = 0; j < i; j++ ) printf( %-3f, j); printf( \n ); This will print
Nested loops for ( i = 4; i > 0; i-- ) for ( j = 0; j < i; j++ ) printf( %-3f, j); printf( \n ); This will print 0 1 2 3 0 1 2 0 1 0
References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8thed.), Pearson, 2016 Andy O Fallon s lecture notes for CptS121 (http://eecs.wsu.edu/~aofallon/cpts121)