Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
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.
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.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
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.
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
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.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.
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
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.
The for loop.
Chapter 9 Control Structures.
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CS 112 Introduction to Programming Loop Examples; Variable Scoping; Nested Loops; Yang (Richard) Yang Computer Science Department Yale University 208A.
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.
Computer Programming -1-
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 9 Repetition.
REPETITION CONTROL STRUCTURE
Branching Constructs Review
Topic 5 for Loops -Arthur Schopenhauer
CiS 260: App Dev I Chapter 4: Control Structures II.
The nested repetition control structures & Continue Statements
Chapter 5 Repetition.
For Loops October 12, 2017.
Outline Altering flow of control Boolean expressions
Chapter 9 Control Structures.
1020: Introduction to Programming Mohamed Shehata November 7, 2016
Building Java Programs
Building Java Programs
Lab5 PROGRAMMING 1 Loop chapter4.
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.
Building Java Programs
Alternate Version of STARTING OUT WITH C++ 4th Edition
The for loop suggested reading:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Building Java Programs
Suggested self-checks:
Building Java Programs
Building Java Programs
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Programming Fundamental
Programming Fundamental
Presentation transcript:

Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1

Code JunkiesLoops 2 Types of loops  definite loop: A loop that executes a known number of times.  indefinite loop: A loop where it is not easily determined in advance how many times it will execute. Indefinatedefinate

For Loop  Is for loop syntax clear to everybody?  for(initialization ;test condition ; update ); Code JunkiesLoops 3

Code JunkiesLoops 4 Nested for loops  A for loop can contain any kind of statement in its body, including another for loop. –The inner loop must have a different name for its loop counter variable so that it will not conflict with the outer loop.  nested loop: Loops placed inside one another, creating a loop of loops. for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { cout<<"six”; } Output: six

Code JunkiesLoops 5 Nested for loop exercise  What is the output of the following nested for loop? for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { cout<<"*”; } cout<<“\n”; }  Output: * ** *** **** ***** ******

Code JunkiesLoops 6 Nested for loop exercise  What is the output of the following nested for loop? for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { cout<<i; } cout<<“\n”; }  Output:

Code JunkiesLoops 7 Nested for loop exercise  Create a nested for loops produce the following output

Code JunkiesLoops 8 Nested for loop exercise  A for loop can have more than one loop nested in it.  What is the output of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= (5 - i); j++) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print(i); } System.out.println(); }  Answer:

Code JunkiesLoops 9 Common nested loop bugs  It is a common bug to accidentally type the wrong loop counter variable, which can lead to incorrect behavior. –What is the output of the following nested loops? for (int i = 1; i <= 10; i++) { for (int j = 1; i <= 5; j++) { cout<<j; } cout<<“\n”; } –What is the output of the following nested loops? for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 5; i++) { cout<<j; } cout<<“\n”; }

While Loop Code JunkiesLoops 10

Code JunkiesLoops 11 Equivalence of for,while loops  Any for loop of the following form: for ( ; ; ) { ; } can be replaced by a while loop of the following form: ; while ( ) { ; }

Code JunkiesLoops 12 The while loop statement  The while loop is a new loop statement that is well suited to writing indefinite loops.  The while loop, general syntax: while ( ) { ; } –Example: int number = 1; while (number <= 200) { cout<<number<<“\n”; number *= 2; } –OUTPUT:

Code JunkiesLoops 13 While loop flow chart  The execution of a while loop can be depicted as the following: | V | | | ^ V V | | | | execute the controlled statements | | | | ^ | V | V | | | | +---<-----< < V | execute statement | | after while loop |

Code JunkiesLoops 14 Example while loop  A loop that finds and prints the first factor of a number (other than 1): Cout<<"Type a number:“ ; int number; Cin>>number; int factor = 2; while (number % factor != 0) { factor++; } Cout<<“First factor: “<<factor;  OUTPUT: Type a number: 49 First factor: 7