Fundamental Programming for Loops Fundamental Programming 310201 Repetition

  • Slides: 46
Download presentation
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

Repetition Statements we have now used two C++ repetition statement – while and do-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 is

Repetition Statements in this class we introduce one more repetition statement - 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 >>

for vs while cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; Nbr. Loops = 0; while (Nbr. Loops < Nbr. Students) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; Nbr. Loops = Nbr. Loops +1; } notes: initialise loop control variable Fundamental Programming 310201

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

for vs while cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; Nbr. Loops = 0; while (Nbr. Loops < Nbr. Students) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; Nbr. Loops = Nbr. Loops +1; } notes: loop condition Fundamental Programming 310201

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

for vs while cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; Nbr. Loops = 0; while (Nbr. Loops < Nbr. Students) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; Nbr. Loops++; } notes: modify loop control variable (to avoid looping forever) Fundamental Programming 310201

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

for vs while cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; for (Nbr. Loops = 0; Nbr. Loops < Nbr. Students; Nbr. Loops++) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; } notes: initialise loop control variable Fundamental Programming 310201

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

for vs while cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; for (Nbr. Loops = 0; Nbr. Loops < Nbr. Students; Nbr. Loops++) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; } notes: loop condition Fundamental Programming 310201

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

for vs while cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; for (Nbr. Loops = 0; Nbr. Loops < Nbr. Students; Nbr. Loops++) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; } notes: modify loop control variable (to avoid looping forever) Fundamental Programming 310201

for Loop Syntax for ( < loop initialisation statement > ; < loop condition

for Loop Syntax for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ) { < for statements > } notes: parentheses around for clause Fundamental Programming 310201

for Loop Syntax for ( < loop initialisation statement > ; < loop condition

for Loop Syntax for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ) { < for statements > } notes: statement performed once before entering loop for first time Fundamental Programming 310201

for Loop Syntax for ( < loop initialisation statement > ; < loop condition

for Loop Syntax for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ) { < for statements > } notes: semi-colons after loop initialisation and loop condition Fundamental Programming 310201

for Loop Syntax for ( < loop initialisation statement > ; < loop condition

for Loop Syntax for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ) { < for statements > } notes: condition tested at the start of each loop – including the very first loop Fundamental Programming 310201

for Loop Syntax for ( < loop initialisation statement > ; < loop condition

for Loop Syntax for ( < loop initialisation statement > ; < loop condition > ; < loop completion statement > ) { < for statements > } notes: statement performed at the end of each loop Fundamental Programming 310201

for Loop Operation for loop initialise statement loop condition false true loop statements loop

for Loop Operation for loop initialise statement loop condition false true loop statements loop completion statement Fundamental Programming 310201

Activity what output is produced by the following code: for ( int Counter =

Activity what output is produced by the following code: for ( int Counter = 0; Counter < 5; Counter++ ) { cout << “Counter = “ << Counter << endl; } Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Feedback the output produced by this code is: Counter = 0 Counter =

Activity Feedback the output produced by this code is: Counter = 0 Counter = 1 Counter = 2 Counter = 3 Counter = 4 Fundamental Programming 310201

Loop Control Variables usually an integer, but can be a character can be declared

Loop Control Variables usually an integer, but can be a character can be declared within the for clause – instead of. . . Fundamental Programming 310201

Loop Control Variables int Nbr. Loops; cout << “Number of marks in exam ==>

Loop Control Variables int Nbr. Loops; cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; for (Nbr. Loops = 0; Nbr. Loops < Nbr. Students; Nbr. Loops++) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; } Fundamental Programming 310201

Loop Control Variables cout << “Number of marks in exam ==> “; cin >>

Loop Control Variables cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; for (int Nbr. Loops = 0; Nbr. Loops < Nbr. Students; Nbr. Loops++) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; } Fundamental Programming 310201

Loop Control Variables int Nbr. Loops; cout << “Number of marks in exam ==>

Loop Control Variables int Nbr. Loops; cout << “Number of marks in exam ==> “; cin >> Nbr. Marks; cout >> “Number of students ==> “ cin >> Nbr. Students; for (int Nbr. Loops = 0; Nbr. Loops < Nbr. Students; Nbr. Loops++) { cout << “Student’s mark ==> “; cin >> Student. Mark; Percentage = 100 * Student. Mark / Nbr. Marks; cout << “ Student’s percentage: “; cout << Percentage; } 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

Loop Control Variables loop control variables are not normally modified within the loop it’s a common source of error… Fundamental Programming 310201

Activity what output is produced by the following code: for ( int Counter =

Activity what output is produced by the following code: for ( int Counter = 0; Counter != 5; Counter++ ) { cout << “Counter = “ << Counter << endl; Counter = Counter + 1; } Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Feedback the output produced by this code is: Counter = 0 Counter =

Activity Feedback the output produced by this code is: Counter = 0 Counter = 2 Counter = 4 Counter = 6 Counter = 8 : ( until you terminate the program! ) Fundamental Programming 310201

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

Activity write the for clause of a for loop to produce this output: Counter = 15 Counter = 14 Counter = 13 Counter = 12 Counter = 11 Counter = 10 Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Break Fundamental Programming 310201

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

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 Loops we saw that an if-else statement can be embedded inside another if-else

Nested Loops we 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 (ifelse, 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

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 Row. Nbr = 1; Row. Nbr <= 5; Row.

Nested Loops for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++ ) { cout << endl << “Row “ << Row. Nbr << “: “; for ( int Col. Nbr = 1; Col. Nbr <= 3; Col. Nbr++ ) { cout << " Col " << Col. Nbr ; } } inner loop Fundamental Programming 310201

Activity for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++

Activity for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++ ) { cout << endl << “Row “ << Row. Nbr << “: “; for ( int Col. Nbr = 1; Col. Nbr <= 3; Col. Nbr++ ) { cout << " Col " << Col. Nbr ; } } activity: what does this code produce as output Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Feedback for ( int Row. Nbr = 1; Row. Nbr <= 5; Row.

Activity Feedback for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++ ) { cout << endl << “Row “ << Row. Nbr << “: “; for ( int Col. Nbr = 1; Col. Nbr <= 3; Col. Nbr++ ) { cout << " Col " << Col. Nbr ; } } feedback: this code produces the following output… Col 1 Col 2 Col 3 Fundamental Programming 310201

Nested Loops for ( int Row. Nbr = 1; Row. Nbr <= 5; Row.

Nested Loops for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++ ) { cout << endl << “Row “ << Row. Nbr << “: “; for ( int Col. Nbr = 1; Col. Nbr <= 3; Col. Nbr++ ) { cout << " Col " << Col. Nbr ; } } inner loop outer loop Fundamental Programming 310201

Nested Loops for ( int Row. Nbr = 1; Row. Nbr <= 5; Row.

Nested Loops for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++ ) { cout << endl << “Row “ << Row. Nbr << “: “; for ( int Col. Nbr = 1; Col. Nbr <= 3; Col. Nbr++ ) { cout << " Col " << Col. Nbr ; } } note: here, three trips around the inner loop are performed for each trip around the outer loop Fundamental Programming 310201

Activity for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++

Activity for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++ ) { cout << endl << “Row “ << Row. Nbr << “: “; for ( int Col. Nbr = 1; Col. Nbr <= 3; Col. Nbr++ ) { cout << " Col " << Col. Nbr ; } } activity: what is the output produced by this code? Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Break Fundamental Programming 310201

Activity Feedback for ( int Row. Nbr = 1; Row. Nbr <= 4; Row.

Activity Feedback for ( int Row. Nbr = 1; Row. Nbr <= 4; Row. Nbr++ ) { cout << endl << “Row “ << Row. Nbr << “: “; for ( int Col. Nbr = 1; Col. Nbr <= 3; Col. Nbr++ ) { cout << " Col " << Col. Nbr ; } } feedback: the output produced by this code is Row 1: Col 1 Col 2 Col 3 Row 2: Col 1 Col 2 Col 3 Row 3: Col 1 Col 2 Col 3 Row 4: Col 1 Col 2 Col 3 Fundamental Programming 310201

Looping Examples Chapter 7 of the textbook provides many examples of looping there also

Looping Examples Chapter 7 of the textbook provides many examples of looping there 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 ++Row. Nbr instead of Row. Nbr++ in

Increment and Decrement the textbook would use ++Row. Nbr instead of Row. Nbr++ in a for loop for ( int Row. Nbr = 1; Row. Nbr <= 5; ++Row. Nbr ) the above for clause has the exact same effect as the one below for ( int Row. Nbr = 1; Row. Nbr <= 5; Row. Nbr++ ) Fundamental Programming 310201

Increment and Decrement the difference between ++Row. Nbr and Row. Nbr++ is quite subtle

Increment and Decrement the difference between ++Row. Nbr and Row. Nbr++ is quite subtle – it can be seen from: Row. Nbr = 1; cout << Row. Nbr++ << endl; cout << Row. Nbr; 1 2 Row. Nbr = 1; cout << ++Row. Nbr << endl; cout << Row. Nbr; 2 2 output Fundamental Programming 310201

Increment and Decrement recall the syntax of cout: cout << <expression>; below, the expression

Increment and Decrement recall the syntax of cout: cout << <expression>; below, the expression is simply the value of variable Row. Nbr cout << Row. Nbr; below, the value of Row. Nbr is incremented after it’s value is used in the expression Row. Nbr = 1; cout << Row. Nbr++ << endl; 1 output Fundamental Programming 310201

Increment and Decrement below, the value of Row. Nbr is incremented before it’s value

Increment and Decrement below, the value of Row. Nbr is incremented before it’s value is used in the expression Row. Nbr = 1; cout << ++Row. Nbr << endl; 2 output the statements below have exactly the same effect – they simply increment the value of Row. Nbr++; ++Row. Nbr; Fundamental Programming 310201

Summary the for loop is a convenience, it’s not needed anything you can do

Summary the 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 Fundamental Programming 310201