Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each uses the four parts of a loop a little differently, giving different loop options to the programmer. 1 Think Possibility The for Statement The for-loop has the following form. for (initialization; condition; update) Statement where expression1 is an assignment expression (called the initial condition), expression2 is a relational, equality, or logical expression (called the test condition), and expression3 is another assignment expression (called the loop operation). All three expressions usually have a common variable. 2 Think Possibility Everytime C encounters a for statement, it 1. executes the initial condition. 2. If the test condition is false, the for loop ends (it does not execute statement). 3. If the test condition is true, it executes statement. 4. It then executes the loop operation and repeats step 2. Example: for (j = 1; j <= 10; ++j) printf (“Hello!\n”); 3 Think Possibility PROGRAMMING EXERCISES: 1. 2. 3. Write a C program that prompts the user to enter several integers and calculates the average. Write a C program that prints all even numbers between 1 and 50. Write a C program that prints all odd numbers between 1 and 50. 4 Think Possibility INFINITE LOOPS Infinite loops are also possible in using for statements. Examples: 1. for (j = 1; ; ++j) printf (“Hello!\n”); In this example, the for statement does not have a test condition. Therefore, there is no condition for the loop to terminate. 2. for (j = 1; j <= 10; ) printf (“Hello!\n”); In this example, the for statement does not have a loop operation. Therefore, the test condition will never become false. 5 Think Possibility The while Statement The while-loop has the following form. initialization while (condition) { body update } where expression is the conditional expression which represents a relational, equality, or logical expression. 6 Think Possibility Everytime C encounters a while statement, it 1. determines whether expression is true or false. 2. If expression is true, then C executes statement and control passes back to the beginning of the while loop. If expression is still true, then C executes statement again. This process is repeated while the expression is true. 3. If expression is initially false, then C ignores the remainder of the while statement. Example: sum = 0; counter = 1; while (counter <= 10) { sum += 1; ++counter } 7 Think Possibility Counting, adding, searching, sorting, and other tasks often involve doing something over and over. The while statement in C is very useful in performing repetitive actions. Example: Write a program to compute 25 using the while statement. Algorithm: 25 = 2 * 2 * 2 * 2 * 2 = 32 25 = multiply 2 to itself 5 times Let i be a counter to monitor how many times the multiplication has taken place. y = 1 (y will eventually hold the final answer) 8 Think Possibility i=1 y=y*2 =1*2=2 i =i+1 =1+1=2 i=2 y=y*2= 2*2= 4 i=i+1 = 2+1=3 i=3 y=y*2=4*2=8 i=i+1 =3+1=4 i=4 y = y * 2 = 8 * 2 = 16 i=i+1 =4+1=5 i=5 y = y * 2 = 16 * 2 = 32 i=i+1 =5+1 =6 9 Think Possibility The algorithm requires that after initializing variables y and i to 1, the statements y=y*2 i=i+1 be repeated 5 times or while i is less than or equal to 5. 10 Think Possibility INFINITE LOOPS The programmer must always make sure that the conditional expression in a while loop must sooner or later become false. Otherwise, the program will enter in an infinite loop. In other words, the loop will not terminate. Example: sum = 0; counter = 1; while (counter <= 10) { sum += 1; } In this example, the conditional expression of the while loop (counter <= 10) will never become false since counter will always be equal to 1. 11 Think Possibility Exercises: 1. 2. 3. 4. 5. PROGRAM TO COMPUTE 25 PROGRAM TO COMPUTE baseexp Write a C program that prompts the user to enter an integer between 5 and 10. The program will then output the number typed in by the user. However, the program should make sure that the number typed in is indeed between 5 and 10. Repeat the previous exercise except that the user is limited to three (3) trials only. If the user exceeds this limit, the program terminates. Write a C program that prompts the user to enter several integers and then outputs the one with the highest value. 12 Think Possibility The do-while Statement The do statement is a variant of the while statement. Instead of making its test at the top of the loop, it makes it at the bottom. The general format of the do statement is: do statement while (expression) where expression is the conditional expression which represents a relational, equality, or logical expression. 13 Think Possibility Everytime C encounters a do statement, it 1. 2. 3. executes statement. The program then evaluates expression. If expression is true, then control passes back to the beginning of the do loop and the process repeats itself. If expression is false, then C executes the statement after the do loop. 14 Think Possibility Example: # include <stdio.h> main() { int num; do { printf ("\n\nEnter an integer between 5 and 10 > "); scanf ("%d", &num); if (num < 5 || num > 10) { printf ("\n\nInvalid number!!! Please try again."); printf ("\n\nEnter an integer between 5 and 10 > "); scanf ("%d", &num); } } while (num < 5 || num > 10); printf ("The number you entered is %d.", num); 15 } Think Possibility THE break AND continue STATEMENTS To interrupt the normal flow of control within a loop (a while or for loop), the programmer can use either the break or continue statement. The break statement causes an exit from an enclosing loop. In other words, encountering the break statement causes immediate termination of the loop. Program control picks up at the code following the loop. Example: for (ctr = 1; ctr <= 100; ++ctr) { printf (“%d\n”, ctr); if (crt = = 50) break; } This program segment will normally print all numbers between 1 to 100. But because of the break statement, only the numbers 16 between 1 to 50 will be printed. Think Possibility #include <stdio.h> main () { int x; for ( ; ; ) { printf ("\nGuess the secret number > "); scanf (“%d”, &x); if (x = = 5) break; else printf ("\nSorry!!!"); } printf("\n\nCongratulations!!!"); } This program will simply ask the user to continuously enter a number until the input is equal to 5. 17 Think Possibility The continue statement works in a somewhat similar way to the break statement. But instead of forcing loop termination, continue forces the next iteration for the loop to take place, skipping any code in between. Example: for (ctr = 1; ctr <= 100; ++ctr) { if (x%2 ! = 0) continue; printf (“%d\n”, ctr); } This program segment will print all even numbers between 1 to 100. 18 Think Possibility