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.

Slides:



Advertisements
Similar presentations
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Advertisements

CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Repeating Structures Do While Loops. Do While Loop While loops have a test condition before the loop –This means that java will test the condition before.
Repeating Actions While and For Loops
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
TESTING LOOPS ● Simple Loops ● Nested Loops ● Concatenated Loops ● Unstructured Loops.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CHAPTER 6 REPETITIVE EXECUTION 6.1 Introduction A repetition structure or loop makes possible the repeated execution of one or more statements called the.
For Next Looping My first Looping Structure. For…Next FOR counter_variable = initial_value TO end_value STEP increment Statement-1 Statement-2 ……. Statement-n.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
There are two additional statements that can be used to control the operation of while loops and for loops: break statements continue statements.
While Loops CMSC 201. Overview Today we will learn about: Looping Structures While loops.
Logic Structure - focus on looping Please use speaker notes for additional information!
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Bordoloi and Bock Control Structures: Iterative Control.
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
CPS120 Introduction to Computer Science Iteration (Looping)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
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.
What do we use to measure the angle? PROTRACTOR Angles, Measuring Angles The unit of measurement of an angle is degrees. We measure angles in degrees.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Chapter Looping 5. The Increment and Decrement Operators 5.1.
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.
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.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 4 Repetition Statements (loops)
EasyCode Foundations Vocabulary Terms.
Chapter 3: Decisions and Loops
Programming Logic and Design Fourth Edition, Comprehensive
Branching Constructs Review
2-D Lists Taken from notes by Dr. Neil Moore
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Efficiency (Chapter 2).
Conditinoal Constructs Review
Repeating Instructions And Advance collection
Conditinoal Constructs Review
Loops A loop is a repetition control structure.
Count Controlled Loops (Nested)
Iteration: Beyond the Basic PERFORM
LOOPS BY: LAUREN & ROMEO.
Chapter 8: More on the Repetition Structure
3.5- The while Statement The while statement has the following syntax:
the captured notes and redid - hopefully it all works.
Alternate Version of STARTING OUT WITH C++ 4th Edition
JavaScript: Control Statements II
CS 1111 Introduction to Programming Spring 2019
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Repetition (While Loop) LAB 9
For Loops Pages
Database Programming Using Oracle 11g
2-D Lists Taken from notes by Dr. Neil Moore
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Control flow: Loops UW CSE 160.
Presentation transcript:

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 can be nested inside each other to any degree you need

Nested loops The general principle of nesting loops is that the inner loop must completely finish its execution before the next iteration of the outer loop starts Every time the outer loop iterates, the inner loop needs to start the control variable back at the beginning again

Nested for loops for i in range(10): for j in range(5): print(i, j) The nesting is shown by the indentation Different variables are used for the two loops, i and j, for clarity Other statements can be inside the outer loop, not just the inner loop

Nested while loops These work on the same principles as the for loops If they are controlled by counters, make sure the initializations of the counters are where they need to be. That is, if the inner loop needs to have its counter set to zero before the loop, you must decide whether the initialization needs to be inside the outer loop or outside the outer loop ct1 = 0 while ct1 < 5: ct2 = 0 while ct2 < 10: print(ct1, ct2) ct2 += 1 ct1 += 1