COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

While 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.
Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
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.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
1 Repetition structures Overview while statement for statement do while statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
COMP 14: Looping (part 2) May 31, 2000 Nick Vallidis.
©2004 Brooks/Cole Chapter 5 Repetition. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Repetition So far, our programs processed a single set.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
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.
Copyright © Texas Education Agency, Computer Programming For Loops.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
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,
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
While ( number
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
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.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
Loops in Java.
CiS 260: App Dev I Chapter 4: Control Structures II.
الحلقات التكرارية وجمل الدوران (loops)
Loops October 10, 2017.
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.
Repetition Control Structure
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
CSCI 1100/1202 February 6, 2002.
Presentation transcript:

COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1

Announcements Facebook Tech Talk tonight, 6pm, SN011 Google info session, 6pm, SN014, tomorrow night 2

Questions? 3

Today in COMP 110 do-while loops for loops

Review: loops Often you need to repeat an action in a program Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich

Review: loops Loop: part of a program that repeats Body: statements being repeated Iteration: each repetition of body Stopping condition Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich

Review: Types of Loops while ◦ Safest choice ◦ Not always most elegant do-while ◦ Loop iterates AT LEAST once for ◦ Similar to while, but often more convenient syntax 7

Review: while loop start; while (not enough sandwiches) { make a sandwich; } distribute sandwiches; Start Enough sandwiches? Distribute sandwiches No Yes Make sandwich

Review: while loop syntax while (boolean expression) { statements; }

Review: Using a while loop int n = 1; while (n <= 10) { System.out.println(n); n = n + 1; }

while loop Evaluate Boolean expression Execute Body End loop true false

while loop First, evaluate Boolean expression If true, go into the body If false, do not go into the body

Types of Loops while ◦ Safest choice ◦ Not always most elegant do-while ◦ Loop iterates AT LEAST once for ◦ Similar to while, but often more convenient syntax 13

do-while loop Similar to while loop At least one iteration of the loop ◦ First execute the loop body ◦ Then evaluate the Boolean expression ◦ If true, execute the body again ◦ If false, quit the body

do-while loop 15 Evaluate Boolean expression Execute Body End loop true false Execute Body From outside the loop Evaluate Boolean expression End loop false Execute Body From outside the loop true Equivalent

do-while loop syntax do { statements; // loop body } while (boolean expression); If Boolean expression is true, repeat loop body Otherwise, exit loop

Using a do-while loop int n = 1; do { System.out.println(n); n = n + 1; } while (n <= 10); Don’t forget the semicolon!

Using a do-while loop What if n = 20? int n = 20; do { System.out.println(n); n = n + 1; } while (n <= 10);

Infinite loop int n = 1; do { System.out.println(n); // n = n + 1; } while (n <= 10);

Types of Loops while ◦ Safest choice ◦ Not always most elegant do-while ◦ Loop iterates AT LEAST once for ◦ Similar to while, but often more convenient syntax 20

for loop Components of a for loop: ◦ Initializing actions ◦ Boolean expression ◦ Loop body ◦ Update actions

for loop 22 Evaluate Boolean expression Execute Body End loop true false Execute Initializing actions Execute Update actions

for loop syntax for (initializing actions; Boolean expression; update actions) { statements; // loop body }

Using a for loop int n; for (n = 1; n <= 10; n++) { System.out.println(n); }

while loops and for loops int n; for (n = 1; n <= 10; n++) { System.out.println(n); } int n = 1; while (n <= 10) { System.out.println(n); n = n + 1; }

for loop: initializing actions Only done ONCE at the beginning of the loop, before the Boolean expression is tested int n; for (n = 1; n <= 10; n++) { System.out.println(n); }

for loop: update actions Executed at the end of every iteration ◦ After the loop body, but before the next Boolean expression evaluation int n; for (n = 1; n <= 10; n++) { System.out.println(n); }

Tracing a for loop int n; for (n = 1; n <= 3; n++) { System.out.println(n); } n = ? n = 1 n = 2 n = 3 n = 4 Execute initializing actions Evaluate Boolean expression Execute body Execute update actions

Infinite loop int n; for (n = 1; n <= 10; n = 0) { System.out.println(n); }

Types of Loops while ◦ Safest choice ◦ Not always most elegant do-while ◦ Loop iterates AT LEAST once for ◦ Similar to while, but often more convenient syntax 30

Wednesday Spend some more time with loops 31