chapter 3 control structures ( repetition ) i ntroduction t o c omputer p rogramming (csc425)
TRANSCRIPT
CHAPTER 3CHAPTER 3CONTROL STRUCTURESCONTROL STRUCTURES
(REPETITION)(REPETITION)
INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER
PROGRAMMINGPROGRAMMING(CSC425)(CSC425)
2
CONTENTS
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
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
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
THE for LOOP
6
THE for LOOP
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
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 executedii.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
THE for LOOP
The use of for loops are shown as follows :Example 1 :
9
THE for LOOP
Example 2 :#include <iostream.h>#include<math.h>#include<iomanip.h>
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
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
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
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
Counter-controlled while loops
Example:
15
Sentinel-controlled while loopso Sentinel variable is tested in the condition and loop
ends
when sentinel is encountered
16
Sentinel-controlled while loopsExample:
sum/counter;