Top Banner
Fundamental Programming 310201 Fundamental Programming for Loops
46
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: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Fundamental Programming

for Loops

Page 2: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Repetition Statements

we have now used two C++ repetition statement – while and do-while

while tests the loop condition at the start of the loop – allowing for the possibility that we may not need to perform the loop

do-while tests the loop condition at the end of the loop – useful when we need to perform a loop at least once

Page 3: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Repetition Statements

in this class we introduce one more repetition statement - for

for is just a convenience, you don’t need it - anything you can do with for, you can do with while

for is convenient when you know in advance how many times you need to perform a loop – instead of…

Page 4: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for vs while cout << “Number of marks in exam ==> “;

cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0;while (NbrLoops < NbrStudents){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;NbrLoops = NbrLoops +1;

}

notes: initialise loop control variable

Page 5: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for vs while cout << “Number of marks in exam ==> “;

cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0;while (NbrLoops < NbrStudents){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;NbrLoops = NbrLoops +1;

}

notes: loop condition

Page 6: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for vs while cout << “Number of marks in exam ==> “;

cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0;while (NbrLoops < NbrStudents){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;NbrLoops++;

}

notes: modify loop control variable (to avoid looping forever)

Page 7: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

cout << “Number of marks in exam ==> “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;

}

for vs while

notes: initialise loop control variable

Page 8: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

cout << “Number of marks in exam ==> “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;

}

for vs while

notes: loop condition

Page 9: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

cout << “Number of marks in exam ==> “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;

}

for vs while

notes: modify loop control variable (to avoid looping forever)

Page 10: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ){

< for statements >}

for Loop Syntax

notes: parentheses around for clause

Page 11: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ){

< for statements >}

for Loop Syntax

notes: statement performed once before entering loop for first time

Page 12: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ){

< for statements >}

for Loop Syntax

notes: semi-colons after loop initialisation and loop condition

Page 13: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ){

< for statements >}

for Loop Syntax

notes: condition tested at the start of each loop – including the very first loop

Page 14: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ){

< for statements >}

for Loop Syntax

notes: statement performed at the end of each loop

Page 15: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

for Loop Operation

for

loopstatements

loopconditio

n

false

true

loopinitialisestateme

nt

loopcompletio

nstatemen

t

Page 16: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity

for ( int Counter = 0; Counter < 5; Counter++ ) {

cout << “Counter = “ << Counter << endl;}

what output is produced by the following code:

Page 17: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Break

Page 18: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Feedback

Counter = 0Counter = 1Counter = 2Counter = 3Counter = 4

the output produced by this code is:

Page 19: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Loop Control Variables

usually an integer, but can be a character

can be declared within the for clause – instead of...

Page 20: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

int NbrLoops;cout << “Number of marks in exam ==> “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;

}

Loop Control Variables

Page 21: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

cout << “Number of marks in exam ==> “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (int NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;

}

Loop Control Variables

Page 22: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

int NbrLoops;cout << “Number of marks in exam ==> “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (int NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++){

cout << “Student’s mark ==> “;cin >> StudentMark;Percentage = 100 * StudentMark / NbrMarks;cout << “ Student’s percentage: “;cout << Percentage;

}

Loop Control Variables

notes: if you try to declare the same variable twice, you get a compilation error

Page 23: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Loop Control Variables

loop control variables are not normally modified within the loop

it’s a common source of error…

Page 24: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity

for ( int Counter = 0; Counter != 5; Counter++ ) {

cout << “Counter = “ << Counter << endl; Counter = Counter + 1;}

what output is produced by the following code:

Page 25: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Break

Page 26: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Feedback

Counter = 0Counter = 2Counter = 4Counter = 6Counter = 8:( until you terminate the program! )

the output produced by this code is:

Page 27: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity

write the for clause of a for loop to produce this output:

Counter = 15Counter = 14Counter = 13Counter = 12Counter = 11Counter = 10

Page 28: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Break

Page 29: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Feedback

the for clause of a for loop to produce this output is:

for ( int Counter = 15; Counter >= 10; Counter-- )

Page 30: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Nested Loopswe saw that an if-else statement can be embedded inside another if-else statement

likewise, a for statement can be nested inside another for statement

note: in fact, any selection statement (if-else, switch) or repetition statement (while, do-while, for) can be embedded inside another selection or repetition statement

Page 31: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Nested Loops

with a nested loop, one performs one or more trips around the inner loop for each trip around the outer loop

here’s an example...

Page 32: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Nested Loops

for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ){ cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; }}

inner loop

Page 33: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity

for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ){ cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; }}

activity: what does this code produce as output

Page 34: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Break

Page 35: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Feedback

for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ){ cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; }}

feedback: this code produces the following output… Col 1 Col 2 Col3

Page 36: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Nested Loops

for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ){ cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; }}

outer loop

inner loop

Page 37: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Nested Loops

for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ){ cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; }}

note: here, three trips around the inner loop are performed for each trip around the outer loop

Page 38: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity

for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ ){ cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; }}

activity: what is the output produced by this code?

Page 39: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Break

Page 40: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Activity Feedback

for ( int RowNbr = 1; RowNbr <= 4; RowNbr++ ){ cout << endl << “Row “ << RowNbr << “:“; for ( int ColNbr = 1; ColNbr <= 3; ColNbr++ ) { cout << " Col " << ColNbr ; }}

feedback: the output produced by this code isRow 1: Col 1 Col 2 Col 3 Row 2: Col 1 Col 2 Col 3 Row 3: Col 1 Col 2 Col 3Row 4: Col 1 Col 2 Col 3

Page 41: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Looping Examples

Chapter 7 of the textbook provides many examples of looping

there are also many examples in the Study Guide

use tracing to check that you understand the mechanics of looping statements

Page 42: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Increment and Decrement

the textbook would use ++RowNbr instead of RowNbr++ in a for loopfor ( int RowNbr = 1; RowNbr <= 5; ++RowNbr )

the above for clause has the exact same effect as the one below for ( int RowNbr = 1; RowNbr <= 5; RowNbr++ )

Page 43: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Increment and Decrement

the difference between ++RowNbr and RowNbr++ is quite subtle – it can be seen from:RowNbr = 1;cout << RowNbr++ << endl;cout << RowNbr;

RowNbr = 1;cout << ++RowNbr << endl;cout << RowNbr;

12

22

output

Page 44: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Increment and Decrementrecall the syntax of cout:cout << <expression>;

below, the expression is simply the value of variable RowNbr cout << RowNbr;

below, the value of RowNbr is incremented after it’s value is used in the expression

RowNbr = 1;cout << RowNbr++ << endl;

1 output

Page 45: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Increment and Decrementbelow, the value of RowNbr is incremented before it’s value is used in the expression

RowNbr = 1;cout << ++RowNbr << endl;

the statements below have exactly the same effect – they simply increment the value of RowNbrRowNbr++;

++RowNbr;

2 output

Page 46: Fundamental Programming 310201 Fundamental Programming for Loops.

Fundamental Programming 310201

Summarythe for loop is a convenience, it’s not needed - anything you can do with a for loop you can do with a while loop

the for loop is useful when the number of required trips around a loop is known before entering the loop

consequently, the for loop is useful when using arrays – the topic of a future class