Loops –Do while Do While Reading for this Lecture, L&L, 5.7.

Slides:



Advertisements
Similar presentations
IS Programming Fundamentals 1 (Lec) Date: September 14, Array.
Advertisements

Java Control Statements
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
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.
Introduction to Programming
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Programming Methodology (1). Implementing Methods main.
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.
Chapter 3 Flow of Control Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Building Java Programs
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
Loops – While, Do, For Repetition Statements Introduction to Arrays
18 File handling1June File handling CE : Fundamental Programming Techniques.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
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 Repetition structures Overview while statement for statement do while statement.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Outline The if Statement and Conditions Other Conditional.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
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.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Slides by Evan Gallagher
CSC111 Quick Revision.
using System; namespace Demo01 { class Program
Control Structures.
Chapter 6 More Conditionals and Loops
Computer Programming Methodology Input and While Loop
Programming Fundamentals
Repetition.
TK1114 Computer Programming
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Conditional Loops.
Debugging October 3, 2007 ComS 207: Programming I (in Java)
LRobot Game.
Java Language Basics.
Unit 3 - The while Loop - Extending the Vic class - Examples
Control Statements Loops.
Lec 4: while loop and do-while loop
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 }
Control Statements Loops.
Presentation transcript:

Loops –Do while Do While Reading for this Lecture, L&L, 5.7

The do Statement A do statement has the following syntax: The statement is executed once initially, and then the condition is evaluated The statement is executed repeatedly until the condition becomes false do { statement; } while ( condition );

The do Statement An example of a do loop: The body of a do loop executes one or more times (Note: at least once) See ReverseNumber.java (page 244)ReverseNumber.java int count = 0; do { count++; System.out.println (count); } while (count < 5);

The do-while Statement public class DoWhileDemo { //Execution always starts from main public static void main(String[] args) { System.out.println(“Enter a number:”); Scanner keyboard = new Scanner(System.in) int num = keyboard.nextInt(); int count = 0; do { count++; System.out.println(count); } while(count <= num); } do Loop_Body while(Boolean_Expression); The loop body may be either a single statement or more likely a compound statement

The do-while Statement true Start Execute Loop_Body do Loop_Body while(Boolean_Expression); false End loop Evaluate Boolean_Expression

The do-while Statement true false Start End loop do { System.out.print(count+ “,”); count++; } while(count <= num); Execute { System.out.print(count+ “,”); count++; } Evaluate count <= num

Difference between while and do-while Statements Do while -Do while statement is executed at least once -Condition is checked after executing statements in loop body While -While statement may be executed zero or more times -Condition is checked before executing statements in loop body

Break in Loops The break statement causes execution to “break” out of the repetitive loop execution (goes just outside the loop’s closing “}”) The effect of break statement on a loop is similar to effect on switch statement. The execution of the loop is stopped and the statement following the loop is executed.

Break in Loops int count = 1; while(count != 50) { count += 2; if(count % 2 == 1) break; }