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");

Slides:



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

Control Structures. Decision Making Structures The if and if…else are all selection control structures that introduce decision-making ability into a program.
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
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.
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.
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.
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.
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:
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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
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”);
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Java Programming: From the Ground Up
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
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.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Program Flow Program Flow follows the exact sequence of listed program statements, unless directed otherwise by a Java control structure.
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 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
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.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Repetition Statements while and do while loops
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.
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.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Final Jeopardy Fundamen tal Java Numerical Data type Boolean Expressi on If/THEN/ WHILE Miscellan eous
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
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.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
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.
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Control Structures.
Chapter 5: Control Structures II
Loop Structures.
Chapter 5: Control Structures II
Loops October 10, 2017.
Outline Altering flow of control Boolean expressions
More Loops.
Java Programming Loops
Module 4 Loops.
Lec 4: while loop and do-while loop
A LESSON IN LOOPING What is a loop?
Java Programming Loops
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
CS2011 Introduction to Programming I Loop Statements (I)
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 Strings – a class for representing sequence of letters Scanner – a class for reading input from keyboard

Scanner options a Scanner reads Strings by default – Scanner input = new Scanner(System.in); – String word = input.next(); a Scanner can also read int or double: – int age = input.nextInt(); – double value = input.nextDouble();

Scanner in Lab 3, 4 Lab Suggests the following steps to read a double: – Read a String String firstNum = input.next(); – Convert the String we just read to a (double) number: double num1 = Double.parseDouble( firstNum); It's much less confusing to just write: – double num1 = input.nextDouble(); // read double – int age = input.nextInt(); // read an int

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 < 7) { 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 = 7; 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) +"!"); }

Lab 4 Dr. Kow's Hamburger Palace Use loops to draw text graphic (while) read input and validate (do-while) you may just use lettuce = input.nextInt(); instead of String s = input.nextLine(); lettuce = Integer.parseInt(s);