Control Structures Looping Part 2 Part I The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

Computer Science 1620 Loops.
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.
Chapter 5: Control Structures II (Repetition)
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Objectives You should be able to describe:
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Control Structures Repetition or Iteration or Looping Part II.
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.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Control Structures RepetitionorIterationorLooping Part I.
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.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (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.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
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.
Control Structures Repetition or Iteration or Looping Part II.
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 8 Repetition Statements
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Outline Altering flow of control Boolean expressions
Computing Fundamentals
Control Structures Repetition or Iteration Looping Part I.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 5: Control Structures II (Repetition)
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Control Structures Looping Part 2 Part I

The C++ for Statement Meaning: 1.Evaluate all initialization expressions 2.Evaluate expression, if true: a)Execute (compound) statement b)Evaluate all altering expressions(s) c)Repeat until expression is false Syntax for (initialize; expression; altering list) statement

The for Statement Syntax for (initialize; expression; alter) statement cnt=1 Example: for (cnt=1; cnt < 7; cnt++) { cout << “Yankees are #1\n”; cout << “They’ll win the Series!\n”; } next statement *

The for Statement ã used as a counting loop (up or down) ã semicolons separate the items ã items 1 and 2 are statements (end with ;) ã you may declare variable and initialize it ex. for (int cnt = 1; cnt < 7; cnt++) (not recommended by instructor) ã performs the same functions as the while

int num; cout << "NUMBER SQUARE CUBE\n" << " \n"; for (num = 1; num < 11; num++) { cout << setw(3) << num << " " << setw(3) << num * num << " " << setw(4) << num * num * num << "\n"; } for (num = 1; num < 11; num++) { cout << setw(3) << num << " " << setw(3) << num * num << " " << setw(4) << num * num * num << "\n"; } A Simple Example *

for Statement Examples for Statement Examples for (k = 1; k <= n; k = k + 1) x = k * k; * k = 12; for (j = 2; k % j == 0; j = j + 1) { cout << j << “ is a divisor of “ << k << ‘\n’; sum = sum + j; } n = 2; // perhaps as input by user

Not Valid: for (j = 0, j < n, j = j + 3) // semicolons needed for (j = 0; j < n ) // three parts needed //or at least a semicolon for Statement Examples for Statement Examples

What goes in the for statement? Initialization: a comma separated list of assignments to execute before even testing for entry Expression: any expression that has a true/false (numeric) result Update: any expression to be evaluated at the end of the loop (after executing the statements inside the loop) before testing for loop exit. “any” may be null or a comma separated list

for -- Null Expressions for -- Null Expressions Example 1:j = 1; sum = 0; for ( ; j <= 10; j = j + 1) sum = sum + j; Example 2:j = 1; sum = 0; for ( ; j <= 10; ) sum = sum + j; * * * Do not leave out the expression or you may have an infinite loop

Nested Loops When a decision or control structure is contained within another decision or control sturcture, the inner one is said to be nested. * while... if... while... You may have repetition within decision and vice versa.

Example int y,z; for (y = 5; y > 0; y--) {cout << "\nAli R."; cout << “**”; } for (z = 1; z < 3; z++) cout <<"\tVic R.\t"; Nested Loops - Ex.1 *

Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Ali R. Vic R.Vic R.** Nested Loops - Ex.1 Output Ali R. Vic R.Vic R.** *

1 int y,z; 2 for (y = 5; y > 0; y--) 3 {cout << "\nAli R."; 4 for (z = 1; z < 3; z++) 5 cout <<"\tVic R.\t"; 6 cout << “**“; 7 } Nested Loops - Ex.1 Execution *

Example 2 Write a program that determines the average score for each of four experiments Each experiment includes six trials Read a score for each trial and print the average for each experiment for each experiment for each trial read score accumulate scores calculate and print averages Even a simple program deserves an algorithm

4 for (trial = 1; trial > score; 7 total = total + score; 8 } 9 avg = total/(trial-1); 10 cout << "Average for experiment "<< exper 11 << " is “<< setprecision(4)<< avg<< "\n\n"; 12 } Nested Loops - Ex. 2 * 1 for (exper =1; exper<=4; exper=exper +1) 2 { cout << "\tScores for experiment "<< exper <<":\n"; 3 total = 0.0;

for vs. while cnt = 1cnt++ for (cnt = 1; cnt < 7; cnt++) { cout << … } cnt = 1; while (cnt < 7) { cout << … cnt++; cnt++;} *

Bowling Team Example A bowling team consists of five players each of whom bowls three games. Write a program that allows the user to enter each players scores and displays the players average. The team average is displayed at the end. First write a plan.

Bowling Team Plan display instructions for - players for - games I/O scores sum scores average scores display scores sum player averages team average display team average

Common Errors Improper braces in nested structures Using = in place of == != versus == This changes the logic, be especially careful when used with && or || infinite loops: != versus && versus || *

The End - part 2 The End