Computer Science 1620 Loops.

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
Objectives You should be able to describe:
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Chapter 4: Control Structures II
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Lecture 4B More Repetition Richard Gesick
Chapter 2.2 Control Structures (Iteration)
Computing Fundamentals
Let’s all Repeat Together
Objectives You should be able to describe: The while Statement
Presentation transcript:

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