Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CSC 221: Computer Programming I Fall 2001  conditional repetition  repetition for: simulations, input verification, input sequences, …  while loops.
Repetition control structures
Computer Science 1620 Loops.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
Chapter 5: Loops and Files.
1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
CS 117 Spring 2002 Repetition Hanly Chapter 4 Friedman-Koffman Chapter 5.
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.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
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.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 6 Looping.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
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 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
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.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
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,
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
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.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
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.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
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.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Engineering Problem Solving with C++, Etter/Ingber
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
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.
Repetition Structures (Loops)
Control Structures - Repetition
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.
Repetition Statements
Control Structures Part 1
Repetition Statements (Loops) - 2
Presentation transcript:

Sahar Mosleh California State University San MarcosPage 1 While Loop and For Loop

Sahar Mosleh California State University San MarcosPage 2 While - Programs are very good at performing similar tasks over and over - In a payroll program, printing thousands of cheques involves a similar activity being repeated thousands of times - Repetition in programming often called looping - Repetition in C++ can be handled by the “while” looping construct

Sahar Mosleh California State University San MarcosPage 3 While Loops while (logical expression) Statement; Next-Statement; Is logical expression true Execute statement Execute next statement No Yes

Sahar Mosleh California State University San MarcosPage 4 While Loops - Often used a compound statement in a while loop while (logical expression) { stmt 1; stmt 2; ……. stmt n; } As long as the logical expression is true, statements within the block will execute repeatedly (checks before each iteration)

Sahar Mosleh California State University San MarcosPage 5 Count Controlled While As long as I have less than 10 parking tickets, I will continue to park illegally - This is a count controlled loop - An activity will continue to take place until a specific count is reached - Could be pseudo-coded as follows:

Sahar Mosleh California State University San MarcosPage 6 Count Controlled While int ticket_ count = 0; while (ticket_ count < 10) { keep parking illegally if (I get a ticket) { ticket_count++; } Loop controlled by counter that must be given an initial value Counter is checked at “top” of loop. If condition true, body of loop is executed Can “nest” constructs within other constructs

Sahar Mosleh California State University San MarcosPage 7 Count Controlled While int count = 1; // Print out “Hello World!” 10 times while (count <= 10) { cout << “Hello World!” << endl; count++; } - What would happen if count was set to 0 initially?

Sahar Mosleh California State University San MarcosPage 8 Event Controlled While As long as I am not in LA, I will continue to drive - This is an event controlled loop An activity will continue to take place until a specific event occurs - Could be pseudo- coded as follows:

Sahar Mosleh California State University San MarcosPage 9 Event Controlled While bool LA = false; while (! LA) { keep driving if (I am in LA) { LA = true; } Loop controlled by Boolean that must be given an initial value Boolean is checked at “top” of loop. If condition is true, body of loop is executed

Sahar Mosleh California State University San MarcosPage 10 Event Controlled While There can be many events that control a while loop – One of the most common is the “end of file” (EOF) event

Sahar Mosleh California State University San MarcosPage 11 End of File - Input file streams generally have a special EOF character at the very end - When a program encounters this character, the EOF condition is raised - This is often an event that stops the loop - When reading an input file stream like cin or fin, the name of the file stream can be used as if it were a Boolean - If the last input operation was successful it will be true -- false otherwise

Sahar Mosleh California State University San MarcosPage 12 End of File #include using namespace std; int main() { ifstream infile; int value; infile. open(“myfile.txt”); infile >> value; while (infile) { cout << value << endl; infile >> value; } This input statement checks to see if the file is empty If there is nothing more to read (EOF), then infile will effectively be false and the loop will soon terminate

Sahar Mosleh California State University San MarcosPage 13 End of File #include using namespace std; int main() { ifstream infile; int value; infile.open(“myfile.txt”); if (! infile) {cout << “Oh no: (can’t open file!)” << endl; return;} infile >> value ; while (infile) { cout << value << endl; infile >> value; } Immediately after the open: infile is true if open successful infile is false if open unsuccessful

Sahar Mosleh California State University San MarcosPage 14 End of File - This program can also be written as follows: ifstream infile; int value; infile.open(“myfile”); if ( ! infile){ cout << “Oh no: (can’t open file!)”; return; } infile >> value; while ( ! infile.eof() ) { cout << value; infile >> value; }

Sahar Mosleh California State University San MarcosPage 15 While - Whiles are often used as “undetermined” loops - Not sure how long a while loop will last - When will EOF be found? - Very often, it is known exactly how long a while loop last - Perhaps an idea to use a more deterministic looping construct

Sahar Mosleh California State University San MarcosPage 16 While Recall the while construct: stmt1; while (expr1) { stmt1; stmt2; stmt3; stmtn; }... initialization Anything to change the Status of expre1 in the while (ex: increment) continuation

Sahar Mosleh California State University San MarcosPage 17 For The for repetition construct: for (expr0; expr1; expr2) { stmt1; stmt2; stmt3; … ;.. ; stmtn; } The for loop is controlled by three statements / expressions initialization increment continuation

Sahar Mosleh California State University San MarcosPage 18 For - A compact counted loop: for (count= 1; count <= 10; count++) { stmt1; stmt2; stmt3; …. ; stmtn; } initialization increment continuation How many times will this loop execute?

Sahar Mosleh California State University San MarcosPage 19 For When it is known “ahead of time” how many times to loop, consider a for loop. For example, - Adding marks of 100 students - Getting exactly 10 numbers from the user - Reading the first 5 numbers from an input file

Sahar Mosleh California State University San MarcosPage 20 For (example) Write a for loops to print stars as follows: * *** ***** ******* ********* ******* ***** *** *

Sahar Mosleh California State University San MarcosPage 21 For int i, stars; // printing the first 5 lines for (stars= 1; stars<= 9; stars= stars+ 2) { for (i= 1; i<= stars; i++) cout << ‘* ’; cout << endl; } // printing the last 4 lines for (stars= 7; stars>= 1; stars= stars- 2) { for (i= 1; i<= stars; i++) cout << ‘* ’; cout << endl ; }