Learning Plan 4 Looping.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

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.
Chapter 6 – Lots of Loops! September 28, Boolean expression Loop body.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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 (!)
Looping Yong Choi School of Business CSU, Bakersfield.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
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.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
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 Looping 5. The Increment and Decrement Operators 5.1.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
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.
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.
Follow up from lab See Magic8Ball.java Issues that you ran into.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Control Structures.
Lesson 05: Iterations Class Chat: Attendance: Participation
Chapter 5: Control Structures II
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
Learning About the Loop Structure (continued)
Chapter 6 – Lots of Loops! September 28, 2010.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 5 Repetition.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Repetition Statements
Outline Altering flow of control Boolean expressions
Chapter 4: Loops and Files
Repetition Control Structure
Chapter 6: Repetition Statements
Let’s all Repeat Together
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
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:

Learning Plan 4 Looping

Loop Structure Decision-making makes programs seem smart Looping makes programs powerful A loop – repeated execution of a block of statements Like if statements, a boolean expression is evaluated If it’s true, a block of statements called the loop body executes and boolean expression evaluated again As long as expression is true, the loop body continues to execute Three types of loops – while, for , and do/while

While Loops Can be used to execute a specified number of times Need to use a counter variable (called a loop control variable Counter variable needs to be incremented/decremented Or the number of times it executes might not be determined until the program is running Called an indefinite While loop

Infinite Loop

While Loop Example

While Loop Problem Example

Using Shortcut Arithmetic Operators loopCount = 1; While(loopCount <3) { System.out.println(“Hello); loopCount = loopCount + 1; } ******************************* loopCount ++ // means to add 1 – called incrementing

Shortcuts - continued loopCount += 1; // also means to add 1 loopCount ==; // means to subtract 1

Indefinite While Loop Often the value of a loop control variable is not altered by adding or subtracting from it, but instead altered by user input Called indefinite because you don’t know how many times it will eventually loop EX: you want to continue performing some task as long as the user indicates a desire to continue You won’t know if the loop will eventually be executed two times or 200 times or not at all

Indefinite While Loop Example Double bankBalance = 1276.23; Int userSelection = Integer.parseInt(JOptionPane.showInputDialog(null, “Show Bank Balance (1 for YES, 2 for NO)”)); While (userSelection == 1) { JOptionPane.showMessageDialog(null, “Available balance is “ + bankBalance); userSelection = Integer.parseInt(JOptionPane.showInputDialog (null, “Show Bank Balance (1 for YES, 2 for NO)”)); } JOptionPane.showMessageDialog(null, “Thanks for using the program”);

Another Example Int password=Integer.parseInt(JOptionPane.showInputDialog (null, “Enter Password”)); While (password !=12345678) { password=Integer.parseInt(JOptionPane.showInputDialog(null, “Enter Password”)); } JOptionPane.showMessageDialog(null, “Welcome to the Site!”);

For Loops Can accomplish the same thing that a while loop can, but typically used only when you have a counter-controlled loop Same as While, but shorter code. Both do the same, but in different order/code Initialize the loop control variable Test the loop control variable Update the loop control variable

For Loops For (int x =1; x<11; x++) { System.out.println(x); } ***************** int x =1; While(x<11) x ++

Do/While Loops All examples thus far showed that the loop body might execute many times However, it is possible that the loop will not execute at all Conditional statement is checked BEFORE the loop body The do/while has a conditional statement AT THE END of the loop body, guaranteeing the loop occurs at least once

Int password=Integer. parseInt(JOptionPane Int password=Integer.parseInt(JOptionPane.showInputDialog (null, “Enter Password”)); While (password !=12345678) { password=Integer.parseInt(JOptionPane.showInputDialog(null, “Enter Password”)); } JOptionPane.showMessageDialog(null, “Welcome to the Site!”); ************** Do While (password !=12345678); JOptionPane.showMessageDialog(null, “Welcome to the site!”);

Nested Loops Just as if statements can be nested, so can loops For (int x =0; x<4; x++) { for (int y =0; y<4; y++) System.out.println(x + “, “ + y); } Can you guess what output will be???

0, 0 0, 1 0, 2 0, 3 1, 0 1, 1 1, 2 1, 3 2, 0 2, 1 2, 2 2, 3 3, 0 3, 1 3, 2 3, 3 For (int x =0; x<4; x++) { for (int y =0; y<4; y++) System.out.println(x + “, “ + y); }