Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Chapter 5: Loops and Files.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Nested Loops. Nesting Control Structures One if statement inside another one An if statement inside a loop A loop inside an if statement Control structures.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Textbook Problem Java – An Introduction to Problem Solving & Programming, Walter Savitch, pp.219, Problem 08.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Repetition Statements while and do while loops
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
AP CS. Enhanced for loop for(object type, declared object) This loop is not necessarily iterative. It is ideal to traverse array arrays and array lists.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Computer Programming -1-
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Repetition.
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
More Loops.
More Loops.
Count Controlled Loops (Nested)
Chapter 4: Loops and Files
LOOPS BY: LAUREN & ROMEO.
INC 161 , CPE 100 Computer Programming
For Loops.
Module 4 Loops.
Control structures to direct program flow
Lab5 PROGRAMMING 1 Loop chapter4.
Week 6 CPS125.
A LESSON IN LOOPING What is a loop?
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Print the following triangle, using nested loops
Building Java Programs
Repetition (While Loop) LAB 9
SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder
Programming Fundamental
Presentation transcript:

Nested For Loops

First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop body }while(condition); for(integer initialization; condition; iteration) { // loop body }while(condition);

while(condition) { while(condition) { // loop body } do { do { // loop body }while(condition); for(integer initialization; condition; iteration) { for(integer initialization; condition; iteration) { // loop body }

Example 1 int n = 10; while(n == 10) { while(n > 0) { n--; }

Example 2 int n = 10; do { do { n--; }while(n > 0); }while(n == 10);

Example 3 for(int k = 0; k < 10; k++) { for(int j = 0; j < 10; j++) { System.out.println(“Hello World”); }

Example 3 continued for(int k = 0; k < 10; k++)// scope of variable k is within this entire loop { for(int j = 0; j < 10; j++) // j is only within this loop { System.out.println(“Hello World”); } System.out.println(j); // you cannot do this, j is “dead” }

Once a loop begins and encounters another loop, the inner loop now executes until it finishes before the outer loop continues. for(int k = 0; k < 10; k++) { for(int j = 0; j < 10; j++) { System.out.println(“Hello World”); } // Loop in red finishes 10 times before the outer loop can // continue.

What’s the output? int j = 0; while(j < 5) { for(int s = 0; s < 5; s++) { System.out.println(“Hello”); } System.out.println(“World”); j++; }

What’s the output? int j = 0; do { System.out.println(“World”); for(int s = 0; s < 5; s++) { System.out.println(“Hello”); } j++; }while(j < 10);

What’s the output? for(int k = 0; k < 5; k++) { for(int j = 0; j < 5; j++) { System.out.println(“Hello World”); } System.out.println(“Hello World”); }

Exercise 1 Print “Hello World” 50 times using two nested for loops. Let outer loop run 10 times and inner loop run 5 times.

Exercise 2 Ask for a user input of a number between Enter a while loop that will loop exactly the number of times of the input. Inside this while loop, enter a number a for loop that will display “Hello world” 5 times.

Exercise 3 Create this Rectangle: It’s a 10 x 5 that prints 10 consistently

Exercise 4 Create the triangle

Exercise 5 Create the Pyramid Keep going until you reach 10.