Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Programming 310201 Fundamental Programming for Loops.

Similar presentations


Presentation on theme: "Fundamental Programming 310201 Fundamental Programming for Loops."— Presentation transcript:

1 Fundamental Programming 310201 Fundamental Programming for Loops

2 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

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

4 Fundamental Programming 310201 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

5 Fundamental Programming 310201 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

6 Fundamental Programming 310201 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)

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

8 Fundamental Programming 310201 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

9 Fundamental Programming 310201 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)

10 Fundamental Programming 310201 for ( ; ; ) { } for Loop Syntax notes: parentheses around for clause

11 Fundamental Programming 310201 for ( ; ; ) { } for Loop Syntax notes: statement performed once before entering loop for first time

12 Fundamental Programming 310201 for ( ; ; ) { } for Loop Syntax notes: semi-colons after loop initialisation and loop condition

13 Fundamental Programming 310201 for ( ; ; ) { } for Loop Syntax notes: condition tested at the start of each loop – including the very first loop

14 Fundamental Programming 310201 for ( ; ; ) { } for Loop Syntax notes: statement performed at the end of each loop

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

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

17 Fundamental Programming 310201 Activity Break

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

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

20 Fundamental Programming 310201 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

21 Fundamental Programming 310201 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

22 Fundamental Programming 310201 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

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

24 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:

25 Fundamental Programming 310201 Activity Break

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

27 Fundamental Programming 310201 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

28 Fundamental Programming 310201 Activity Break

29 Fundamental Programming 310201 Activity Feedback the for clause of a for loop to produce this output is: for ( int Counter = 15; Counter >= 10; Counter-- )

30 Fundamental Programming 310201 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

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

32 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

33 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

34 Fundamental Programming 310201 Activity Break

35 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

36 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

37 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

38 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?

39 Fundamental Programming 310201 Activity Break

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

41 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

42 Fundamental Programming 310201 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++ )

43 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; 1212 2222 output

44 Fundamental Programming 310201 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

45 Fundamental Programming 310201 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

46 Fundamental Programming 310201 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


Download ppt "Fundamental Programming 310201 Fundamental Programming for Loops."

Similar presentations


Ads by Google