Top Banner
Basic Control Flow: Repetitions and Loop Statements TCB2073 Structured Programming & Database Systems
24
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 4 c++

Basic Control Flow: Repetitions and Loop Statements

TCB2073Structured Programming & Database

Systems

Page 2: Lecture 4 c++

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

Page 3: Lecture 4 c++

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

Control Structures Revisit

Sequence Selection Repetition

Page 4: Lecture 4 c++

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

C++ Repetition Structures

Page 5: Lecture 4 c++

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

Page 6: Lecture 4 c++

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

Page 7: Lecture 4 c++

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

Page 8: Lecture 4 c++

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

Problem 1: Print N (qty) asterisks

Perform desk check!

Page 9: Lecture 4 c++

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

Problem 2: Descending order (N to 0)

Perform desk check!

Page 10: Lecture 4 c++

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

Problem 3: Sum a series of N numbers

Perform desk check!

Page 11: Lecture 4 c++

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

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

Page 12: Lecture 4 c++

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

Page 13: Lecture 4 c++

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 <<” * “;

}

Page 14: Lecture 4 c++

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

Problem 1: Print N (qty) asterisks

Perform desk check!

Page 15: Lecture 4 c++

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

Page 16: Lecture 4 c++

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

Page 17: Lecture 4 c++

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);

Page 18: Lecture 4 c++

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

do .. while Counting Stars

Page 19: Lecture 4 c++

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

Problem 3: to sum N numbers, without knowing N

updating

testing

Page 20: Lecture 4 c++

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

Page 21: Lecture 4 c++

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

Page 22: Lecture 4 c++

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

Page 23: Lecture 4 c++

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

Page 24: Lecture 4 c++

TBB1073 : Basic Control Flow -Repetition and loop statements

Summary

Page 24 of 13