Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Repetition Statements Recitation – 02/20/2009 CS 180 Department of Computer Science, Purdue University.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control Recitation – 09/(11,12)/2008 CS 180 Department of Computer Science, Purdue University.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Some loop programs 1: Read in 10 integers and output their sum
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
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.
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.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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.
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,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
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,
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,
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
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.
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.
UCT Department of Computer Science Computer Science 1015F Iteration
Lecture 6 Repetition Richard Gesick.
Loops in Java.
Chapter 5: Control Structures II
CiS 260: App Dev I Chapter 4: Control Structures II.
Arrays, For loop While loop Do while loop
Programming Fundamentals Lecture #6 Program Control
MSIS 655 Advanced Business Applications Programming
- Additional C Statements
Outline Altering flow of control Boolean expressions
LRobot Game.
Control Statements Loops.
Repetition Control Structure
A LESSON IN LOOPING What is a loop?
CHAPTER 21 LOOPS 1.
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
Lec 6 Loop Statements Introduction to Computer Programming
Presentation transcript:

Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University

Now posted on the class webpage. Due Wed, Sept. 24 at 10 pm. Start early. All questions on the class newsgroup. Exam in 2 weeks (10/1); Consult Mentor (Debbie) for more on pattern, advice etc. Evening consulting hours. LWSN B146. MTW 7-10 pm. Project 3 and Exam

Loop statements while do-while for break and continue statements Display text in Applets Outline

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 body of the loop A loop could be used to compute sum of N numbers What are Loops?

while loop continuously executes a block of statement until a particular condition is true Syntax while (boolean_expression) { statement; } The while statement

Example: int count = 1; while (count < 11) { System.out.println ("Count is: " + count); count++; } while statement example

The do-while statement Similar to a while statement, except that the loop body is executed at least once Syntax do { Statement; } while (Boolean_Expression); Remember the semicolon at the end of while. It helps the compiler to distinguish do-while and while statements

Example: int count = 1; do { System.out.println ("Count is: " + count); count = count+1; } while (count<=10); do-while statement example

Syntax for (Initialization; Condition; Update) { statement; } Initialization expression initializes the loop and is executed once at the beginning Loop terminates when the Condition evaluates to false Update can be an increment or a decrement on a variable. It is executed after the first iteration The for statement

for statement example 1  The action of the for loop in listing 4.5

Example: Print all even numbers in the first 10 numbers for (int count =1; count<=10;count++) { if(count%2 == 0) System.out.print(count+” ”); } Output: for statement example 2

Useful to operate on collection of data such as enumeration Example enum Names {James, Joshua, Scott} for (Names name: Names.values()) System.out.println(name+” ”); Names.values() represent all the values in the enumeration. The for-each statement

The break statement break statement can be used to end the loop immediately Example: for (int n=1; n<=5;n++) { if (n==3) { break; } System.out.print(n+” ”); } Output: 1 2

The continue statement A continue statement ends current loop iteration and begins next iteration Example: for (int i = 0; i < 10; i++) { if (i == 5) { continue; } System.out.print(i+” ”); } Output: It is recommended to sparingly use break and continue statements

loops in Applets A multiface Applet Uses loop to draw several smiley faces Uses if statement to alter appearance View sample program, listing 4.9 class MultipleFacessample program

drawString Method Similar to drawoval method, but displays text Example canvas.drawString("Hello",10, 20); Writes word Hello at point (10, 20) Used to place "Kiss, Kiss" and "Tee Hee" on screen in listing 4.9