Repetition Structures

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loop Statements (Iteration). Iteration  A portion of a program that repeats a statement or group of statements is called a loop.  Each repetition of.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
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
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
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 Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
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.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
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.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
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.
Control Structures (Repetition structure) Jump Statements
Chapter 4 Repetition Statements (loops)
Unit-1 Introduction to Java
Control Statements: Part 2
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Expressions and Control Flow in JavaScript
Chapter 5: Control Structures II
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Arrays, For loop While loop Do while loop
Loop Control Structure.
Outline Altering flow of control Boolean expressions
Additional Control Structures
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Lab5 PROGRAMMING 1 Loop chapter4.
A LESSON IN LOOPING What is a loop?
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Presentation transcript:

Repetition Structures Chapter 4 Repetition Structures Chapter 4 - Repetition Structures

Chapter 4 - Repetition Structures The while Loop A while loop contains a statement or block of statements that are repeated indefinitely as long as some boolean expression is true. The test is performed at the beginning of the loop. Examples: while ( booleanExpr ) while ( booleanExpr ) { statement; statement 1; statement 2; ... } Be careful not to place a semicolon after the while state-ment–that would produce a null statement as the body of the loop. Chapter 4 - Repetition Structures

Chapter 4 - Repetition Structures The do/while Loop A do/while loop also contains a statement or block of statements that are repeated indefinitely as long as some boolean expression is true, but the test is performed at the end of the loop. Every do/while loop is executed at least once. Example: do { statement 1; statement 2; ... } while ( booleanExpr ) Chapter 4 - Repetition Structures

Chapter 4 - Repetition Structures The for Loop (1) A for loop executes a block of statements a specified number of times. The structure of a for loop is: Chapter 4 - Repetition Structures

Chapter 4 - Repetition Structures The for Loop (2) When a for loop starts, the loop index is set to initExpr, and contineExpr is evaluated. If the continuation expression is true, the loop is executed. When the loop finishes executing, incrementExpr is executed to change the value of the loop index, and contineExpr is evaluated again. If the continuation expression is still true, the loop is executed again. This process continues until contineExpr is false. Then execution skips to the first statement after the end of the loop. Chapter 4 - Repetition Structures

The continue Statement The continue statement interrupts the execution of a loop, and returns control to the top of the loop. Example: Result: Chapter 4 - Repetition Structures

Chapter 4 - Repetition Structures The break Statement The break statement interrupts the execution of a loop, and transfers control to the first statement after the loop. Example: Result: Chapter 4 - Repetition Structures

break and continue Statements in Nested Loops In nested loops, an ordinary break or continue statement applies to the innermost loop only. Example: Results: Chapter 4 - Repetition Structures

Labeled break / continue Statements A label can be added to each loop in a nested structure, and the label can be used to specify which loop a break or continue statement applies to. Example: Results: Chapter 4 - Repetition Structures

Chapter 4 - Repetition Structures Formatted Output Unlike languages such as C and Fortran, Java has no simple way to format numbers for display Class chapman.io.Fmt makes up for this lack. It contains two methods: printf, which prints formatted data to the standard output stream, and sprintf, which creates a formatted string. This class is very useful for creating structured tables of data, or for specifying the number of decimal digits to print out. Chapter 4 - Repetition Structures

Chapter 4 - Repetition Structures Formatted Output (2) The syntax of method printf is: Fmt.printf(format,value); where format is a format string and value is the value to be printed. The format string must contain a format descriptor (marked by an initial % character), which serves as a “model” for how to print out the value. All characters in the format string except for the format descriptor are printed unchanged. Chapter 4 - Repetition Structures

Chapter 4 - Repetition Structures Formatted Output (3) The structure of a format descriptor is: Example: Fmt.printf("Average price = $ %5.2f \n", ave); Result: Average price = $ 3.77 Chapter 4 - Repetition Structures