While Statement.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
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.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
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.
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.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Chapter 5 Loops.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
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. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Iterative Statements: while, for, do-while Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006.
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Java Review if Online Time For loop Quiz on Thursday.
Building Java Programs Program Logic and Indefinite Loops.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
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.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Intro to Programming STARS College of Communication and Information Florida State University Written by: Hannah Brock Alissa Ovalle Nicolaus Lopez Martin.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Sophomore Scholars Java
CSC111 Quick Revision.
REPETITION CONTROL STRUCTURE
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CS161 Introduction to Computer Science
Lecture 7: Repeating a Known Number of Times
Chapter 6 More Conditionals and Loops
Loop Structures.
Switch Statement.
Chapter 5: Loops and Files.
Repetition-Counter control Loop
Repetition.
Char data type and the String class.
Data types, Expressions and assignment, Input from User
Chapter 5: Control Structures II
TK1114 Computer Programming
Comp Sci 200 Programming I Jim Williams, PhD.
Something about Java Introduction to Problem Solving and Programming 1.
Boolean Expressions And if…else Statements.
Control Statement Examples
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Conditional Loops.
Java Language Basics.
Unit 3 - The while Loop - Extending the Vic class - Examples
Lec 4: while loop and do-while loop
Lecture Notes – Week 2 Lecture-2
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
A LESSON IN LOOPING What is a loop?
Repetition Statements
Building Java Programs
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.
Loops CGS3416 Spring 2019 Lecture 7.
Presentation transcript:

while Statement

Objectives: Learn the Java syntax for while loops Learn about the Java increment and decrement operators.

Iterations It is essential that a program be able to execute the same set of instructions many times: otherwise a program would do only as much work as a programmer! Repeating the same code fragment several times is called iterating. Java provides three control statements for iterations (a.k.a. loops): for, while, and do-while. Iterations are what makes a program different from a cookbook recipe.

The while Loop condition is any logical expression, as in if while ( condition ) { statement1; statement2; ... statementN; } The body of the loop Many people prefer to always use braces, as it is easier to add statements to the body of the loop. If the body has only one statement, the braces are optional while ( condition ) statement1;

The while Loop (cont’d) Example: // Prints “Hello World” ten times int p = 0; while ( p < 10 ) { System.out.print(“Hello World”); p++; } Initialization Test Increment When we say “increment” we really mean any change in the variables tested in the condition.

The while Loop (cont’d) Initialization: the variables tested in the condition must be initialized to some values. If the condition is false at the outset, the loop is never entered. Test: the condition is tested before each iteration. If false, the program continues with the first statement after the loop. Increment (change): At least one of the variables tested in the condition must change in the body of the loop.

Common while loop Errors { statements; ... } Extra semicolon It is safer to always use braces in while loops while (...) statement1; statement2; ... Missing braces

Increment and Decrement Operators Symbol Purpose Example Result Increment ++ increment by 1 int n = 5; out.print(n++); 5 n == 6 out.print(++n); 6 Decrement -- decrement by 1 out.print(n--); n == 4 out.print(--n); 4

Top Down Design

Top-Down Design (cont’d) Start JCreator. Open the file “Lab07.java”. Lab08.java is in your Lab07 folder.

Top-Down Design (cont’d) Write a program that reads two integers from the keyboard and outputs all the ASCII values from the first number up to the second number.

Top-Down Design (cont’d) import java.util.Scanner; public class Lab07 { public static void main(String[ ] args) { Lab08 lab = new Lab08( ); lab.input(); // Enter data from the kybd lab.output( ); // Display output }

Top-Down Design (cont’d) input(), process() and output( ) are the smaller parts of the problem.

Top-Down Design (cont’d) What fields are required to solve the problem? NOTE: Fields should be the values we will read from the keyboard. int start; int end;

Top-Down Design (cont’d) import java.util.Scanner; public class Lab07 { private int start; private int end; public static void main(String[ ] args) Lab07 lab = new Lab07( ); lab.input(); // Enter data from the kybd lab.output( ); // Display output }

Top-Down Design (cont’d) input Prompt the user to enter a sentence then read the sentence from the keyboard. Enter the starting value: 65 Enter the ending value: 70

Top-Down Design (cont’d) public void input() { Scanner reader = new Scanner(System.in); System.out.print("Enter the starting value: "); start = reader.nextInt(); System.out.print("Enter the ending value: "); end = reader.nextInt(); }

Top-Down Design (cont’d) output We want to display all the ASCII values from start to end. 65 = A 66 = B 67 = C 68 = D 69 = E 70 = F Do you see the loop? Something that is progressively growing or shrinking.

Top-Down Design (cont’d) public void output() { }

Top-Down Design (cont’d) public void output() { int ascii = start; }

Top-Down Design (cont’d) public void output() { int ascii = start; while ( ) { }

Top-Down Design (cont’d) public void output() { int ascii = start; while (ascii <= end) { }

Top-Down Design (cont’d) public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); }

Top-Down Design (cont’d) public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) Run The Program: Enter the starting value: Enter the ending value: 255

Top-Down Design (cont’d)

Top-Down Design (cont’d)

Top-Down Design (cont’d)

Top-Down Design (cont’d)

Top-Down Design (cont’d)

How does a while loop work?

Top-Down Design (cont’d) public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; } start = 65 end = 68

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; } start = 65 end = 68

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= start output 65 68 true 65 = A 66 66 = B 67 67 = C 68 = D public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C 68 = D 69 public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C 68 = D 69 false public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; }

Top-Down Design (cont’d) ascii end acsii <= end output 65 68 true 65 = A 66 66 = B 67 67 = C 68 = D 69 false public void output() { int ascii = start; while (ascii <= end) { System.out.println(ascii + " = " + (char) ascii); ascii++; } OUTPUT 65 = A 66 = B 67 = C 68 = D

Questions?

Begin Lab 07