General Condition Loop

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Computer Science 1620 Loops.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
The If/Else Statement, Boolean Flags, and Menus Page 180
What is the out put #include using namespace std; void main() { int i; for(i=1;i
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
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
CONTROLLING PROGRAM FLOW
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.
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.
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.
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,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Control Structures RepetitionorIterationorLooping Part I.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
1 Standard Version of Starting Out with C++, 4th Brief Edition 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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Chapter 3 Selection Statements
Topic 4: Looping Statements
Chapter 6: Loops.
REPETITION CONTROL STRUCTURE
Loops in Java.
Control Structures.
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.
Chapter 2.2 Control Structures (Iteration)
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
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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.
Loops October 10, 2017.
Iteration with While You can say that again.
Additional Control Structures
Loops A loop is a repetition control structure.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
Looping III (do … while statement)
CS150 Introduction to Computer Science 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
Objectives You should be able to describe: The while Statement
Repetition Statements (Loops) - 2
Control Statements Paritosh Srivastava.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

General Condition Loop A general condition loop is a catch-all category for loops that are not count-controlled nor sentinel-controlled The program loops until some general condition becomes false Usually it is implemented with the while statement

Nim's Game Consider Nim's game. This is a two player game. We begin with a pile of 21 stones, and the players takes turns, each player taking 1, 2, or 3 stones from the pile. The player who takes the last stone from the pile loses.

Nim's Game (Code) int num = 21, winner = 1, n; while (num > 0) { cout << num << " stones. Plr 1: How many?"; cin >> n; num -= n; if(num <= 0) winner = 2; else { cout << num << " stones. “ << “Plr 2: Take how many?"; } cout << "Winner is " << winner << endl;

Nim's Game (Output) 21 stones. Plr 1: Take how many?2 Winner is 1

Break, continue, and return Three different C++ statements can be used to change how the loop code is done: The break statement ends the loop immediately. Control pass to the first statement after the loop. The continue statement jumps to the end of the loop, but the loop continues. The return statement returns from the function (main).

Nim's Game (Code 2) int num = 21, p = 1, n; while (true) { cout << num << " stones. Plr " << p << ": Take how many?"; cin >> n; num -= n; p = 3 - p; // Change player, 1->2, 2-> 1 if(num <= 0) { cout << "Winner is " << p << endl; break; }

Do-While statement The do-while statement is similar to the while statement, but the test comes after the loop body: do { <statement> while (<Boolean expression>); So, the loop body is always done first, then the expression is evaluated and if true, the loop is repeated.

Sentinel-controlled Loop The do-while statement could be used to implement a sentinel controlled loop, but at the cost of writing the test twice. do { cin >> temp; if(temp >= 0) <process temp>; while(temp>=0);

The Comma Operator The comma operator is used to sequentially evaluate expressions. For example, a+b, c-d, i++, will first evaluate a+b, then evaluate c-d, and then evaluate i++. The value of whole expression is the value of the last term. It is most commonly used in the iteration step of a for-loop. for(i=0, j=0; i < 10; i++, j+=2){}

Exercise Consider how to print out one page of a monthly calendar: Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 12 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Exercise (Cont'd) The program should read in the number of days in the month, and the first day of the month (0=Sun, 1=Mon, 2=Tue,...) and then print out the month properly formatted.