Lec 4: while loop and do-while loop

Slides:



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

Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Console Input Using the Scanner Class Starting with version 5.0, Java includes a class for doing.
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.
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.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Java Programming: From the Ground Up
Lecture 2: Classes and Objects, using Scanner and String.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 5 Loops.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Repetition Statements while and do while loops
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Slides prepared by Rose Williams, Binghamton University Console Input and Output.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
UCT Department of Computer Science Computer Science 1015F Iteration
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
CSC111 Quick Revision.
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Control Structures.
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Loop Structures.
User input We’ve seen how to use the standard output buffer
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Repetition.
Chapter 5: Control Structures II
Control Statement Examples
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
While Statement.
Flow Control in Java.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Loops October 10, 2017.
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
More Loops.
More Loops.
Java Programming Loops
Module 4 Loops.
Do … Loop Until (condition is true)
Loop Strategies Repetition Playbook.
Lecture Notes – Week 2 Lecture-2
A LESSON IN LOOPING What is a loop?
Java Programming Loops
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
Lec 6 Logical Operators String comparison
Announcements Lab 3 was due today Assignment 2 due next Wednesday
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

Lec 4: while loop and do-while loop

Review from last week if Statements if (num < 0.5){ ...println("heads"); } if –else Statements if (num < 0.5){ ...println("heads"); } else { ...println("tails");

Review from last week if –else if Statements if (n < 0.4){ ...println("heads"); } else if (n <0.8){ ...println("tails"); else if (n < 0.85){ ...println("edge"); else { ... println("fell off table");

Review from last week How to store text data in a variable? make String variable String message = new String("Hello world"); System.out.println( message ); How to read input from keyboard?

Use a Scanner object! Scanner input = new Scanner(System.in); like a vending machine w/ three buttons next() nextInt() nextDouble() input

Scanner options Create a Scanner object and connect to keyboard Scanner input = new Scanner(System.in); // make it only make one Scanner object in your program near top of program, before you read anything a Scanner reads Strings using the next method String word = input.next(); // read a String a Scanner can also read int or double: int age = input.nextInt(); // read an int double value = input.nextDouble(); // read a double

while loops A while loop is a way of repeating a set of instructions as long as some condition is met In general, a while loop looks like this: while(condition) {         statements } The condition must be a boolean expression There can be any number of statements inside the loop Sort of like ``an if statement that keeps repeating until the condition becomes false''

A while loop example int i = 0; while(i < 5) { System.out.println(i); i = i + 1; } System.out.println(); System.out.println("i=" + i); i (i<7) Console

Flowchart of previous slide

Processing a while loop Java does the following when it meets a while loop: 1) Check to see if the condition is true 2) If the condition was false, bail out of the loop, otherwise go to 3) 3) Execute the body of the while loop and go back to 1)

Another while loop example int i = 4; while(i > 0){ System.out.println(i); i = i - 1; } i (i>0) Console

Some common loop errors False from the get-go int i = 0; while(i > 7) {     System.out.println(i);     i = i + 1; } nothing is printed!

Never terminates int i = 0; while(i < 7) { System.out.println(i); i = i - 1; } This is an "infinite loop" In BlueJ, R-click red run indicator, reset JVM

Another no-no loop int i = 0; while(i < 7) ; { System.out.println(i); i = i + 1; } This is an "infinite loop" In BlueJ, R-click red run indicator, reset JVM

What will this loop output? int i = 10; while(i < 30) {     System.out.println(i);     i = i + 5; } i (i<30) Console

Another shortcut Programmers are very lazy Instead of i = i + 1; we say i++; instead of i = i – 1; we say i --; instead of i = i + 5; we say i +=5; You can experiment with these in Lab 4 if you like

do loop A do loop is very much like a while loop, except that the condition is tested at the end of the loop instead of the beginning Looks like this: do {         statements } while(condition); The body of the loop is always executed the first time through, since the condition isn't checked until the end of the loop

How many times does the loop cycle? int i = 0; while(i > 0) { i = i - 1; } System.out.println(i); // prints 0

Very Similar, only do-while i=0; do { i = i - 1; }while(i > 0); System.out.println(i); // prints -1 do-while always executes at least once

Example: User Input loop // Ask for age until a valid number is entered Scanner input = new Scanner(System.in); int age; do { System.out.println("What's your age?"); age = input.nextInt(); }while (age < 0 ); //loop back as long as input is bad System.out.println("Oh yeah? Well I'm " + (age+1) +"!"); }

Flowchart of previous slide

Lab 4 Dr. Kow's Hamburger Palace Use loops to draw text graphic (while) read input and validate (do-while) use a Scanner to read input lettuce = input.nextInt();