Counting Loops.

Slides:



Advertisements
Similar presentations
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Advertisements

While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
CS150 Introduction to Computer Science 1
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
Chapter 4 Summation.
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Arrays.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
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 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.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 05 (Part III) Control Statements: Part II.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Overview Go over parts of quiz? Another iteration structure for loop.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
The Basics of Arrays Problem: How can the rancher easily catalog all of his cattle?
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
FALL 2001ICOM Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Sentinel Loops. while Syntax while(expression) statement.
Basic concepts of C++ Presented by Prof. Satyajit De
Programming Loops (continued).
CS 1428 Exam I Review.
Programming Fundamentals
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming Fundamentals
Function Basics.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
While Loops.
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
CS 101 First Exam Review.
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
CS150 Introduction to Computer Science 1
CS 1428 Exam I Review.
Presentation transcript:

Counting Loops

for Syntax for (expression1; expression2; expression3)     statement

for Syntax for (expression1; expression2; expression3)     statement

for Syntax for (expression1; expression2; expression3)     statement

for Syntax for (expression1; expression2; expression3)     statement

for Syntax for (expression1; expression2; expression3)     statement

for Syntax for (expression1; expression2; expression3)     statement

Bad Form cout << “What is 2+2? ”; cin >> sum; for (; sum != 4;) { cout << “Incorrect answer. What is 2+2? ” << endl; cin >> sum; }

Example #1 float average; long sum = 0; short i = 0; cout << “enter positive integer: ”; cin >> max; for(i=0; i <= max; i++)   sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

Example #1 float average; long sum = 0; short i = 0; cout << “enter positive integer: ”; cin >> max; for(i=0; i <= max; i++)   sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

Example #1 float average; long sum = 0; short i = 0; cout << “enter positive integer: ”; cin >> max; for(i=0; i <= max; i++)   sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

Example #1 float average; long sum = 0; short i = 0; cout << “enter positive integer: ”; cin >> max; for(i=0; i <= max; i++)   sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

Example #2 float average; long sum = 0; short i = 0; cout << “enter positive integer: ”; cin >> max; for (i=0; i <= max; i+=2)   sum += i; average = static_cast<float>(sum) / max; cout << “average is ” << average << endl;

Goal Output * * * * * * * * * * * * * * *

Outputting 5 Lines for (int i = 1; i <= 5; i++) {   cout << endl; }

Outputting 5 Stars for (int i = 1; i <= 5; i++) {   cout << “* ”; }

Combining for (int i = 1; i <= 5; i++) {    for (int j = 1; j <= 5; j++)         cout << "* ";                           cout << endl; } output variable values i=1 j=?

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values i=1 j=1

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * i=1 j=1

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * i=1 j=2

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * i=1 j=2

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * i=1 j=3

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * i=1 j=3

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * i=1 j=4

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * i=1 j=4

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * i=1 j=5

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=1 j=5

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=1 j=6

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=1 j=6

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=2 j=?

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=2 j=1

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * i=2 j=1

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * i=2 j=2

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * i=2 j=2

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * i=2 j=3

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * i=2 j=3

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * i=2 j=4

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * i=2 j=4

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * i=2 j=5

Combining for (int i = 1; i <= 5; i++) {     for (int j = 1; j <= 5; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=2 j=5 oops!!!

Goal vs Actual Goal * * * * * * * * * * * * * * * Actual * * * * * * * * * * * * * * * Actual * * * * * * * * * * * * * * * * * * * * * * * * * i=1 i=2 j=5 i=3 j=4 i=4 j=3 i=5 j=2 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values i=1 j=?

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values i=1 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * i=1 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * i=1 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * i=1 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * i=1 j=3

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * i=1 j=3

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * i=1 j=4

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * i=1 j=4

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * i=1 j=5

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=1 j=5

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=1 j=6

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=1 j=6

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=2 j=?

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * i=2 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * i=2 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * i=2 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * i=2 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * i=2 j=3

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * i=2 j=3

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * i=2 j=4

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * i=2 j=4

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * i=2 j=5

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * i=2 j=5

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * i=3 j=?

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * i=3 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * i=3 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * i=3 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * i=3 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * i=3 j=3

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * i=3 j=3

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * i=3 j=4

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * i=3 j=4

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * i=4 j=?

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * i=4 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * i=4 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * i=4 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * i=4 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * i=4 j=3

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * i=4 j=3

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * i=5 j=?

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * i=5 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * * i=5 j=1

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * * i=5 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * * i=5 j=2

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * * i=6 j=?

Fixed for (int i=1; i <= 5; i++) {     for (int j=1; j <= 5 – i + 1; j++)           cout << "* ";                           cout << endl; } output variable values * * * * * * * * * * * * * * * i=6 j=?

Goal Output * * * * * * * * * * * * * * *

Solution Rows * * * * * * * * * * * * * * * for (short i = 0; i < 5; i++) {     for (short j = 1; j <= i; j++)         cout << " ";     for (short j = i; j < 5; j++)         cout << "* ";     cout << endl; } * * * * * * * * * * * * * * *

Solution Spaces * * * * * * * * * * * * * * * for (short i = 0; i < 5; i++) {     for (short j = 1; j <= i; j++)         cout << " ";     for (short j = i; j < 5; j++)         cout << "* ";     cout << endl; } * * * * * * * * * * * * * * *

Solution Stars * * * * * * * * * * * * * * * for (short i = 0; i < 5; i++) {     for (short j = 1; j <= i; j++)         cout << " ";     for (short j = i; j < 5; j++)         cout << "* ";     cout << endl; } * * * * * * * * * * * * * * *

Out of Scope for (int i = 1; i <= 10; i++)   cout << “hello” << endl; cout << i << endl;

Out of Scope for (int i = 1; i <= 10; i++) {   cout << “hello” << endl; } cout << i << endl;

Fixed Scope int main() { int i; for (i = 1; i <= 10; i++) {      cout << “hello” << endl; } cout << i << endl; return 0; }

Duplicate Variables int main() { int i = 20; for (int i =1; i <= 10; i++) {      cout << “hello” << endl; } cout << i << endl; return 0; }

End of Session