 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

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?
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
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.
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.
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.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
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.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
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.
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.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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,
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
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 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Fundamental Programming Fundamental Programming More on Repetition.
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)
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
 Data Streams  Numeric Output  Numeric Input  Multiple Numeric Output  Multiple Numeric Input  Character Output  Character Input  String Output.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
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.
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.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Welcome to Who Wants to be a Millionaire
CS161 Introduction to Computer Science
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Loops October 10, 2017.
Iteration with While You can say that again.
Repetition Statements
One-Dimensional Array Introduction Lesson xx
File I/O with Records Lesson xx
Value returning Functions
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Control Structures Part 1
Looping III (do … while statement)
do/while Selection Structure
Repetition Statements (Loops) - 2
(Dreaded) Quiz 2 Next Monday.
Presentation transcript:

 for loop  while loop  do-while loop

for (begin point; end point ; incrementation ) { //statements to be repeated }

for (c = 0; c < 10 ; c++ ) { cout << “You get no bull from this Kow\n”; }

while ( condition ) { //statements to be repeated }

int x = 1; while ( x < 10 ) { cout << x << “ “ << x*x << “\n”; x = x +1; }

do { //statements to be repeated } while ( condition );

float inch = 1.0; do { cout << inch << “ inch = “ << inch * 2.54 << “ cm\n”; inch = inch + 1.0; } while ( inch < 10 );

1.Read in a line of text followed by a return. 2.Count the number of characters in the line. 3.Repeat steps 1 & 2 as many times as the user desires.

again Promt for sentence Initialize length to 0 Read char. into ch ch != ‘\n’ Increment length by 1 Read next char. into ch ch Print length of text again == ‘y’ Prompt and read into again

#include using std::cin; using std::cout; using std::endl; int main() { char ch, again; int length; do { cout." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

#include using std::cin; using std::cout; using std::endl; int main() { char ch, again; int length;

do { cout." << " I'll tell you length" << endl; length = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

do { cout." << " I'll tell you length" << endl; length = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

do { cout." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

do { cout." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

do { cout." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

do { cout." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

do { cout." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

1.A for loop is generally used when you know how many times you want to repeat a section of code. 2.When you don’t know how many times you want to repeat a section of code but you want to specify a condition in which the code is to be repeated, use a while or a do-while loop. 3.A do-while loop is used when you want the code to be executed at least once. In a do- while, the test is done at the end of the loop 4.In a while loop, the condition is checked at the beginning of the loop. If the condition is false, the body of the loop will never be executed.

 for loop  while loop  do-while loop