Presentation is loading. Please wait.

Presentation is loading. Please wait.

Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.

Similar presentations


Presentation on theme: "Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each."— Presentation transcript:

1 Think Possibility 1 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.

2 Think Possibility 2 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.

3 Think Possibility 3 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”);

4 Think Possibility 4 PROGRAMMING EXERCISES: 1.Write a C program that prompts the user to enter several integers and calculates the average. 2.Write a C program that prints all even numbers between 1 and 50. 3.Write a C program that prints all odd numbers between 1 and 50.

5 Think Possibility 5 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.

6 Think Possibility 6 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.

7 Think Possibility 7 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 }

8 Think Possibility 8 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)

9 Think Possibility 9 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

10 Think Possibility 10 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.

11 Think Possibility 11 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.

12 Think Possibility 12 Exercises: 1.PROGRAM TO COMPUTE 2 5 2.PROGRAM TO COMPUTE base exp 3.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. 4.Repeat the previous exercise except that the user is limited to three (3) trials only. If the user exceeds this limit, the program terminates. 5.Write a C program that prompts the user to enter several integers and then outputs the one with the highest value.

13 Think Possibility 13 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.

14 Think Possibility 14 Everytime C encounters a do statement, it 1.executes statement. 2.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. 3.If expression is false, then C executes the statement after the do loop.

15 Think Possibility 15 Example: # include main() { int num; do { printf ("\n\nEnter an integer between 5 and 10 > "); scanf ("%d", &num); if (num 10) { printf ("\n\nInvalid number!!! Please try again."); printf ("\n\nEnter an integer between 5 and 10 > "); scanf ("%d", &num); } } while (num 10); printf ("The number you entered is %d.", num); }

16 Think Possibility 16 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 between 1 to 50 will be printed.

17 Think Possibility 17 #include 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.

18 Think Possibility 18 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.


Download ppt "Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each."

Similar presentations


Ads by Google