Fundamental Programming Fundamental Programming for Loops
Fundamental Programming 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 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 for vs while cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0; while (NbrLoops < NbrStudents) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; NbrLoops = NbrLoops +1; } notes: initialise loop control variable
Fundamental Programming for vs while cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0; while (NbrLoops < NbrStudents) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; NbrLoops = NbrLoops +1; } notes: loop condition
Fundamental Programming for vs while cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; NbrLoops = 0; while (NbrLoops < NbrStudents) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; NbrLoops++; } notes: modify loop control variable (to avoid looping forever)
Fundamental Programming cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } for vs while notes: initialise loop control variable
Fundamental Programming cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } for vs while notes: loop condition
Fundamental Programming cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; 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 for ( ; ; ) { } for Loop Syntax notes: parentheses around for clause
Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: statement performed once before entering loop for first time
Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: semi-colons after loop initialisation and loop condition
Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: condition tested at the start of each loop – including the very first loop
Fundamental Programming for ( ; ; ) { } for Loop Syntax notes: statement performed at the end of each loop
Fundamental Programming for Loop Operation for loop statements loop condition false true loop initialise statement loop completion statement
Fundamental Programming Activity for ( int Counter = 0; Counter < 5; Counter++ ) { cout << “Counter = “ << Counter << endl; } what output is produced by the following code:
Fundamental Programming Activity Break
Fundamental Programming Activity Feedback Counter = 0 Counter = 1 Counter = 2 Counter = 3 Counter = 4 the output produced by this code is:
Fundamental Programming Loop Control Variables usually an integer, but can be a character can be declared within the for clause – instead of...
Fundamental Programming int NbrLoops; cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } Loop Control Variables
Fundamental Programming cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (int NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; cin >> StudentMark; Percentage = 100 * StudentMark / NbrMarks; cout << “ Student’s percentage: “; cout << Percentage; } Loop Control Variables
Fundamental Programming int NbrLoops; cout “; cin >> NbrMarks; cout >> “Number of students ==> “ cin >> NbrStudents; for (int NbrLoops = 0; NbrLoops < NbrStudents; NbrLoops++) { cout “; 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 Loop Control Variables loop control variables are not normally modified within the loop it’s a common source of error…
Fundamental Programming 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 Activity Break
Fundamental Programming Activity Feedback Counter = 0 Counter = 2 Counter = 4 Counter = 6 Counter = 8 : ( until you terminate the program! ) the output produced by this code is:
Fundamental Programming 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 Activity Break
Fundamental Programming Activity Feedback the for clause of a for loop to produce this output is: for ( int Counter = 15; Counter >= 10; Counter-- )
Fundamental Programming 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 (if- else, switch) or repetition statement (while, do-while, for) can be embedded inside another selection or repetition statement
Fundamental Programming 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 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 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 Activity Break
Fundamental Programming 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 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 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 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 Activity Break
Fundamental Programming 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 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 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 Increment and Decrement the textbook would use ++RowNbr instead of RowNbr++ in a for loop for ( 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 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; output
Fundamental Programming Increment and Decrement recall the syntax of cout: cout ; 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 Increment and Decrement below, 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 RowNbr RowNbr++; ++RowNbr; 2 output
Fundamental Programming 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