CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
-
Upload
ethan-small -
Category
Documents
-
view
225 -
download
2
Transcript of CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
![Page 1: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/1.jpg)
CHAPTER 3CHAPTER 3CONTROL STRUCTURESCONTROL STRUCTURES
(REPETITION)(REPETITION)
INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER
PROGRAMMINGPROGRAMMING(CSC425)(CSC425)
![Page 2: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/2.jpg)
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
![Page 3: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/3.jpg)
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
![Page 4: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/4.jpg)
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
![Page 5: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/5.jpg)
5
THE for LOOP
![Page 6: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/6.jpg)
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
![Page 7: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/7.jpg)
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.
![Page 8: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/8.jpg)
8
THE for LOOP
The use of for loops are shown as follows :Example 1 :
![Page 9: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/9.jpg)
9
THE for LOOP
Example 2 :#include <iostream.h>#include<math.h>#include<iomanip.h>
![Page 10: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/10.jpg)
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)
![Page 11: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/11.jpg)
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
![Page 12: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/12.jpg)
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)
![Page 13: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/13.jpg)
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
![Page 14: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/14.jpg)
14
Counter-controlled while loops
Example:
![Page 15: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/15.jpg)
15
Sentinel-controlled while loopso Sentinel variable is tested in the condition and loop
ends
when sentinel is encountered
![Page 16: CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.fdocuments.in/reader036/viewer/2022070401/56649f205503460f94c38508/html5/thumbnails/16.jpg)
16
Sentinel-controlled while loopsExample:
sum/counter;