Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer...

26
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: “for” statement in C++ 1 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Transcript of Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer...

Page 1: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

Computer ProgrammingDr. Deepak B Phatak

Dr. Supratik ChakrabortyDepartment of Computer Science and Engineering

IIT Bombay

Session: “for” statement in C++

1Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Page 2: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

• Iteration idioms in programming

• Necessity and convenience of iteration

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

2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics

Page 3: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

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

• “break” statement in “for” loops

• Comparison of different iteration constructs in C++

3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture

Page 4: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 4

Loop Condition

Loop

Loop Body

Page 5: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 5

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

Note absence of semi-colon

Page 6: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

Flowchart Representation of “for”

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6

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

Page 7: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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”

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7

Page 8: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8

Page 9: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

Flowchart Representation

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9

Read numStudentsInitialize aggregates (sum, min, max)

count <= numStudents?

Ask for marksRead marks

Update aggregates

Compute averagePrint aggregates

FALSE

TRUE

Initialize count to 1

Increment count

Page 10: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10

Page 11: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11

Page 12: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12

Page 13: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 13

Initialization code Loop condition Instruction to execute after

every iteration

“for” loop body

Page 14: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14

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

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

}

Page 15: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 15

Page 16: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 16

Page 17: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

Our C++ Program with “break”

int main() {int marks, sum = 0, min, max;float average, count; // Variable declarationsfor (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, maxreturn 0;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 17

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

Infinite loop !!!

Page 18: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

Our C++ Program with “break”

int main() {int marks, sum = 0, min, max;float average, count; // Variable declarationsfor (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, maxreturn 0;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 18

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

Next statement executed after “break”

These instructions skipped after executing ”break”

Page 19: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

“for” Loops with Empty Parts

int main() {int marks, sum = 0, min, max;float average, count; // Variable declarationsfor (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, maxreturn 0;

} Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 19

Skipping loop condition in “for” equivalent to “true” loop condition

Page 20: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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 … }

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 20

count = 1.0 count = count + 1true

;

;

Page 21: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

From “for …” to “while …”

for (initialization;

loop condition;

instrAfterEveryIteration) {

“for” Loop Body

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 21

Initialization ;

while (loop condition)

{

“for” Loop Body;

instrAfterEveryIteration;

}

Page 22: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

From “while …” to “for …”

for ( ; loop condition; )

{

“while”Loop Body

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 22

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 …”

Page 23: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

“for … “ vs “while …”

for (count = 1.0;

count <= numStudents;

count = count + 1)

{

// Process marks

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 23

count = 1.0;

while (count <= numStudents)

{

// Process marks

count = count + 1;

}

Real computation we want to do

“Book-keeping” cleanly isolated

Page 24: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

IIT Bombay

“for … “ vs “while …”

for (count = 1.0;

count <= numStudents;

count = count + 1)

{

// Process marks

}

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 24

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

Page 25: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 25

Page 26: Computer Programming - CSE, IIT Bombaycs101/2014.2/lecture-slides/CS101x_S... · Computer Programming Dr. Deepak B Phatak ... Flowchart Representation of “for ... a good idea to

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

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 26