While and Do While Loops

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Lecture 4 More on Java® Data Types, Control Structures.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
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.
Lecture 8 Sept 25, Till now ► I► I► I► Introduction to computers ► S► S► S► Simple Programs using basic concepts like variables and data types,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Introduction to C Programming CE Lecture 4 Further Control Structures in C.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Count and add list of numbers From user input and from file.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
COMP Loop Statements Yi Hong May 21, 2015.
Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
UCT Department of Computer Science Computer Science 1015F Iteration
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Loops A loop is: Java provides three forms for explicit loops:
C++ Iterative Constructs
Chapter 6 More Conditionals and Loops
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Chapter 4 Loops Case Studies
Arrays, For loop While loop Do while loop
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
While Loop Design ENGI 1020 Fall 2018.
Introduction to Algorithms and Programming COMP151
Control Structures Repetition
LOOPS BY: LAUREN & ROMEO.
Control structures to direct program flow
Chapter 2.2 Control Structures (Iteration)
What output is produced by the following fragment?
A LESSON IN LOOPING What is a loop?
Debugging October 4, 2006 ComS 207: Programming I (in Java)
while while (condition) { statements }
More Loops Topics Counter-Controlled (Definite) Repetition
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
Chap 7. Advanced Control Statements in Java
CSCI 1100/1202 February 6, 2002.
Chapter 4 Repetition Structures
Programming Fundamental
Looping Structures.
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

While and Do While Loops By Ms. Bellacera

While Loop Form while (condition) { statement; } //executes an //unknown //number of times int z=0; while (z<10) { z=z+1; } cout<<“z is “<<z<<endl; What gets printed?

Do While Loop Form int num, sum=0; do { cout<<“enter the number”<<endl; cin<<num; sum=sum+num; } while (num!=0); cout<<“sum is “<<sum<<endl; do { statement; } while (condition); //executes //at least once What’s this? What gets printed?

While versus For Loops initialization while (condition) { statement incrementation } for (initialization; condition; incremenatation) { statement . }

While/For Loops Questions 1. Write the while loop equivalent to the following loop. double total = 0.0; double val; for (val=1.0; val<1000; val*=1.5) { total +=val; } 2. Write a for loop equivalent to the following while loop. int k=1; int sum=0; while (k<=num) sum+=k; k+=2;

While versus For Loops initialization int z=0; while (z<10) { z++; } cout<<“z is“<<z<<endl; condition for (int z=0; z<10; z++) { } cout<<“z is “<<z<<endl; incrementation

Create the Factorial Program Produce a listing of factorials starting at 0! Continue until the value of the factorial goes over 4,000,000. Show the factorial and its value on each iteration. (ie. 0! = 1 1! = 1 2! = 2 . )