Loops A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Computer Science 1620 Loops.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
COMP 14 Introduction to Programming Mr. Joshua Stough February 16, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
CPS120 Introduction to Computer Science Iteration (Looping)
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
CONTROLLING PROGRAM FLOW
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
CPS120 Introduction to Computer Science Iteration (Looping)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
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.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
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.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Mobile Development Workshop
Chapter 4 Repetition Statements (loops)
Loops in Java.
CS161 Introduction to Computer Science
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Web Programming– UFCFB Lecture 16
Beginning C Lecture 4 Lecturer: Dr. Zhao Qinpei
Control Structure Senior Lecturer
Outline Altering flow of control Boolean expressions
LRobot Game.
More Loops.
Selection Statements.
A LESSON IN LOOPING What is a loop?
Flow of Control.
Repetition Statements (Loops) - 2
Another Example Problem
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.
Repetition Structures
CS2011 Introduction to Programming I Loop Statements (I)
CSCI 1100/1202 February 6, 2002.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Problem 1 Given n, calculate 2n
Looping and Repetition
Presentation transcript:

Loops A portion of a program that repeats a statement or a group of statements is called a loop. The statement or group of statements to be repeated is called the body of the loop. A loop could be used to compute grades for each student in a class. There must be a means of exiting the loop.

The while Statement Also called a while loop A while statement repeats while a controlling boolean expression remains true The loop body typically contains an action that ultimately causes the controlling boolean expression to become false.

The while Statement Syntax while (Boolean_Expression) Body_Statement or { First_Statement Second_Statement … }

The while Statement

The do-while statement Also called a do-while loop Similar to a while statement, except that the loop body is executed at least once Syntax do Body_Statement while (Boolean_Expression); Don’t forget the semicolon!

The for Statement A for statement executes the body of a loop a fixed number of times. Example for (count = 1; count <= 10; count++) System.out.println(count);

The for Statement Syntax Corresponding while statement for (Initialization, Condition, Update) Body_Statement Body_Statement can be either a simple statement or a compound statement in {}. Corresponding while statement Initialization while (Condition) Body_Statement_Including_Update

The for Statement

Matching Game Algorithm Ask the user to play the game or quit If the user decides to play Generate 3 random numbers If all 3 numbers match, display the message “You Win!” If any two numbers match, display the message “Matched 2” Otherwise, display the message “Sorry. Better luck next time” Repeat until the user quits