Chapter 4 Loops While loop The for loop do… while break and continue

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Structured Program Development in C
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
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
C Lecture Notes 1 Structured Program Development.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (while) Outline 3.7The While Repetition.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
ALGORITHMS AND FLOWCHARTS
Algorithm: procedure in terms of
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II
Control Statements: Part 1
Chapter 5: Control Structures II
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 5: Control Structures II
CHAPTER 5A Loop Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 5: Repetition Structures
Repetition-Counter control Loop
Chapter 2.2 Control Structures (Iteration)
( Iteration / Repetition / Looping )
Control Statements Kingdom of Saudi Arabia
Chapter 4 – Control Structures Part 1
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Control Structures - Repetition
Additional Control Structures
MSIS 655 Advanced Business Applications Programming
Structured Program
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 3 - Structured Program Development
Looping and Random Numbers
3 Control Statements:.
Chapter 3 - Structured Program Development
Chapter 6: Repetition Statements
EPSII 59:006 Spring 2004.
Loops.
Chapter 4: Repetition Structures: Looping
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops.
Presentation transcript:

Chapter 4 Loops While loop The for loop do… while break and continue labeled break

Repetition The while loop lets say we wish to display the numbers from 1 to 10 print “1\n2\n3\n4\n5\n6\n7\n8\n9\n10”; now 1 to 100000 x=1; while (x<=100000) { print x; x=x+1; }

The while loop parts 3 key parts initialize counter variable aka control variable x=1; while (x<=100000) { print x; x=x+1; } the exit condition the change value

Loop issues initializing the counter infinite loops for clarity indent the loop body memorize the basic while loop be able to adjust it to perform other tasks.

Lets talk through some examples a counter controlled loop to total 10 grades and print an average and total. lets say we want the user to determine the number of grades to process? describe 2 ways? user enters number of grades before entering grades a sentinel or flag value

Top-down, step-wise refinement what is that? first write spuedocode to describe the problem at a high level determine class average for the quiz; this is the top then break this into smaller pieces initialize variables input, total and count grades calculate a print result Then further break each of these down into smaller pieces as needed.

Top down design and coding This method helps to structure the resulting code. It uses the divide and conquer approach. Take a large formidable problem and break it into many smaller problems that can be solved more easily Allow problems to be solved by groups of people each smaller piece can be designed, coded and tested separately.

Top down design Not always used by experienced programmers developing familiar solutions.

Nested control structures commonly if statements are used inside of other if statements or inside of loops. while ( count<=10 ) { input = JOptionPane.showInputDialog(“Enter grade”); grade = Integer.parseInt(input); if (grade < 60 ) failed = failed +1 else passed =passed +1; count++; }

This declares and initializes the control variable The for loop for (int x = 1; x<100; x++) { print x; } // x no longer exists int x; for (x = 1; x<100; x++) { // x still exists This declares and initializes the control variable

The do….while Condition evaluated after at least one pass through the loop body. The while and for loops may never enter the loop body x=0; do { print x; x++; }while (x<100);

The break statement used to exit a loop consider this example. what will be displayed? for (c=1;c<10;c++) { if (c==5) break; print c; }

The continue statement used to continue the next iteration in a loop consider this example. what will be displayed? for (c=1;c<10;c++) { if (c==5) continue; print c; }

Labeled code blocks and break and continue What will this display mycode: { print “in my code”; for (int x=1;x<10;x++) { if (x==5) break mycode; print x; } print “done with loop”

Homework Page 214-215 Do number 1, 7 Due in Monday Oct 2 Also write a program to draw a Diamond with a odd input size for example 5 ..X.. .XXX. XXXXX