Chapter 9 Control Structures.

Slides:



Advertisements
Similar presentations
WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?
Advertisements

Introduction to Computers and Programming Lecture 9: For Loops New York University.
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:
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
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.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Chapter 4: Control Structures II
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);
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
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:
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
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 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.
For Loop Tips And Tricks
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Follow up from lab See Magic8Ball.java Issues that you ran into.
Slides by Evan Gallagher
Slides by Evan Gallagher
CHAPTER 4 DECISIONS & LOOPS
Chapter 9 Repetition.
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5 Repetition.
Decision statements. - They can use logic to arrive at desired results
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Alice in Action with Java
The University of Texas – Pan American
Outline Altering flow of control Boolean expressions
Chapter 9 Control Structures.
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Lecture Notes – Week 3 Lecture-2
LOOPS BY: LAUREN & ROMEO.
Control Statements Loops.
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
Building Java Programs
Control Statements Loops.
Repetition Statements
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);  }