Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Loops.

Similar presentations


Presentation on theme: "Computer Science 1620 Loops."— Presentation transcript:

1 Computer Science 1620 Loops

2 Write a program to display a table of squares and cubes of the first 5 integers:
NUMBER SQUARE CUBE

3 Write a program to display a table of squares and cubes of the first 5 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; return 0; }

4

5 Write a program to display a table of squares and cubes of the first 5 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; return 0; }

6 Write a program to display a table of squares and cubes of the first 20 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; return 0; }

7 Write a program to display a table of squares and cubes of the first 50 integers:
#include <iostream> using namespace std; #include <iomanip> int main () cout << " " << endl; cout << "NUMBER SQUARE CUBE" << endl; { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; int num = 1; num++; return 0; }

8 Write a program to display a table of squares and cubes of the first n integers, where n is specified by the user (let n be of max 10): #include <iostream> #include <iomanip> using namespace std; int main () { int num = 0; int size; cout << "Please enter the number of squares and cubes to display: "; cin >> size; cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; if (num < size) { num++; cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; }

9 Write a program to display a table of squares and cubes of the first n integers, where n is specified by the user (let n be of max 10): if (num < size) { num++; cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; } return 0;

10 Issues 1) Repeated code 2) Non-scalable 3) Non user-friendly

11 While Loop while: while ( ) boolean expression C++ Statement
this statement will be executed repeatedly, for as long as this expression has value true note that the statement can be a compound statement

12 Rewrite our program to display a table of squares and cubes of the first 5 integers:
NUMBER SQUARE CUBE

13 Write a program to display a table of squares and cubes of the first 5 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; return 0; }

14 Write a program to display a table of squares and cubes of the first 5 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0;

15 Program: Program Memory: Output: #include <iostream>
#include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; Output:

16 Program: Program Memory: Output:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; cout << "NUMBER SQUARE CUBE" << endl; Output: NUMBER SQUARE CUBE

17 Program: Program Memory: Output:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; cout << " " << endl; Output: NUMBER SQUARE CUBE

18 Program: Program Memory: num 1 Output: int num = 1;
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 1 int num = 1; Output: NUMBER SQUARE CUBE

19 Program: Program Memory: num 1
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 1 while (num <= 5) { Since 1 <= 5, the expression is true, so the while statement is executed Output: NUMBER SQUARE CUBE

20 Program: Program Memory: num 1 Output: #include <iostream>
#include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 1 cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; Output: NUMBER SQUARE CUBE

21 We are now at the end of the while statement.
Program: Program Memory: #include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 2 1 num++; Output: We are now at the end of the while statement. We "loop" to the beginning again, testing to see if the expression is false. NUMBER SQUARE CUBE

22 Program: Program Memory: num 2
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 2 while (num <= 5) { Since 2 <= 5, the expression is true, so the while statement is executed Output: NUMBER SQUARE CUBE

23 Program: Program Memory: num 2 Output: #include <iostream>
#include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 2 cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; Output: NUMBER SQUARE CUBE

24 We are now at the end of the while statement.
Program: Program Memory: #include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 2 3 num++; Output: We are now at the end of the while statement. We "loop" to the beginning again, testing to see if the expression is false. NUMBER SQUARE CUBE

25 Program: Program Memory: num 3
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 3 while (num <= 5) { Since 3 <= 5, the expression is true, so the while statement is executed Output: NUMBER SQUARE CUBE

26 Program: Program Memory: num 3 Output: #include <iostream>
#include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 3 cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; Output: NUMBER SQUARE CUBE

27 We are now at the end of the while statement.
Program: Program Memory: #include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 3 4 num++; Output: We are now at the end of the while statement. We "loop" to the beginning again, testing to see if the expression is false. NUMBER SQUARE CUBE

28 Program: Program Memory: num 4
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 4 while (num <= 5) { Since 4 <= 5, the expression is true, so the while statement is executed Output: NUMBER SQUARE CUBE

29 Program: Program Memory: num 4 Output: #include <iostream>
#include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 4 cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; Output: NUMBER SQUARE CUBE

30 We are now at the end of the while statement.
Program: Program Memory: #include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 5 4 num++; Output: We are now at the end of the while statement. We "loop" to the beginning again, testing to see if the expression is false. NUMBER SQUARE CUBE

31 Program: Program Memory: num 5
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 5 while (num <= 5) { Since 5 <= 5, the expression is true, so the while statement is executed Output: NUMBER SQUARE CUBE

32 Program: Program Memory: num 5 Output: #include <iostream>
#include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 5 cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; Output: NUMBER SQUARE CUBE

33 We are now at the end of the while statement.
Program: Program Memory: #include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 6 5 num++; Output: We are now at the end of the while statement. We "loop" to the beginning again, testing to see if the expression is false. NUMBER SQUARE CUBE

34 Program: Program Memory: num 6
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 6 while (num <= 5) { Since 6 <= 5 is false, the while loop statement is not executed, but skipped (just like with an if) Output: NUMBER SQUARE CUBE

35 Program: Program Memory: num 6 Output: return 0;
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; num 6 return 0; Output: NUMBER SQUARE CUBE

36

37 Modify the program to print out the results for the first 20 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0;

38 Modify the program to print out the results for the first 20 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 20) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0;

39 Modify the program to print out the results for the first 10000 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 10000) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0;

40 Modify the program to print out the results for the first 10000 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 10000) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0;

41 Modify the program to print out the results for the first n integers, where n is specified by the user: #include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 10000) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0;

42 Modify the program to print out the results for the first n integers, where n is specified by the user: #include <iostream> #include <iomanip> using namespace std; int main () { int n; cout << "Please enter the number of squares and cubes to display: "; cin >> n; cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 10000) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0;

43 Modify the program to print out the results for the first n integers, where n is specified by the user: #include <iostream> #include <iomanip> using namespace std; int main () { int n; cout << "Please enter the number of squares and cubes to display: "; cin >> n; cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= n) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0;

44 Designing the While Loop
typical format: Loops are typically controlled by a variable (or set of variables). This is where the variables receive their values. initialization while ( ) { } boolean expression C++ Statements This is typically where the control variables are updated. <update>

45 Modify the program to print out the results for the first 5 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } return 0; Initialization Update

46 Loop Programming useful to determine beforehand
what the initialization value of the control variable is: assigning a variable a value reading a value from the user what the loop expression is what the loop statement will do (aside from the update) what the update is

47 Example: Write a program that calculates the sum of the first 20 positive integers ( …. + 20) Initialization: initialize control variable to 1 initialize the sum to 0 Loop Expression Loop while the control variable <= 20 Loop statement Add the control variable value to the current sum Update increment the variable by 1

48 Write a program that calculates the sum of the first 20 positive integers.
#include <iostream> #include <iomanip> using namespace std; int main () { int sum = 0; int num = 1; while (num <= 20) { sum += num; num++; } cout << "Sum = " << sum << endl; return 0; initialization while ( boolean expression) Initialization: initialize control variable to 1 initialize sum to 0 Loop Expression Loop while the control variable <= 20 Loop statement Add the control variable value to the current sum Update increment the variable by 1 C++ Statements <update> }

49 Write a program that calculates the sum of the first 20 positive integers.
#include <iostream> #include <iomanip> using namespace std; int main () { int sum = 0; int num = 1; while (num <= 20) { sum += num; num++; } cout << "Sum = " << sum << endl; return 0; while ( boolean expression) Initialization: initialize control variable to 1 initialize sum to 0 Loop Expression Loop while the control variable <= 20 Loop statement Add the control variable value to the current sum Update increment the variable by 1 C++ Statements <update> }

50 Write a program that calculates the sum of the first 20 positive integers.
#include <iostream> #include <iomanip> using namespace std; int main () { int sum = 0; int num = 1; while (num <= 20) { sum += num; num++; } cout << "Sum = " << sum << endl; return 0; while ( Initialization: initialize control variable to 1 initialize sum to 0 Loop Expression Loop while the control variable <= 20 Loop statement Add the control variable value to the current sum Update increment the variable by 1 C++ Statements <update> }

51 Write a program that calculates the sum of the first 20 positive integers.
#include <iostream> #include <iomanip> using namespace std; int main () { int sum = 0; int num = 1; while (num <= 20) { sum += num; num++; } cout << "Sum = " << sum << endl; return 0; while ( Initialization: initialize control variable to 1 initialize sum to 0 Loop Expression Loop while the control variable <= 20 Loop statement Add the control variable value to the current sum Update increment the variable by 1 <update> }

52 Write a program that calculates the sum of the first 20 positive integers.
#include <iostream> #include <iomanip> using namespace std; int main () { int sum = 0; int num = 1; while (num <= 20) { sum += num; num++; } cout << "Sum = " << sum << endl; return 0; Initialization: initialize control variable to 1 initialize sum to 0 Loop Expression Loop while the control variable <= 20 Loop statement Add the control variable value to the current sum Update increment the variable by 1

53 Write a program that calculates the sum of the first 20 positive integers.
#include <iostream> #include <iomanip> using namespace std; int main () { int sum = 0; int num = 1; while (num <= 20) { sum += num; num++; } cout << "Sum = " << sum << endl; return 0; Initialization: initialize control variable to 1 initialize sum to 0 Loop Expression Loop while the control variable <= 20 Loop statement Add the control variable value to the current sum Update increment the variable by 1

54

55 There are several forms of loops
counter-controlled loops when you know exactly how many iterations there will be (like the previous example) sentinel-controlled loops when you don't know how many iterations there will be require a special value for stopping flag-controlled loop loops until a boolean variable changes value

56 Sentinel-controlled Loop
common when reading input from a user from a file continues to loop until a pre-defined special value is input value is called a sentinel

57 Example: Write a program to read in numbers from a user, and prints the sum of the numbers.
How do we know when the user is done? Solution: Use a sentinal value (such as 0) That is, loop until the user enters the sentinel value, then stop and print the sum of the numbers.

58 Example: Write a program to read in numbers from a user, and prints the sum of the numbers.
Initialization: initialize sum to 0 read input from user into control variable Loop Expression Loop while the control variable is not zero Loop statement Add the control variable value to the current sum Update

59 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } cout << "Sum = " << sum << endl; return 0; initialization Initialization: initialize sum to 0 read input from user into control variable Loop Expression Loop while the control variable is not zero Loop statement Add the control variable value to the current sum Update while ( boolean expression) C++ Statements <update> }

60 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } cout << "Sum = " << sum << endl; return 0; Initialization: initialize sum to 0 read input from user into control variable Loop Expression Loop while the control variable is not zero Loop statement Add the control variable value to the current sum Update while ( boolean expression) C++ Statements <update> }

61 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } cout << "Sum = " << sum << endl; return 0; Initialization: initialize sum to 0 read input from user into control variable Loop Expression Loop while the control variable is not zero Loop statement Add the control variable value to the current sum Update C++ Statements <update> }

62 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } cout << "Sum = " << sum << endl; return 0; Initialization: initialize sum to 0 read input from user into control variable Loop Expression Loop while the control variable is not zero Loop statement Add the control variable value to the current sum Update <update> }

63 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } cout << "Sum = " << sum << endl; return 0; Initialization: initialize sum to 0 read input from user into control variable Loop Expression Loop while the control variable is not zero Loop statement Add the control variable value to the current sum Update }

64 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } cout << "Sum = " << sum << endl; return 0; Initialization: initialize sum to 0 read input from user into control variable Loop Expression Loop while the control variable is not zero Loop statement Add the control variable value to the current sum Update }

65

66 For Loop similar to while loop
explicit place for initialization and update each component separated by semicolons initialization while ( ) { } boolean expression C++ Statements update for ( ; ; ) initialization boolean expression update C++ Statements

67 For Loop this statement is executed once, before the loop starts
this expression is evaluated before the loop statement this statement is executed after the loop statement for ( ; ; ) initialization boolean expression update C++ Statements

68 Rewrite our while loop for the square and cube table using a for loop
int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } for ( ; ; ) initialization boolean expression update C++ Statements

69 Rewrite our while loop for the square and cube table using a for loop
int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } for ( ; ; ) int num = 1 boolean expression update C++ Statements

70 Rewrite our while loop for the square and cube table using a for loop
int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } for ( ; ; ) int num = 1 num <=5 update C++ Statements

71 Rewrite our while loop for the square and cube table using a for loop
int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } for ( ; ; ) int num = 1 num <=5 num++ C++ Statements

72 Rewrite our while loop for the square and cube table using a for loop
int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } for ( ; ; ) int num = 1 num <=5 num++ cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl;

73 Rewrite our while loop for the square and cube table using a for loop
int num = 1; while (num <= 5) { cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; num++; } for (int num = 1; num <= 5; num++) cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl;

74 Write a program to display a table of squares and cubes of the first 5 integers:
#include <iostream> #include <iomanip> using namespace std; int main () { cout << "NUMBER SQUARE CUBE" << endl; cout << " " << endl; for (int num = 1; num <= 5; num++) cout << setw(6) << num << setw(10) << num*num << setw(8) << num*num*num << endl; return 0; }

75 For Loops you can have more than one statement (actually expressions!!) in the initialization component separated by commas (comma operator) int x, sum; for (x = 1, sum = 0; x < 20; x++) sum += x;

76 Comma operator (left to right, very low precedence!)
expr1 , expr2 (expr1 is evaluated, expr2 is evaluated, result is the value of expr2) Examples 2,3,4,5 evaluates to 5 Was created for “for” loop!

77 For Loops you can declare a variable in the initialization component
int sum = 0; for (int x = 1; x < 20; x++) sum += x;

78 For Loops you can declare more than one variable of the same type in the initialization statement they must be declared with only one keyword // this is ok for (int x = 1, sum = 0; x < 20; x++) sum += x; // this is not ok for (int x = 1, int sum = 0; x < 20; x++) sum += x;

79 For Loops you cannot declare variables of different types in the initialization component you can, however, assign variables of different types in the initialization component // this is not ok for (int x = 1, double sum = 0; x < 20; x++) sum += x; int x; double sum; for (x = 1, sum = 0; x < 20; x++) sum += x;

80 For Loops any while loop can be rewritten as a for loop how to choose?
Personal preference For Loops are typically used when: the initialization and update are simple (typically only one or two small statements) the loop is count controlled (not sentinel controlled)

81 Example: Convert the following while loop to a for-loop
int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } Ugly – much easier to read as a while loop int sum; int num; for (sum = 0; cout << "Please enter a number: ", cin >> num; num != 0; cout << "Please enter a number: ", cin >> num) { sum += num; }

82 For Loops it is not required that any code be placed into the initialization or update component they can be empty when they are empty, they are known as nulls in this case, the for loop looks much like a while loop

83 Example: Convert the following while loop to a for-loop
int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } int sum = 0; int num; cout << "Please enter a number: "; cin >> num; for ( ; num != 0; ) { sum += num; }

84 Question: How many times will the following loop execute?
Answer: 0 times if the condition is initially false, the loop never executes this means a while loop and a for loop may never execute their code int max = 10; int num = 11; while (num < max) { cout << num << endl; num++; }

85 Do-While Syntax: do C++ Statement while ( ); boolean expression
The condition is tested after the statement is executed

86 Example: Write a program to read in numbers from a user, and prints the sum of the numbers.
User is done when 0 (sentinel) is typed in

87 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } cout << "Sum = " << sum << endl; return 0; }

88 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; cout << "Please enter a number: "; cin >> num; while (num != 0) { sum += num; } cout << "Sum = " << sum << endl; return 0; }

89 Write a program to read in numbers from a user, and prints the sum of the numbers.
#include <iostream> using namespace std; int main () { int sum = 0; int num; do { cout << "Please enter a number: "; cin >> num; sum += num; } while (num != 0); cout << "Sum = " << sum << endl; return 0; } Since the code inside of the loop gets executed at least once, we can read our initial value inside the loop }

90

91 While/For vs. Do-While Which to use?
While/For Loops execute their statement 0 or more times Do-while executes its statement at least 1 time Which to use? programmer preference do-while loops are quite uncommon more difficult to read can be rewritten using a while loop

92 Example: Write a program that reads an integer n from the user and displays n stars in a line:
Enter the numbers of stars: 5 *****

93 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int stars; cout << "Enter the number of stars: "; cin >> stars; for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; return 0;

94 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; return 0;

95 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0;

96 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0;

97 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0;

98 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0;

99 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0;

100 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0;

101 Example: Write a program that reads an integer n from the user and displays n stars in a line:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; int i = 1; while (i <= n) { cout << "*"; i++; } cout << endl; return 0;

102

103 Nested Loops Nested Loops
recall that a loop body can contain virtually any C++ statement: this includes another loop when a loop is inside another loop, we call the inner loop a nested loop the nested loop is called once for each iteration of the outer loop

104 Example: Write a program that reads an integer n from the user and displays a square of n x n stars:
Enter the height of square: 5 *****

105 Problem Breakdown: ***** for (int j = 0; j < n; j++) {
for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; Repeat n times draw a line of n stars go to next line

106 Problem Breakdown: ***** for (int j = 0; j < n; j++) {
for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; Repeat n times Repeat n times draw a line of n stars go to next line

107 Problem Breakdown: ***** for (int j = 1; j <= n; j++) {
for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; Repeat n times Repeat n times draw a line of n stars go to next line

108 Problem Breakdown: ***** for (int j = 1; j <= n; j++) {
for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; Repeat n times Repeat n times draw a line of n stars go to next line go to next line

109 Problem Breakdown: ***** for (int j = 1; j <= n; j++) {
for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; Repeat n times Repeat n times draw a line of n stars go to next line go to next line

110 Problem Breakdown: ***** ?? for (int j = 1; j <= n; j++) {
for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; Repeat n times Repeat n times draw a line of n stars draw a line of n stars ?? go to next line go to next line

111 This code drew us a line of n stars!!
Example: Write a program that reads an integer n from the user and displays n stars in a line: #include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0; for (int i = 1; i <= n; i++) { cout << "*"; } This code drew us a line of n stars!!

112 This code drew us a line of n stars!!
Example: Write a program that reads an integer n from the user and displays n stars in a line: #include <iostream> using namespace std; int main() { int n; cout << "Enter the number of stars: "; cin >> n; for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0; for (int i = 1; i <= n; i++) { cout << "*"; } This code drew us a line of n stars!!

113 Problem Breakdown: ***** ?? for (int j = 1; j <= n; j++) {
for (int i = 1; i <= stars; i++) { cout << "*"; } cout << endl; Repeat n times Repeat n times for (int i = 1; i <= n; i++) { cout << "*"; } draw a line of n stars draw a line of n stars ?? go to next line go to next line

114 Problem Breakdown: ***** for (int j = 1; j <= n; j++) {
for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; Repeat n times draw a line of n stars go to next line

115 Write a C++ program to display a square of stars on the screen:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the height of the square: "; cin >> n; for (int j = 1; j <= n; j++) { for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0;

116

117 Note that inside the inner loop, the code has access to the outer loop variables
for (int j = 1; j <= n; j++) { for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; At this point in the code, we have access to i and j variable

118 This means the outer loop can be used to control the inner loop
Example: Write a program to display the following to the screen: * ** *** **** ***** ****** ******* ********

119 Write a C++ program to display a square of stars on the screen:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the height of the square: "; cin >> n; for (int j = 1; j <= n; j++) { for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; return 0; What controls the length of the line being drawn?

120 Problem Breakdown: * ** *** **** ***** ****** ******* ******** ??
for (int j = 1; j <= n; j++) { for (int i = 1; i <= n; i++) { cout << "*"; } cout << endl; Repeat n times draw x stars, where x is the current pass through the loop. ?? go to next line

121 Problem Breakdown: * ** *** **** ***** ****** ******* ********
for (int j = 1; j <= n; j++) { for (int i = 1; i <= j; i++) { cout << "*"; } cout << endl; Repeat n times draw x stars, where x is the current pass through the loop. go to next line

122 Write a C++ program to display a triangle of stars on the screen:
#include <iostream> using namespace std; int main() { int n; cout << "Enter the height of the triangle: "; cin >> n; for (int j = 1; j <= n; j++) { for (int i = 1; i <= j; i++) { cout << "*"; } cout << endl; return 0;

123 It is possible to embed loops as deep as you like:
for (int a = 0; a < n; a++) { for (int b = 0; b < n; b++) { for (int c = 0; c < n; c++) { for (int d = 0; d < n; d++) { ...

124 Caution must be used when embedding loops
Loops have the power to create a long program with few lines Nested loops increase length multiplicatively for (int i = 1; i <= n; i++) { cout << "*"; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << "*"; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= j; j++) { for (int k = 1; k <= j; k++) { cout << "*"; } cout statement executes n times cout statement executes n2 times cout statement executes n3 times

125 break and continue C++ statements break continue terminates a loop
code begins at statement following loop continue skips the remaining code in the loop

126 Example: Write a program that reads in 5 user numbers, and displays their inverse (1/x)
if the user enters a 0, print "Stop trying to break my program", and exits the loop

127 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and exits the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; cout << "The inverse of " << x << " is " << (1.0 / x) << endl; } return 0;

128 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and exits the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; cout << "The inverse of " << x << " is " << (1.0 / x) << endl; } return 0;

129

130 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and exits the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; cout << "The inverse of " << x << " is " << (1.0 / x) << endl; } return 0;

131 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and exits the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; if (x == 0) { cout << "Stop trying to break my program" << endl; break; } cout << "The inverse of " << x << " is " << (1.0 / x) << endl; return 0;

132

133 Example: Write a program that reads in 5 user numbers, and displays their inverse (1/x)
if the user enters a 0, print "Stop trying to break my program", but continues the loop

134 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and continues the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; if (x == 0) { cout << "Stop trying to break my program" << endl; break; } cout << "The inverse of " << x << " is " << (1.0 / x) << endl; return 0;

135 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and continues the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; if (x == 0) { cout << "Stop trying to break my program" << endl; continue; } cout << "The inverse of " << x << " is " << (1.0 / x) << endl; return 0;

136

137 break and continue some consider both bad programming practice
break is somewhat common continue is very uncommon some consider both bad programming practice difficult to follow code can always be rewritten with conditional statements

138 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and exits the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; if (x == 0) { cout << "Stop trying to break my program" << endl; break; } cout << "The inverse of " << x << " is " << (1.0 / x) << endl; return 0;

139 #include <iostream> using namespace std; int main() {
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and exits the loop #include <iostream> using namespace std; int main() { int x = 99; for (int count = 0; (x != 0) && (count < 5); count++) { cout << "Please enter a number: "; cin >> x; if (x == 0) { cout << "Stop trying to break my program" << endl; } else { cout << "The inverse of " << x << " is " << (1.0 / x) << endl; } return 0;

140 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and continues the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; if (x == 0) { cout << "Stop trying to break my program" << endl; continue; } cout << "The inverse of " << x << " is " << (1.0 / x) << endl; return 0;

141 #include <iostream> using namespace std; int main() { int x;
Write a program that reads in 5 user numbers, and displays their inverse (1/x) if the user enters a 0, print "Stop trying to break my program", and continues the loop #include <iostream> using namespace std; int main() { int x; for (int count = 0; count < 5; count++) { cout << "Please enter a number: "; cin >> x; if (x == 0) { cout << "Stop trying to break my program" << endl; } else { cout << "The inverse of " << x << " is " << (1.0 / x) << endl; } return 0;

142 Loops – Summary three types of loops three styles of loop coding
while for do-while three styles of loop coding fixed count sentinel-controlled flag-controlled loops can be nested break and continue artificial loop control use with care


Download ppt "Computer Science 1620 Loops."

Similar presentations


Ads by Google