+ All Categories
Home > Documents > Lecture 4 c++

Lecture 4 c++

Date post: 27-Oct-2014
Category:
Upload: renuga-subramaniam
View: 49 times
Download: 2 times
Share this document with a friend
Popular Tags:
24
Basic Control Flow: Repetitions and Loop Statements TCB2073 Structured Programming & Database Systems
Transcript

Basic Control Flow: Repetitions and Loop Statements

TCB2073Structured Programming & Database

Systems

TBB1073 : Basic Control Flow -Repetition and loop statements Page 2 of 13

Objectives

By the end of this lecture, you will be able to: describe the C++ repetition structures (while, for, do..while)

design and implement a C++ program that involves repetition structures

use SENTINEL in a do .. while statement desk check loop statement

TBB1073 : Basic Control Flow -Repetition and loop statements Page 3 of 13

Control Structures Revisit

Sequence Selection Repetition

TBB1073 : Basic Control Flow -Repetition and loop statements Page 4 of 13

C++ Repetition Structures

TBB1073 : Basic Control Flow -Repetition and loop statements Page 5 of 13

Terms

loop a control structure that repeats a group of steps in a program.

loop body the statements that are repeated in the loop.

counter-controlled loop (counting loop) a loop whose required number of iterations can be determined before loop execution begins

loop repetition condition the condition that controls loop repetition

loop control variable the variable whose value controls loop repetition

infinite loop a loop that executes forever

TBB1073 : Basic Control Flow -Repetition and loop statements Page 6 of 13

General Flowchart for while Statement (counter-controlled loop)

initialisation

testing

updating

Loop body

TBB1073 : Basic Control Flow -Repetition and loop statements Page 7 of 13

While Statement Syntax

SYNTAX: while (loop repetition condition)statement

EXAMPLE: /*display N asterisks */count_star = 0;while (count_star < N) {

cout << ” * ”;count_star = count_star + 1;

}

INTERPRETATION : The statements in the body loop are repeated as long as the loop repetition condition is true. The while loop is exited when the repetition condition is false

TBB1073 : Basic Control Flow -Repetition and loop statements Page 8 of 13

Problem 1: Print N (qty) asterisks

Perform desk check!

TBB1073 : Basic Control Flow -Repetition and loop statements Page 9 of 13

Problem 2: Descending order (N to 0)

Perform desk check!

TBB1073 : Basic Control Flow -Repetition and loop statements Page 10 of 13

Problem 3: Sum a series of N numbers

Perform desk check!

TBB1073 : Basic Control Flow -Repetition and loop statements Page 11 of 13

Problem 4: Find the average of a series of N numbers

TBB1073 : Basic Control Flow -Repetition and loop statements Page 12 of 15

do .. while statement

executes the statements in the loop body at least once

Loop body

updating

testing

TBB1073 : Basic Control Flow -Repetition and loop statements Page 13 of 15

for Statement Syntax

SYNTAX: for (initialisation expression;loop repetition condition;update expression)statement

EXAMPLE: /*display N asterisks */for (count_star = 0;

count_star < N;count_star++) {cout <<” * “;

}

TBB1073 : Basic Control Flow -Repetition and loop statements Page 14 of 15

Problem 1: Print N (qty) asterisks

Perform desk check!

TBB1073 : Basic Control Flow -Repetition and loop statements Perform desk check! Page 15 of 15

Problem 2: Descending order (N to 0)

intialisation

testing

updating

TBB1073 : Basic Control Flow -Repetition and loop statements Page 16 of 15

do .. while statement

executes the statements in the loop body at least once

Loop body

updating

testing

TBB1073 : Basic Control Flow -Repetition and loop statements Page 17 of 15

do .. while statement syntax

SYNTAX: do {statement

} while(loop repetition condition);

EXAMPLE: /*display N asterisks */count_star = 0;do { cout <<“ * ”; count_star++;} while (count_star < 5);

TBB1073 : Basic Control Flow -Repetition and loop statements Page 18 of 15

do .. while Counting Stars

TBB1073 : Basic Control Flow -Repetition and loop statements Page 19 of 15

Problem 3: to sum N numbers, without knowing N

updating

testing

TBB1073 : Basic Control Flow -Repetition and loop statements Page 20 of 15

SENTINEL

Sentinel value an end marker that follows the last item in a list of data

e.g#define SENTINEL 999

List of data:56 90 85 100 72 45 999

Sentinel value

TBB1073 : Basic Control Flow -Repetition and loop statements Page 21 of 6

Problem #1

Write a nested loop to produce the following pattern if the user input is 5

TBB1073 : Basic Control Flow -Repetition and loop statements Page 22 of 6

Problem #2

Write a nested loop to produce the following pattern if the user input is 5

TBB1073 : Basic Control Flow -Repetition and loop statements Page 23 of 6

Problem #3

Write a nested loop to produce the following pattern if the user input is 5

TBB1073 : Basic Control Flow -Repetition and loop statements

Summary

Page 24 of 13


Recommended