Computer Science 1620 Loops
Write a program to display a table of squares and cubes of the first 5 integers: NUMBER SQUARE CUBE ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125
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; }
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; }
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; }
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; }
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; }
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;
Issues 1) Repeated code 2) Non-scalable 3) Non user-friendly
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
Rewrite our program to display a table of squares and cubes of the first 5 integers: NUMBER SQUARE CUBE ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125
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; }
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;
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:
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
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 ------ ------ ----
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 ------ ------ ----
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 ------ ------ ----
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 ------ ------ ---- 1 1 1
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 ------ ------ ---- 1 1 1
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 ------ ------ ---- 1 1 1
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 ------ ------ ---- 1 1 1 2 4 8
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 ------ ------ ---- 1 1 1 2 4 8
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 ------ ------ ---- 1 1 1 2 4 8
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 ------ ------ ---- 1 1 1 2 4 8 3 9 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 ------ ------ ---- 1 1 1 2 4 8 3 9 27
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 ------ ------ ---- 1 1 1 2 4 8 3 9 27
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 ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64
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 ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64
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 ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64
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 ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125
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 ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125
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 ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125
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 ------ ------ ---- 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125
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;
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;
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;
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;
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;
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;
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;
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>
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
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
Example: Write a program that calculates the sum of the first 20 positive integers (1 + 2 + 3 + …. + 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
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> }
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> }
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> }
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> }
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
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
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
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
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.
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
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> }
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> }
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> }
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> }
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 }
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 }
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
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
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
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
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
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
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;
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;
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; }
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;
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!
For Loops you can declare a variable in the initialization component int sum = 0; for (int x = 1; x < 20; x++) sum += x;
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;
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;
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)
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; }
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
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; }
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++; }
Do-While Syntax: do C++ Statement while ( ); boolean expression The condition is tested after the statement is executed
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
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; }
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; }
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 }
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
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 *****
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;
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;
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;
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;
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;
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;
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;
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;
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;
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
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 *****
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
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
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
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
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
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
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!!
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!!
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
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
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;
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
This means the outer loop can be used to control the inner loop Example: Write a program to display the following to the screen: * ** *** **** ***** ****** ******* ********
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?
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
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
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;
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++) { ...
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
break and continue C++ statements break continue terminates a loop code begins at statement following loop continue skips the remaining code in the loop
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
#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;
#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;
#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;
#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;
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
#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;
#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;
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
#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;
#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;
#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;
#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;
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