CiS 260: App Dev I Chapter 4: Control Structures II.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 5: Control Structures II (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.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
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
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Java Fundamentals 4.
Chapter 9 Repetition.
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
The switch Statement, and Introduction to Looping
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Control Structures II (Repetition)
Java Programming: Guided Learning with Early Objects
Repetition-Sentinel,Flag Loop/Do_While
Programming Fundamentals
JavaScript: Control Statements.
Chapter 5: Control Structures II
Chapter 5 Repetition.
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Control Statements Loops.
Lab5 PROGRAMMING 1 Loop chapter4.
Chapter 5: Control Structures II (Repetition)
More Loops Topics Counter-Controlled (Definite) Repetition
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Control Statements:.
Presentation transcript:

CiS 260: App Dev I Chapter 4: Control Structures II

The while Loop (Repetition) You often need to repeat the same statements in a program many times. Repetition or __________ structures allow this. The Java while statement has this syntax while( expression ) statement ; The expression, called a loop _________, is the decision-maker and is either true or false. The statement is called the ______ of the loop. The statement executes continuously while the expression is ______. Watch out for _________ loops.

Example The output is ____________ F expression statement i = 0 while( i <= 20 ) { System.out.print( i + “ ” ); i = i + 5; } The output is ____________ The variable i is called the loop ___________ variable. When i reaches the value of ____, flow leaves the loop.

Counter-Controlled while Loops If you know in advance how many times a loop is to execute, you can use a ________ variable. final int N = 4; int counter = 0; while( counter < N ) { System.out.print( “Hello ” ); counter++; } The output is ___________________________

Sentinel-Controlled while Loops If you don’t know in advance how many times a loop is to execute, you can use a ________. final int SENTINEL = -999; int number; while( number != SENTINEL ) { number = Integer.parseInt(keyboard.readLine()); System.out.println( number + “ ” ); } The output is the series of numbers typed by the user until the user types _______.

Other while Loops Flag-controlled while loops A _________ variable controls the loop boolean found = false; while( ! Found ) { System.out.println( “Did you find it?” ); answer = System.in.read(); if( answer == ‘y’ || answer == ‘Y’ ) found = true; } EOF-Controlled while loops If reading data from a file, a loop can test each line of data read. When the line doesn’t exist (EOF), the loop stops.

The for Loop (Repetition) The while loop is used for any kind of repetition. The for loop is equivalent to a counter-controlled __________ loop. The syntax is for ( initial statement; loop condition; increment statement ) statement ; The initial statement is executed only once. The loop condition is then evaluated. If _____, the loop statement (block) is executed and the increment statement is executed. Then the previous step is repeated.

Examples of for Loops The output “Hello everyone” appears __ times. for( i = 1; i <= 5; i++ ) { System.out.print( “Hello” ); System.out.println( “ everyone” ); } The output “Hello everyone” appears __ times. Without the braces, “ everyone” appears _____. int N = 5, sum = 0; for( counter = 1; counter <= N; counter++ ) sum = sum + counter; System.out.println( sum ); The output of the previous statements is _____.

The do…while Loop (Repetition) The syntax of the do…while statement is do statement while ( expression ) ; The expression is called the _____ condition. The statement is always executed at least once. Then the expression is evaluated. If true, the statement is executed again, and so on.

Example of do…while The output from the above is ______________. { System.out.print(i + “ ”); i = i + 5; } while( i <= 20); The output from the above is ______________.

break and continue break is used to Skip the remainder of the switch structure Exit early from a loop continue causes the remaining statements in the loop to be skipped and continues with the next iteration.

Nested Control Structures Nesting is placing control structures (such as if…else) within control structures (such as while). One author states, “…nesting of control structures provides new power, subtlety, and complexity.”