More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }

Slides:



Advertisements
Similar presentations
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.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
An introduction to arrays
Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
1 Repetition structures Overview while statement for statement do while statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
UNIT II Decision Making And Branching Decision Making And Looping
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
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Chapter 4: Loops and Files
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Chapter 5 Loops.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
While ( number
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
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.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Three kinds of looping structures The while loop The for loop The do (also called the do-while) loop All have equivalent power, e.g., if you can write.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
The switch Statement, and Introduction to Looping
Control Structures.
Chapter 5: Control Structures II
Repetition-Sentinel,Flag Loop/Do_While
Loop Control Structure.
Iteration with While You can say that again.
LRobot Game.
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Presentation transcript:

More loops while and do-while

Recall the for loop in general for (initialization; boolean_expression; update) { }

while loop in general while (condition) { statement 1 ; statement 1 ; statement 2 ; statement 2 ;... statement n ; statement n ;}

while loop in general while (condition) single-statement; single-statement;

do-while loop in general do { //one or more statements here //one or more statements here statement 1 ; statement 1 ;... statement n ; statement n ; } while (condition);

while loop vs. do-while loop while (condition) { …}// do { … } while (condition); What’s the difference?

while loop vs. do-while loop while (condition) { …}// do { … } while (condition); The body of the do-while is always executed at least one time (regardless of the condition)!

while loop vs. do-while loop while (condition) { …}// do { … } while (condition); Minor difference: If the body of the while-loop contains only a single statement, one doesn’t need to declare a block. The do-while always requires the declaration of a block.

What other statements included a condition? What is a condition?

Russian Roulette game Let chamber #1 be the chamber with the shell. All other chambers are empty. Let chamber #1 be the chamber with the shell. All other chambers are empty. You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise... You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise... Output should be something like: Output should be something like: 1: click!1: click! 1: click!1: click! 2: click!… 2: click!… 3: Bang!10: click! 3: Bang!10: click! You’re dead!You win! You’re dead!You win!

import java.util.Random; /* Let chamber #1 be the chamber with the shell. All other chambers are empty. You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise... Output should be something like: 1: click! 2: click! 3: Bang! You’re dead! */ class RussianRoulette { public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game ? //check the results //check the results if ( ? ) System.out.println( "You win!" ); if ( ? ) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }}

class RussianRoulette { public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; ? //check the results //check the results if ( !dead ) System.out.println( "You win!" ); if ( !dead ) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }}

class RussianRoulette { public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; while (!dead) { while (!dead) { ? } //check the results //check the results if (!dead) System.out.println( "You win!" ); if (!dead) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }}

public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; while (!dead) { while (!dead) { switch (r.nextInt() % 6 + 1) { switch (r.nextInt() % 6 + 1) { case -1: case -1: case 1: case 1: System.out.println( i + ": Bang!" ); System.out.println( i + ": Bang!" ); dead = true; dead = true; break; break; default: default: System.out.println( i + ": click!" ); System.out.println( i + ": click!" ); break; break; } } //check the results //check the results if (!dead) System.out.println( "You win!" ); if (!dead) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); } This is good but it keeps pulling the trigger until the inevitable eventuall happens!

public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; int i = 0; int i = 0; while (!dead && i<N) { while (!dead && i<N) { switch (r.nextInt() % 6 + 1) { switch (r.nextInt() % 6 + 1) { case -1: case -1: case 1: case 1: System.out.println( i + ": Bang!" ); System.out.println( i + ": Bang!" ); dead = true; dead = true; break; break; default: default: System.out.println( i + ": click!" ); System.out.println( i + ": click!" ); break; break; } i++; i++; } //check the results //check the results if (!dead) System.out.println( "You win!" ); if (!dead) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }

import java.util.Random; /* Let chamber #1 be the chamber with the shell. All other chambers are empty. You get 10 tries. If in 10 tries you never get the shell, you win! Otherwise... Output should be something like: 1: click! 2: click! 3: Bang! You’re dead! */ class RussianRoulette { public static void main ( String args[] ) { public static void main ( String args[] ) { //declare vars //declare vars //declare our random number generator. //declare our random number generator. //1..6 = one for each chamber //1..6 = one for each chamber Random r = new Random(); Random r = new Random(); final int N = 10; //max number of tries final int N = 10; //max number of tries //play the game //play the game boolean dead = false; boolean dead = false; for (int i=1; i<=N && !dead; i++) { for (int i=1; i<=N && !dead; i++) { switch (r.nextInt() % 6 + 1) { switch (r.nextInt() % 6 + 1) { case -1: case -1: case 1: case 1: System.out.println( i + ": Bang!" ); System.out.println( i + ": Bang!" ); dead = true; dead = true; break; break; default: default: System.out.println( i + ": click!" ); System.out.println( i + ": click!" ); break; break; } } //check the results //check the results if (!dead) System.out.println( "You win!" ); if (!dead) System.out.println( "You win!" ); else System.out.println( "You're dead!" ); else System.out.println( "You're dead!" ); }} Another version using a for- loop. One could use a do- while as well.