• No results found

Department of Computer Science and Engineering IIT Bombay

N/A
N/A
Protected

Academic year: 2022

Share "Department of Computer Science and Engineering IIT Bombay"

Copied!
26
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: “for” statement in C++

(2)

IIT Bombay

• Iteration idioms in programming

• Necessity and convenience of iteration

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

Quick Recap of Relevant Topics

(3)

IIT Bombay

• Iteration using “for ….” statement in C++

• “break” statement in “for” loops

• Comparison of different iteration constructs in C++

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

“for …” Statement in C++

Part of program before iteration

for (iteration initialization ; loop condition ;

instructions to execute at end of every iteration) {

Block of statements (“for” loop body ) }

Part of program after iteration

Semi-colons not to denote end of executable statements, but to separate three parts inside “for ( ….. )”

Note absence of

semi-colon

(6)

IIT Bombay

Flowchart Representation of “for”

Part of program before iteration

Logical expression (loop condition)

“for” loop body Part of program after iteration

FALSE

TRUE

Iteration Initialization

Instructions at end of every

iteration

(7)

IIT Bombay

Points to Remember About “for”

for (initialization; loop condition; instructions after every iteration) { “for” loop body }

• Initialization code executed only once before first entry in loop

• Loop condition checked before executing “for” body Can lead to zero executions of “for” body

• Number of times loop condition is checked =

Number of times “for” body executed + 1, if loop terminates

• Loop condition can be changed in “for” body or in “instructions

after every iteration”

(8)

IIT Bombay

Revisiting The Quiz Marks 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)

count <= numStudents?

Ask for marks Read marks

Update aggregates

Compute average Print aggregates

FALSE

TRUE

Initialize count to 1

Increment

count

(10)

IIT Bombay

C++ Program with “for”

int main() {

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

float average, count; // Variable declarations

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

for (count = 0.0; count <= numStudents; count = count + 1) { cout << “Give marks of student “ << count << “: “;

cin >> marks;

// Update sum, max, min }

average = sum/count;

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

}

(11)

IIT Bombay

C++ Program with “for”

int main() {

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

float average, count; // Variable declarations

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

for (count = 0.0; count <= numStudents; count = count + 1) { cout << “Give marks of student “ << count << “: “;

cin >> marks;

// Update sum, max, min }

average = sum/count;

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

}

(12)

IIT Bombay

C++ Program with “for”

int main() {

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

float average, count; // Variable declarations

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

for (count = 1.0; count <= numStudents; count = count + 1) { cout << “Give marks of student “ << count << “: “;

cin >> marks;

// Update sum, max, min }

average = sum/count;

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

}

(13)

IIT Bombay

C++ Program with “for”

int main() {

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

float average, count; // Variable declarations

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

for (count = 1.0; count <= numStudents; count = count + 1) { cout << “Give marks of student “ << count << “: “;

cin >> marks;

// Update sum, max, min }

average = sum/count;

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

}

Initialization code Loop condition Instruction to execute after every iteration

“for” loop body

(14)

IIT Bombay

C++ Program with “for”

int main() {

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

float average, count; // Variable declarations

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

for (count = 1.0; count <= numStudents; count = count + 1) { cout << “Give marks of student “ << count << “: “;

cin >> marks;

// Update sum, max, min }

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;

}

(15)

IIT Bombay

C++ Program with “for”

int main() {

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

float average, count; // Variable declarations

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

for (count = 1.0; count <= numStudents; count = count + 1) { cout << “Give marks of student “ << count << “: “;

cin >> marks;

// Update sum, max, min }

average = sum/count;

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

}

(16)

IIT Bombay

“break” Statement in “for” Loops

• Behaviour same as in “while” and “do … while …” loops

Jump out of the loop immediately on executing “break”

Recall our variant of the quiz 1 marks 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

(17)

IIT Bombay

Our C++ Program with “break”

int main() {

int marks, sum = 0, min, max;

float average, count; // Variable declarations for (count = 1.0; true ; count = count + 1)

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

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

else { … Update sum, min, max … } }

average = sum/(count – 1);

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

}

Jump out of “for” loop on executing “break”

Infinite loop !!!

(18)

IIT Bombay

Our C++ Program with “break”

int main() {

int marks, sum = 0, min, max;

float average, count; // Variable declarations for (count = 1.0; true ; count = count + 1)

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

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

else { … Update sum, min, max … } }

average = sum/(count – 1);

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

}

Jump out of “for” loop on executing “break”

Next statement executed after “break”

These instructions skipped

after executing ”break”

(19)

IIT Bombay

“for” Loops with Empty Parts

int main() {

int marks, sum = 0, min, max;

float average, count; // Variable declarations for (count = 1.0; true ; count = count + 1)

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

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

else { … Update sum, min, max … } }

average = sum/(count – 1);

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

}

Skipping loop condition in “for”

equivalent to “true” loop condition

(20)

IIT Bombay

“for” Loops with Empty Parts

for ( count = 1.0 ; true ; count = count + 1)

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

cin >> marks;

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

else { … Update sum, min, max … } }

count = 1.0 true count = count + 1

;

;

(21)

IIT Bombay

From “for …” to “while …”

for (initialization;

loop condition;

instrAfterEveryIteration) {

“for” Loop Body }

Initialization ;

while (loop condition) {

“for” Loop Body;

instrAfterEveryIteration;

}

(22)

IIT Bombay

From “while …” to “for …”

for ( ; loop condition; ) {

“while”Loop Body }

while (loop condition) {

“while” Loop Body }

“for … ” to “do … while …” , and “do … while …” to “for …”

can be done by

recalling the transformation of “while …” to and from “do … while …”

(23)

IIT Bombay

“for … “ vs “while …”

for (count = 1.0;

count <= numStudents;

count = count + 1) {

// Process marks }

count = 1.0;

while (count <= numStudents) {

// Process marks count = count + 1;

}

Real computation we want to do

“Book-keeping” cleanly isolated

(24)

IIT Bombay

“for … “ vs “while …”

for (count = 1.0;

count <= numStudents;

count = count + 1) {

// Process marks }

count = 1.0;

while (count <= numStudents) {

// Process marks count = count + 1;

}

Real computation we want to do “Book-keeping” mixed up “Book-keeping” mixed up

(25)

IIT Bombay

“for …” vs “while …”

• Often a programmer’s choice dependent on the context

• In general, a good idea to separate book-keeping from real computation

• Prefer “for …” loops

• However, loop condition may not simply be based on book- keeping

• Real computation in loop body may determine loop condition

• Prefer “while …” loops

(26)

IIT Bombay

Summary

• “for …” statement in C++

• Variants of “for …” statement

• Use of “break” statement in “for …” statements

• Comparison with “while …” and “do … while …” statements

References

Related documents

Rhushabh Goradia and Piyush PorwalComputer Science and Engineering IIT Bombay rhushabh@cse.iitb.ac.in, porwalpiyush@cse.iitb.ac.in... Outline

Memory locations accessed: local variables/arrays of functions Statically allocated in stack segment when function is called.. Quick Recap of

Choice of comparison operator crucially determines sorting order (increasing/decreasing), and also how equal elements

• Decide which half of array to recurse on based on output of comparison

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

• Recall how we accessed member data values of structures V3 p, *ptrP;. cin

• Uses dynamically allocated array to store elements Array can grow or shrink in size. • Dynamic memory management