chapter 3 control structures ( repetition ) i ntroduction t o c omputer p rogramming (csc425)

16
CHAPTER 3 CHAPTER 3 CONTROL STRUCTURES CONTROL STRUCTURES (REPETITION) (REPETITION) INTRODUCTION TO INTRODUCTION TO COMPUTER PROGRAMMING COMPUTER PROGRAMMING (CSC425) (CSC425)

Upload: ethan-small

Post on 04-Jan-2016

228 views

Category:

Documents


2 download

TRANSCRIPT

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

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)

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)

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)

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)

5

THE for LOOP

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

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)

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)

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)

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)

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)

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)

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)

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)

14

Counter-controlled while loops

Example:

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

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)

16

Sentinel-controlled while loopsExample:

sum/counter;