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,

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

CS0007: Introduction to Computer Programming
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Fundamentals of Python: First Programs
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java Programming: From the Ground Up
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Chapter 5 Loops.
Flow of Control Part 1: Selection
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
TOPIC 10 THE IF STATEMENT 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Review if Online Time For loop Quiz on Thursday.
Georgia Institute of Technology Conditionals – part 2 Barb Ericson Georgia Institute of Technology August 2005.
Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CS0007: Introduction to Computer Programming
Lecture 4b Repeating With Loops
CSC111 Quick Revision.
Georgia Institute of Technology
Repetition (While-Loop) version]
Control Statements: Part 2
Chapter 6 More Conditionals and Loops
Repetition.
Barb Ericson Georgia Institute of Technology August 2005
Building Java Programs
Building Java Programs
Conditional Loops.
Outline Altering flow of control Boolean expressions
Georgia Institute of Technology
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Lecture Notes – Week 2 Lecture-2
CSC 1051 – Data Structures and Algorithms I
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.
CSS161: Fundamentals of Computing
Presentation transcript:

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, and instructor materials prepared by B. Ericson.

Outline 2  How to use logical operators  How to use while loops in general  How to do keyboard input

While Loops 3  Recall that the basic syntax for a while loop is: while (test) { body of loop } where  test is a condition that is true or false  body of the loop consists of the statements to be executed while the condition is true

While loops 4  We will now look at while loops that do not just involve counting  Our tests will be more general than just checking the end value for a counter, as in a previous example: int total = 0; int counter = 1; while (counter <= 100) { total = total + counter; counter = counter + 1; } …

Recall: Relational Operators 5  Relational operators  >>=<<===!=  Compare two operands of the same type  Relational expressions  Express a condition that evaluates to true or false  Example: (counter <= 100)  Sometimes we want to test for several conditions being true  We can combine relational expressions using logical operators

Logical Operators 6  and operator &&  Used to check if several things are true  Example: (x > 0) && (x <100) Expression evaluates to true if both (x >0) and (x < 100)  Short circuiting : evaluation stops if the first condition turns out to be false Why does Java do this?

Logical Operators 7  or operator ||  Used to check if at least one of several things is true  Example: (x > 0) || (x <100) Expression evaluates to true if either (x >0) or (x < 100)  Java does short circuiting for || also

Logical Operators 8  Exclusive or operator ^  Used to check if one and only one of the things is true  Example: (x < 0) ^ (y <0) Expression evaluates to true if either (x < 0) or (y < 0) but not both  Can this be short-circuited?

Logical Operators 9  not operator !  Used to change the value of a condition to its opposite !true is false !false is true  Example: !(x == y)  Expression evaluates to true if (x == y) is false, evaluates to false if (x ==y) is true

Truth Tables for && and || in Java 10 ABA&&B true false anyfalse ABA||B trueanytrue falsetrue false

Using && (and) and || (or) 11  Check that a value is in a range  Example: check for valid pixel color values Is some value between 0 and 255 inclusive? 0 <= x <= 255 is written in Java as 0 <= x && x <= 255 or x >= 0 && x <= 255  Check if at least one of several things is true  Example: Is the color black or white?

Keyboard Input 12  The textbook provides a class called SimpleInput  In the bookClasses folder  It contains methods that make it easy for use to input data from the keyboard:  getNumber for getting doubles  getIntNumber for getting integers  getString for getting strings  They are class methods (not object methods)

SimpleInput Methods 13  public static double getNumber(String message)  The parameter message is the prompt message to display to the user  The input window will keep appearing until a valid number type is input  The number typed by the user is returned as a double

SimpleInput Methods 14  public static int getIntNumber(String message)  The number typed by the user is returned as an int  public static String getString(String message)  The data typed by the user is returned as a String

While Loop Example 15  Suppose we want a user to enter a number between 0 and 255 inclusive. If the number entered is not in the correct range, the user should be asked again to enter the number: int number = -1; while ( number 255) { number = SimpleInput.getIntNumber("Enter a number between 0 and 255 inclusive: "); }

While Loop Example 16  What will happen if the user enters the number 300? the number -2?  Why is the variable number initialized to -1 and not to 0?

Keyboard and while 17  Write a program that takes an image and changes the red value of all pixels to some specific value  Exercise: write a method makeRed for the Picture class with the header public void makeRed(int redValue)  Sample main program: public class TestRed{ public static void main(String args[ ]) { int redValue = 100; Picture p = new Picture(FileChooser.pickAFile()); p.explore(); p.makeRed(redValue); p.explore(); } }

Continued 18  Now what if we wanted to see what the picture looks like with the red values set to 150? Or 200?  We would have to change the statement int redValue = 100; to int redValue = 150; etc.  We would have to recompile the program before we could run it again  A more flexible way: ask the user to enter the new red value on the keyboard  And also check that the input is valid!

19 public class TestRed { public static void main(String args[]) { int redValue = -1; Picture p = new Picture(FileChooser.pickAFile()); p.explore(); while ( redValue 255) { redValue = SimpleInput.getIntNumber( " Enter a number between 0 and 255 inclusive: " ); } p.makeRed(redValue); p.explore(); } } Continued