Fundamental Programming 310201 Fundamental Programming for Loops.

46
Fundamental Programming 310201 Fundamental Programming for Loops

Transcript of Fundamental Programming 310201 Fundamental Programming for Loops.

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

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…

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

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

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)

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

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

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)

Fundamental Programming 310201

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

< for statements >}

for Loop Syntax

notes: parentheses around for clause

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

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

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

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

Fundamental Programming 310201

for Loop Operation

for

loopstatements

loopconditio

n

false

true

loopinitialisestateme

nt

loopcompletio

nstatemen

t

Fundamental Programming 310201

Activity

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

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

what output is produced by the following code:

Fundamental Programming 310201

Activity Break

Fundamental Programming 310201

Activity Feedback

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

the output produced by this code is:

Fundamental Programming 310201

Loop Control Variables

usually an integer, but can be a character

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

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

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

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

Fundamental Programming 310201

Loop Control Variables

loop control variables are not normally modified within the loop

it’s a common source of error…

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:

Fundamental Programming 310201

Activity Break

Fundamental Programming 310201

Activity Feedback

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

the output produced by this code is:

Fundamental Programming 310201

Activity

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

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

Fundamental Programming 310201

Activity Break

Fundamental Programming 310201

Activity Feedback

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

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

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

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...

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

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

Fundamental Programming 310201

Activity Break

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

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

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

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?

Fundamental Programming 310201

Activity Break

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

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

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++ )

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

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

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

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