• No results found

Session: “while” and “do while” statements in C++

N/A
N/A
Protected

Academic year: 2022

Share "Session: “while” and “do while” statements in C++"

Copied!
27
0
0

Loading.... (view fulltext now)

Full text

(1)

IIT Bombay

Computer Programming

Dr. Deepak B Phatak Dr. Supratik Chakraborty

Department of Computer Science and Engineering IIT Bombay

Session: “while” and “do while” statements in C++

(2)

IIT Bombay

• Iteration idioms in programming

• Necessity and convenience of iteration

• Glimpse of iteration constructs in C++

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Iteration using “while” and “do … while” statements in C++

• “break” statement in loops

Overview of This Lecture

(4)

IIT Bombay

Recall Generic Iteration Construct

Part of program before iteration

Iteration initialization (setting up initial values, etc)

Iterate/Repeat as long as a logical condition stays true {

Block of statements

Optional instructions to execute at end of every iteration }

Part of program after iteration

Loop Condition

Loop

Loop Body

(5)

IIT Bombay

“while” Statement in C++

Part of program before iteration

Iteration initialization (setting up initial values, etc)

while (loop condition)

{

Block of statements

Optional instructions to execute at end of every iteration }

Part of program after iteration

Block of statements (Body of “while” loop)

Part of program before execution

(6)

IIT Bombay

Flowchart Representation of “while”

Part of program before iteration

Logical expression (loop condition)

Loop Body Part of program after iteration

FALSE

TRUE

(7)

IIT Bombay

Points To Remember About “while”

while (loop condition) { Loop Body }

• Loop condition checked before executing loop body Can lead to zero executions of loop body

• Number of times loop condition is checked =

Number of times loop body executed + 1, if loop terminates

• If loop condition is not changed in loop body, inifinite loop

(non-terminating program) possible

(8)

IIT Bombay

Back To Our Problem

Read number of students in CS101, read quiz 1 marks of all

CS101 students and print their sum, average, maximum and

minimum

(9)

IIT Bombay

Flowchart Representation

Read numStudents

Initialize aggregates (sum, min, max) Initialize count to 1

count <= numStudents?

Ask for marks Read marks

Update aggregates Increment count

Compute average Print aggregates

FALSE

TRUE

(10)

IIT Bombay

C++ program with “while”

int main() {

int marks, sum = 0, min, max, numStudents;

float average, count; // Variable declarations

cout << “Give number of students: “; cin >> numStudents;

count = 1.0; // Count of student marks processed while (count <= numStudents) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

// Update sum, max, min count = count + 1;

}

average = sum/count;

// Print average, sum, min, max return 0;

}

(11)

IIT Bombay

C++ program with “while”

int main() {

int marks, sum = 0, min, max, numStudents;

float average, count; // Variable declarations

cout << “Give number of students: “; cin >> numStudents;

count = 1.0; // Count of student marks processed while (count <= numStudents) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

// Update sum, max, min count = count + 1;

}

average = sum/count;

// Print average, sum, min, max return 0;

}

(12)

IIT Bombay

C++ program with “while”

int main() {

int marks, sum = 0, min, max, numStudents;

float average, count; // Variable declarations

cout << “Give number of students: “; cin >> numStudents;

count = 1.0; // Count of student marks processed while (count <= numStudents) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

// Update sum, max, min count = count + 1;

}

average = sum/count;

// Print average, sum, min, max return 0;

}

(13)

IIT Bombay

C++ program with “while”

int main() {

int marks, sum = 0, min, max, numStudents;

float average, count; // Variable declarations

cout << “Give number of students: “; cin >> numStudents;

count = 1.0; // Count of student marks processed while (count <= numStudents) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

// Update sum, max, min count = count + 1;

}

average = sum/count;

// Print average, sum, min, max return 0;

}

sum = sum + marks;

if (count == 1) { min = marks; max = marks; } else {

min = (min > marks) ? marks: min;

max = (max < marks) ? marks: max;

}

(14)

IIT Bombay

C++ program with “while”

int main() {

int marks, sum = 0, min, max, numStudents;

float average, count; // Variable declarations

cout << “Give number of students: “; cin >> numStudents;

count = 1.0; // Count of student marks processed while (count <= numStudents) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

// Update sum, max, min count = count + 1;

}

average = sum/count;

// Print average, sum, min, max return 0;

}

(15)

IIT Bombay

Accumulation or Aggregation in Loops

int main() {

int marks, sum = 0, min, max, numStudents;

float average, count; // Variable declarations

cout << “Give number of students: “; cin >> numStudents;

count = 1.0; // Count of student marks processed while (count <= numStudents) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

// Update sum, max, min count = count + 1;

}

average = sum/count;

// Print average, sum, min, max return 0;

}

Inputs were provided one after another:

“streaming“ inputs

We did not remember all inputs seen so far, only some aggregates

Aggregates: “summary” of streaming inputs seen so far so that we can compute final result

Accumulation or aggregation: key to

programming with loops for streaming inputs

(16)

IIT Bombay

A Variant Of Our Problem

Read quiz 1 marks of CS101 students one at a time Stop reading if -1000 is entered as marks

Print number of marks entered, sum, average, maximum and minimum

• Difference from earlier version:

We do not know a priori how many marks will be entered Indicated by special end-of-inputs marks (-1000)

We’ll know when to stop only after reading -1000 as marks

(17)

IIT Bombay

Infinite loop !!!

Modifying Our Earlier C++ program

int main() {

int marks, sum = 0, min, max;

float average, count; // Variable declarations count = 1.0; // Count of student marks processed while (true) {

cout << “Give marks of student “ << count << “: “; cin >> marks;

if (marks == -1000) { … exit loop … } else { … Update sum, max, min … } count = count + 1;

}

average = sum/(count – 1);

// Print count – 1, average, sum, min, max return 0;

}

C++ provides an easy

way to do this

(18)

IIT Bombay

“break” Statement In “while” Loop

while (true) {

cout << “Give marks of student “ << count << “: “;

cin >> marks;

if (marks == -1000) { break; }

else { … Update sum, max, min … } count = count + 1;

}

Recall “break” from

“switch … case … “

(19)

IIT Bombay

Can We Do Without “break”?

bool exitFlag = false;

while (! exitFlag) {

cout << “Give marks of student “ << count << “: “;

cin >> marks;

if (marks == -1000) {exitFlag = true;}

else {

… Update sum, max, min … count = count + 1;

} }

Include within “else” block to preserve behaviour of

program with “break”

(20)

IIT Bombay

Convenience Of “break” In Loops

while (true) {

cout << “Give marks of student “ << count << “: “;

cin >> marks;

if (marks == -1000) { break; }

else { … Update sum, max, min … } count = count + 1;

}

“break” avoids such annoying

complications

(21)

IIT Bombay

Recap: “while” Statement in C++

Part of program before iteration

Iteration initialization (setting up initial values, etc)

while (loop condition)

{

Block of statements

Optional instructions to execute at end of every iteration }

Part of program after iteration

Block of statements (Body of “while” loop)

Part of program before execution

(22)

IIT Bombay

“do … while …” Statement in C++

Part of program before iteration

Iteration initialization (setting up initial values, etc)

do

{

Block of statements

Optional instructions to execute at end of every iteration } while (loop condition)

Part of program after iteration

Block of statements (Body of “do-while” loop)

Part of program before execution

(23)

IIT Bombay

“do … while …” Statement Flowchart

Part of program before iteration

Logical expression (loop condition)

Loop Body Part of program after iteration

FALSE

TRUE

Loop Body

(24)

IIT Bombay

From “while …” to “do … while …”

while (loop condition) { Loop Body

}

if (loop condition) { do {

Loop Body

} while (loop condition);

}

(25)

IIT Bombay

From “do … while …” to “while …”

Loop Body;

while (loop condition) { Loop Body

}

do {

Loop Body

} while (loop condition);

“break” statements can be used in “do … while …”

in same manner as in “while …”

(26)

IIT Bombay

“do … while …” vs “while …”

• Almost the same

• Prefer “do … while …” when we are guaranteed to execute loop body at least once

• Prefer “while …” if loop body may not be executed at all

• Programmer’s choice

(27)

IIT Bombay

Summary

• “while” statement in C++

• “do … while …” statement in C++

• Use of “break” statements

References

Related documents

Read in marks of the 100 students in a class, given in roll number order, 1 to 100.. After that, students may arrive in any order, and give their

• We want to store quiz 1 and quiz 2 marks of CS101 students in an encoded form. So that others cannot figure out the

Write a program to read in a list of students from the file database.txt and answer queries of the following type:. h 3 : Print all students in

Display all roll numbers who got highest marks // marks defined and read into as before. We can know the maximum marks only after seeing all

The English Language Proficiency Course (ELPC) aims to enable students to improve their ability to speak, read, listen and write in English.. General Objectives: The

The total marks of achievement earned by the students in the entire academic session in (i) Games and Sports (ii) Cultural &amp; Literary Activities will be sent,

The total marks of achievement earned by the students in the entire academic session in (i) Games and Sports (ii) Cultural &amp; Literary Activities will be sent,

The total marks of achievement earned by the students in the entire academic session in (i) Games and Sports (ii) Cultural &amp; Literary Activities will be sent,