Control-structure - while loop and do-while loop.pptx

arshadfarhad08 16 views 33 slides Oct 12, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

This presentation discusses the use of while and do-while using C++ language with examples.


Slide Content

Lecture-10: Loops Computer Programming 10/12/24 1

Today we will cover The while loop The while Loop for Input Validation Counters The do-while loop 2

Conditional Operator (? :) Syntax (Boolean expression) ? Statement1 : Statement2; 3

The while Loop

The while Loop Loop : a control structure that causes a statement or statements to repeat General format of the while loop: while ( expression ) statement ; statement ; can also be a block of statements enclosed in { } 5

The while Loop – How It Works while ( expression ) statement ; expression is evaluated if true , then statement is executed, and expression is evaluated again if false , then the loop is finished and program statements following statement execute 6

The Logic of a while Loop 7

The while loop-Example 8

How the while Loop in previous slide Lines 9 through 13 Works 9

Flowchart of the while Loop 10

The while Loop is a Pretest Loop expression is evaluated before the loop executes. The following loop will never execute: int n = 6; while (n <= 5) { cout << "Hello! ICP\n"; number++; } 11

Watch Out for Infinite Loops The loop must contain code to make expression become false Otherwise, the loop will have no way of stopping Such a loop is called an infinite loop , because it will repeat an infinite number of times 12

Example of an Infinite Loop int n = 1; while (n <= 5) { cout << "Hello ICP\n"; } 13

Using the while Loop for Input Validation

Using the while Loop for Input Validation Input validation is the process of inspecting data that is given to the program as input and determining whether it is valid. The while loop can be used to create input routines that reject invalid data, and repeat until valid data is entered. 15

Using the while Loop for Input Validation Here's the general approach, in pseudocode: Read an item of input. While the input is invalid Display an error message. Read the input again. End While 16

Input Validation Example cout << "Enter a number less than 10: "; cin >> number; while (number >= 10) { cout << "Invalid Entry!" << "Enter a number less than 10: "; cin >> number; } 17

18

19

Flowchart for Input Validation 20

Input Validation in Program 5-5 21

Counters

Counters Counter : a variable that is incremented or decremented each time a loop repeats Can be used to control execution of the loop (also known as the loop control variable ) Must be initialized before entering loop 23

A Counter Variable Controls Continued… 24

A Counter Variable Controls 25

The do-while Loop

The do-while Loop do-while : a posttest loop – execute the loop, then test the expression General Format: do statement ; // or block in { } while ( expression ); Note that a semicolon is required after ( expression ) 27

The Logic of a do - while Loop 28

An Example do - while Loop int x = 1; do { cout << x << endl; } while(x < 0); Although the test expression is false, this loop will execute one time because do - while is a posttest loop. 29

A do-while Loop Continued… 30

A do-while Loop 31

do-while Loop Notes Loop always executes at least once Execution continues as long as expression is true , stops repetition when expression becomes false 32

Questions 10/12/24 33