Looping Yong Choi School of Business CSU, Bakersfield.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
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
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
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.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Java Programming: From the Ground Up
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Loops and Files
Using Shortcut Arithmetic Operators Accumulator: A variable that is used to total. Its value is repeatedly increased by some amount. Java provides shortcuts.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
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.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Control Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
JavaScript: Control Statements.
Learning About the Loop Structure (continued)
Java I.
Outline Altering flow of control Boolean expressions
Control Statements Loops.
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
Learning Plan 4 Looping.
Control Statements:.
Presentation transcript:

Looping Yong Choi School of Business CSU, Bakersfield

Objectives Learn about the loop structure Use a while loop Use shortcut arithmetic operators Use a for loop Learn how and when to use a do…while loop Learn about nested loops

Learning about the Loop Structure Loop: A structure that allows repeated execution of a block of statements Loop body: A block of statements; as long as the expression is true, the loop body executes Iteration- One execution of any loop

Using the while Loop while Loop: execute a body of statements continually as long as the Boolean expression continues to be true –Consists of the keyword while followed by a Boolean expression within parentheses followed by the body of the loop –Use when you need to perform a task a predetermined number of times

Using a while Loop Incrementing – Altering a loop by adding one to loop control variable Decrementing – Altering a loop by subtracting one from a loop control variable

While Loop Example public class loopExample { public static void main (String[] args ) { int count = 1; // start count out at one while ( count <= 3 ) // loop while count is <= 3 { System.out.println( "count is:" + count ); count = count + 1; // add one to count, same as (count++) } System.out.println( "Done with the loop" ); } }

Syntax of the while statement while ( condition ) loop body // a statement or block statement When the condition is true, the loop body is exectued. When the condition is false, the loop body is skipped, and the statment after the loop is executed. Once execution has passed to the statement after the loop, the while statement is finished, at least for now. If the condition is false the very first time it is evaluated, the loop body will not be executed even once.

Using Shortcut Arithmetic Operators To increase a variable’s value by exactly one: prefix ++ –Used before the variable name ++someValue; postfix ++ (recommend) –Used after the variable name anotherValue++;

Counting Upwards by Two's int count = 0; // count is initialized while ( count <= 6 ) // count is tested { System.out.println( "count is:" + count ); count = count + 2; // count is changed by 2 } System.out.println( "Done counting by two's." );

Decrementing the Loop Control Variable The loop control variable in a counting loop can be changed by a negative value. Here is a program fragment that decrements the loop control variable at the bottom of each iteration: int count = 2; // count is initialized while ( count >= 0 ) // count is tested { System.out.println( "count is:" + count ); count = count - 1; // count is changed by -1 } System.out.println( "Done counting down." );

Infinite Loop What’s the result of below loop? int count = 13; int decrement = -1; while ( count >= 0 ) { System.out.println( "count is:" + count ); count = count - decrement; } System.out.println( "Count was " + count + " when it failed the test");

Using a for Loop For loop: A special loop that is used when a definite number of loop iterations is required Keyword for Use a set of parentheses Three sections within parentheses –Initializing the loop control variable –Testing the loop control variable –Updating the loop control variable

Example of For Statement public class loopExample { public static void main (String[] args ) { int count, sum; sum = 0; for ( count = 0; count <= 5; count++ ) { sum = sum + count ; System.out.print( count + " " ); } System.out.println( "sum is: " + sum );

Syntax of for Statement Java (and several other languages) has a for statement which combines the three aspects of a loop into one statement. In general, it looks like this: for ( initialize ; test ; change ) loopBody ; The initialize, test, and change are statements or expressions that (usually) perform the named action. The loopBody can be a single statement or a block statement. Here is an example of a for statement: for ( count = 0; count < 10; count++ ) System.out.print( count + " " );

Side By Side for loop int count, sum; sum = 0; for ( count = 0; count <= 5; count++ ) { sum = sum + count ; System.out.print( count + " " ); } System.out.println( "sum is: " + sum ); While loop int count, sum; sum = 0; count = 0; while ( count <= 5 ) { sum = sum + count ; System.out.print( count + " " ); count++ ; } System.out.println( "sum is: " + sum );

Using a do…while Loop The while loop can be used to implement any loop. However, the for loop is very convenient. The do…while loop is occasionally convenient. –Of the three looping statements, it is used the least. Some programmers prefer not to use it at all.

Learning How and When to Use a do…while loop Checks at the bottom of the loop after one repetition has occurred –Bottom-driven loop Loop body executes at least one time The loop starts with the keyword do The body of the loop is contained within curly braces

Example of do…while Statement int count = 0; // initialize count to 0 do { System.out.println( count ); // loop body: includes code to count++ ; // change the count } while ( count < 10 ); // test if the loop body should be // executed again.

while Loop with Alternatives import java.io.* ; public class SqrtCalc { public static void main( String[] args ) throws IOException { String chars ; double x; BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in) ); chars = "yes" ; while ( chars.equals( "yes" ) || chars.equals( "YES" ) || chars.equals( "y" ) || chars.equals( "Y" ) ) { System.out.print("Enter a number-->"); chars = stdin.readLine(); x = (Double.valueOf(chars)).doubleValue(); System.out.println("Square root of " + x + " is " + Math.sqrt( x ) ); System.out.print("Do you wish to continue? (yes or no) -->"); chars = stdin.readLine(); } } }

Nested Loops Loops can be nested much like if statements You can place a while loop within a while loop, a for loop within a for loop, a do…while loop within a do…while loop, or use any combination of these loops Try the program on page 196 – 198