Nested Loops, Break/Continue. Nested Loops Nested loops : loop inside a loop – Loop through rows and columns – Loop through every word in every file –

Slides:



Advertisements
Similar presentations
Nested Loops. Nested loops Just as a selection structure can be nested within another selection structure (or within a loop), a loop can also be nested.
Advertisements

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.
Computer Science 1620 Loops.
Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
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.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
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.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
do - while  while: Execute the body of the loop at least once
Lecture 14: Control Flow. The break, continue, goto statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 3- Flow Control. Overview n Why flow control n Branches vs. Loops n Branch statements n Loop Statements n Complex loops n Boolean variables n.
Counter-Controlled Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount.
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:
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:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Nested for loops.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
For Loop Tips And Tricks
Decision Making and Branching (cont.)
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students, continue; and break; statements.
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.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Computer Programming -1-
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.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Topic 5 for Loops -Arthur Schopenhauer
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Lecture 4B More Repetition Richard Gesick
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Iteration with While You can say that again.
Control Structure Senior Lecturer
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Example Problems for Exam#2 (covers chapters 5, 6, 7, 8, 9)
Conditinoal Constructs Review
For Loops.
Building Java Programs
Building Java Programs
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
The for loop suggested reading:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Loops CGS3416 Spring 2019 Lecture 7.
Programming Fundamental
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Nested Loops, Break/Continue

Nested Loops Nested loops : loop inside a loop – Loop through rows and columns – Loop through every word in every file – Loop through every number less than every number less than 100

Nested Loop Regions Regions for nested loops: – With row major ordering //values available through every row/col for/while each row { //start row //values that start over for each row for/while each value in row { one "cell" } //end row }

Nested While Loops Careful with init/update for while loops – Broken Attempt

Fixed Whiles Outer loop works with i Inner with j – reset each time

Multiplication Table Print this table:

Multiplication Table Look for patterns – Have to work in rows

Header

Body

Break Complex exit conditions can get ugly:

Break Break exits a loop – Use CAREFULLY to avoid complex conditions

Continue Continue skips rest of loop body

Continue Keep special case special, normal case normal GoodLess Good for(int i = 10; i >= 1; i--) if( i == 5 ) continue; cout << i; } for(int i = 10; i >= 1; i--) if( i != 5 ) { cout << i; } }

Continue With While, must still update! int i = 0; while(i < 10) { if( i == 5 ) { i++; //infinite loop without! continue; } cout << i; i++; }

Prime Finder Find/Count primes <= n

Starting Point Possible primes: 2…n

Starting Point All numbers 2…n

Starting Point All numbers 2…n Only print primes… – Not divisible by anything in range 2-(number-1)

Starting Point Only print primes… – Not divisible by anything in range 2-(number-1)

Merged Code

Counting Need to count the primes

Counting Need to count the primes

Limiting line width Limit to 10 numbers per line – Need new counter – Increment at each print – When hits 10: Print newline Reset counter

Limiting line width Limit to 10 numbers per line – Need new counter – Increment at each print – When hits 10: Print newline Reset counter

Optimization No need to keep going if we find a factor

Other Optimizations No need to test evens – Include 2 always – Start from 3 and count by 2's Only go up to sqrt(n) Use a better algorithm

Optimizations Premature optimization is the root of all evil – Code a correct solution first – Take obvious measures to speed code – If needed, fix bottlenecks – High level optimizations trump low level ones