©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 4: Loops and Files
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
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.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis.
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.
Looping Yong Choi School of Business CSU, Bakersfield.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
CSC 204 Programming I Loop I The while statement.
Chapter 4: Loops and Files
Chapter 5 Loops.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Chapter The Increment and Decrement Operators There are numerous times where a variable must simply be incremented or decremented. number = number.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
Repetition Statements while and do while loops
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Lecture 3 Looping and FIiling. 5-2 Topics – The Increment and Decrement Operators – The while Loop – Using the while Loop for Input Validation – The do.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Loops and Files Starting Out with Java From Control Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
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.
Lecture 4b Repeating With Loops
Control Structures.
Loop Structures.
Lecture 4- Loops and Files
Chapter 5: Loops and Files.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Learning About the Loop Structure (continued)
Chapter 4: Loops and Files
Chapter 4: Loops and Files by Tony Gaddis and Godfrey Muganda
Chapter 4: Loops and Files
class PrintOnetoTen { public static void main(String args[]) {
while while (condition) { statements }
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Ch.4 – 5 Topics The auto Increment and Decrement Operators
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.
Presentation transcript:

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING While Loop

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4-2 The Increment and Decrement Operators There are numerous times where a variable must simply be incremented or decremented. number = number + 1; number = number – 1; Java provide shortened ways to increment and decrement a variable’s value. Using the ++ or -- unary operators, this task can be completed quickly. number++; or ++number; number--; or --number; Example: IncrementDecrement.javaIncrementDecrement.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. IncrementDecrement.java /** This program demonstrates the ++ and -- operators. */ public class IncrementDecrement { public static void main(String[] args) { int number = 4; // number starts out with 4 // Display the value in number. System.out.println("number is " + number); System.out.println("I will increment number.");

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. IncrementDecrement.java (cont) // Increment number. number++; // Display the value in number again. System.out.println("Now, number is " + number); System.out.println("I will decrement number."); // Decrement number. number--; // Display the value in number once more. System.out.println("Now, number is " + number); }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4-5 Differences Between Prefix and Postfix When an increment or decrement are the only operations in a statement, there is no difference between prefix and postfix notation. When used in an expression: –prefix notation indicates that the variable will be incremented or decremented prior to the rest of the equation being evaluated. –postfix notation indicates that the variable will be incremented or decremented after the rest of the equation has been evaluated. Example: Prefix.javaPrefix.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Prefix.java /** This program demonstrates the ++ and -- operators in prefix mode. */ public class Prefix { public static void main(String[] args) { int number = 4; // number starts out with 4 // Display the value in number. System.out.println("number is " + number); System.out.println("I will increment number.");

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. Prefix.java (cont) // Increment number. ++number; // Display the value in numbe again. System.out.println("Now, number is " + number); System.out.println("I will decrement number."); // Decrement number. --number; // Display the value in number once again. System.out.println("Now, number is " + number); }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4-8 The while Loop Java provides three different looping structures. The while loop has the form: while(condition) { statements; } While the condition is true, the statements will execute repeatedly. The while loop is a pretest loop, which means that it will test the value of the condition prior to executing the loop.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. 4-9 The while Loop Care must be taken to set the condition to false somewhere in the loop so the loop will end. Loops that do not end are called infinite loops. A while loop executes 0 or more times. If the condition is false, the loop will not execute. Example: WhileLoop.javaWhileLoop.java

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. WhileLoop.java /** This program demonstrates the while loop. */ public class WhileLoop { public static void main(String [] args) { int number = 1; while (number <= 5) { System.out.println("Hello"); number++; } System.out.println("That's all!"); }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved The while loop Flowchart statement(s) true boolean expression? false

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Infinite Loops In order for a while loop to end, the condition must become false. The following loop will not end: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); } The variable x never gets decremented so it will always be greater than 0. Adding the x-- above fixes the problem.

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Infinite Loops This version of the loop decrements x during each iteration: int x = 20; while(x > 0) { System.out.println("x is greater than 0"); x--; }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved Block Statements in Loops Curly braces are required to enclose block statement while loops. (like block if statements) while (condition) { statement; }

©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved The while Loop for Input Validation Input validation is the process of ensuring that user input is valid. System.out.print("Enter a number in the " + "range of 1 through 100: "); number = keyboard.nextInt(); // Validate the input. while (number 100) { System.out.println("That number is invalid."); System.out.print("Enter a number in the " + "range of 1 through 100: "); number = keyboard.nextInt(); }