Chapter 9 Control Structures.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Introduction to Computers and Programming Lecture 9: For Loops New York University.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
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
1 for Loops Computer Science is a science of abstraction - creating the right model for a problem and devising the appropriate mechanizable techniques.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Jaeki Song ISQS6337 JAVA Lecture 04 Control Structure - Selection, and Repetition -
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
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.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
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.
CHAPTER 3 CONTROL STRUCTURES ( REPETITION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Repetition Statements while and do while loops
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1 The for loop. 2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so.
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Counting Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
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.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Chapter 9 Control Structures.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
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.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Java 24th sep /19/2018 CFILT.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 5 Repetition.
Decision statements. - They can use logic to arrive at desired results
For Loops October 12, 2017.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Lecture Notes – Week 3 Lecture-2
LOOPS BY: LAUREN & ROMEO.
Control Statements Loops.
Chapter 8: More on the Repetition Structure
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Building Java Programs
QUIZ 5 – RESULT.
Repetition Statements (Loops) - 2
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Presentation transcript:

Chapter 9 Control Structures

Objectives Code programs using the following looping techniques: While Loop Do While Loop For Loop Explain when the Break and Continue statements would be used when coding loops. Walk through While, Do While, and For loops documenting variables as they change.

while loop While Syntax: Flowchart: while (boolean condition) {         repeated statements   }

while loop example: While : Output: int counter = 1;  while (counter <=5)  {        System.out.println ("Hello " + counter);        counter ++; } Hello 1 Hello 2 Hello 3 Hello 4 Hello 5

do while loop Do While Syntax: Flowchart: do {          repeated statements   } while (boolean condition);

do while loop example: Do While : Output: Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 int counter = 1;  do { System.out.println("Hello " + counter);  counter++;  }  while (counter <=5);

for loop Initialization - initializes the start of the loop. for (initialization;   test;    increment)  {            statements; } Initialization - initializes the start of the loop. Example: int i = 0; Boolean Test - occurs before each pass of the loop. Example: i<10; Increment - any expression which is updated at the end of each trip through the loop. Example: i ++,   j+=3 Statements are executed each time the loop is executed.

for loop logic

for loop example 1: for loop Results for (int counter =1;  counter <=5;  counter++ ) { System.out.println ("Hello  " + counter); } Hello 1 Hello 2 Hello 3 Hello 4 Hello 5

for loop example 2: for loop with modulus results for (int number = 0;   number <1000;   number++) {           if ( number  %  12  = =  0 )  {                    System.out.println("Number is " + number);  } } ..... etc. Number is 996

for loop example 3: for loop to print even numbers from 1 to 20 results   for (int number = 2;   number <=20;   number+=2) {          System.out.println("Number is " + number);  } The number is 2 The number is 4 The number is 6 The number is 8 The number is 10 The number is 12 The number is 14 The number is 16 The number is 18 The number is 20

for loop example 4: for loop with multiple initialization & increments results   for (int i=0,  j=0 ;  i*j < 1000;  i++,  j+=2) {       System.out.println( "The answer is " + i + " * " + j + " = " + i*j );  }

Break Statement for (int index =1; index<= 1000; index ++) { if (index   ==   400) { break; }   System.out.println("The index is " + index);  }

Continue Statement for (int index = 1, index <= 1000; index ++) { if (index == 400)  { continue;  } System.out.println("The index is " + index);  }