Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Similar presentations


Presentation on theme: "CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)"— Presentation transcript:

1 CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

2 2 C ONTENTS Introduction The for loops Nested for loops The while loops Counter-controlled while loops Sentinel-controlled while loops The do..while loops break and continue statement

3 3 Introduction The repetition structure is a section of repeating code in a program (a loop) The loop contains a set of statements to be executed repeatedly based on a condition 3 basic types of loops : for while do..while

4 4 THE for LOOP The general form of the for statement is: for (initial statement; loop condition; update statement) statement; The initial statement, loop condition, and update statement are called for loop control statements

5 5 THE for LOOP

6 6 The initial statement consists of a single statement used to set the initial or starting value of a variable called counter variable The loop condition tests the value of the counter variable and determines when the loop is to stop The expression (update statement) provides the increment value added or subtracted from the counter variable each time the loop is executed

7 7 THE for LOOP During the execution of the for statement, the following steps of action occurs : 1.The initial statement is evaluated. 2.The loop condition is evaluated. 3.If the loop condition evaluates to true, then i. statement is executed ii.execute the update statement (the third expression in the parentheses). iii.repeat Step 2 until the loop condition evaluates to false. otherwise the for loop terminates and control transfers to the next statement following it.

8 8 THE for LOOP The use of for loops are shown as follows : Example 1 :

9 9 THE for LOOP Example 2 : #include

10 10 THE for LOOP 2 forms of the for statement : Ascending for loop Example : for (variable=initial_value; variable <= final_value; increment expression) e.g : for (i = 10; i <= 40; i += 5) Descending for loop for (variable=initial_value; variable >= final_value; decrement expression) e.g : for (i = 40; i >= 10; i -= 5)

11 11 THE while LOOPS Required for repetitive execution that cannot be determined in advance The syntax : while (condition) statement; statement can be simple or compound; the body of the loop condition acts as a decision maker and is usually a logical expression The parentheses are part of the syntax

12 12 THE while LOOPS condition provides an entry condition statement executes if the expression initially evaluates to true Loop condition is then reevaluated Statement continues to execute until the expression is no longer true (false)

13 13 Counter-controlled while loops o If you know exactly how many pieces of data need to be read, the while loop becomes a counter controlled loop

14 14 Counter-controlled while loops Example:

15 15 Sentinel-controlled while loops o Sentinel variable is tested in the condition and loop ends when sentinel is encountered

16 16 Sentinel-controlled while loops Example: sum/counter;


Download ppt "CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)"

Similar presentations


Ads by Google