Catie Welsh February 9, 2011 1.  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?
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.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
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
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
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.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
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,
Flow of Control Chapter 3 Flow of control Branching Loops
Chapter 4: Control Structures II
Repetition Statements while and do while loops
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
While ( number
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
CiS 260: App Dev I Chapter 4: Control Structures II.
Iteration with While You can say that again.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
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.
LOOPS BY: LAUREN & ROMEO.
Repetition Statements (Loops) - 2
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
PROGRAM FLOWCHART Iteration Statements.
Looping and Repetition
Presentation transcript:

Catie Welsh February 9,

 Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2

3

◦ Use a switch statement when you have more than 2 conditions on a single variable ◦ Example: Weekdays – if you have a different action to perform for each day of the week, use a switch statement ◦ Use an if-else for all other scenarios:  More than one variable you’re testing (multiple conditions)  Testing for a range of values  Variable is not an int or char ◦ Example: Grades - each grade (A, B, C, D, E) has a range of values that reflect each grade letter 4

 Loop - part of program that repeats  Body - statements being repeated  Iteration - each repetition of body  Stopping condition 5

 while ◦ Safest choice ◦ Not always most elegant  do-while ◦ Loop iterates at least ONCE  for ◦ Numeric computation changes by equal amount 6

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.

Evaluate Boolean Expression Execute Body End loop true false 8

While Loops  Syntax while (Boolean_Expression) Body_Statement or while (Boolean_Expression) { First_Statement Second_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!

Evaluate Boolean Expression Execute Body End loop true false 12 Execute Body

13

 First, the loop body is executed.  Then the boolean expression is checked. ◦ As long as it is true, the loop is executed again. ◦ If it is false, the loop is exited.  Equivalent while statement Statement(s)_S1 while (Boolean_Condition) Statement(s)_S1 Do-while Loops

Loop Practice Write a while loop or a do-while loop that will compute the sum of the first n positive odd numbers. For example, if n is 5, you should compute

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

 Syntax 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 For Loops

Evaluate Boolean Expression Execute Body End loop true false 18 Execute Initializing Action Execute Update Action

 Possible to declare variables within a for loop int sum = 0; for (int n = 1 ; n <= 10 ; n++) sum = sum + n * n;  Note that variable n is local to the loop

Loop Practice Write a for loop that will compute the sum of the first n positive even numbers. For example, if n is 5, you should compute

 while ◦ Safest choice ◦ Not always most elegant  do-while ◦ Loop iterates at least ONCE  for ◦ Numeric computation changes by equal amount 22

 Problem with program preventing correct execution  Two most common mistake in loops ◦ Off-by-one errors ◦ Infinite Loops!!!!!! 23

Infinite Loops  A loop which repeats without ever ending is called an infinite loop.  If the controlling boolean expression never becomes false, a while loop or a do-while loop will repeat without ending.

count = 1; while (count <= num) { System.out.print(count + “, “); //count++; } 25

count = 1; while (count <= num); { System.out.print(count + “, “); count++; } 26

int count; // initializing action; boolean expression; update action for (count = 1; count >= num; count++) { System.out.print(count + “, “); } 27

 Help with Program 2 28