For Loops.

Slides:



Advertisements
Similar presentations
Solving Problems with Repetition. Objectives At the end of this topic, students should be able to: Correctly use a while statement in a C# program Correctly.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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:
CS150 Introduction to Computer Science 1
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
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,
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.
Overview Go over parts of quiz? Another iteration structure for loop.
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:
Repetition 8/9/2009. Learning to Program -- Suggestions Spend a lot of time fiddling around with code –Programming is something you have to learn by doing.
Nested for loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
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.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
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.
Adding and Subtracting Integers (Positive and Negative Numbers)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
The switch Statement, and Introduction to Looping
Chapter 4 Loops DDC 2133 Programming II.
Java's for Statement.
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.
CS 108 Computing Fundamental Notes for Thursday, October 5, 2017
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 07 More Repetition Richard Gesick.
Computer Science 101 While Statement.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Lecture 4B More Repetition Richard Gesick
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Repetition and Loop Statements
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
More Loops.
More Loops.
Chapter 4: Loops and Files
Java Programming Loops
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
LOOPS BY: LAUREN & ROMEO.
Chapter 6: Repetition Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
© 2016 Pearson Education, Ltd. All rights reserved.
Computing Fundamentals
Arrays.
A LESSON IN LOOPING What is a loop?
Building Java Programs
Java Programming Loops
Loops.
Building Java Programs
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

For Loops

Main body structure for(declare an integer, declare conditions, declare iteration) for(int n = 0, n <= 10, n++) { // whatever code you want to execute on the for loop } In this for loop, n is initialized to 0, our condition is that n must be less than or equal to 0, n will go up by 1 every iteration of the loop

for(int n = 0; n <= 100; n++) for(int n = 10; n <= 100; n+5) for(int n = 100; n < 100; n++) for(int n = 0; n < 10; n+10) for(int n = 50; n > 100; n+30) for(int n = 0; n <= 10; n+3)

Some Rules regarding the loop for(int n = 0; n <= 100; n++) If integer n is declared outside of this loop, it will generate error. The scope of n is inside this loop meaning: { } n = 5 // this generates an error because the scope of n has finished

Is this ok? int n; for(n = 0; n <= 100; n++) { } What about this: int x = 100 for(int n = 0; n <= x; n++) Finally, what about this? int s = 0; for(int n = s; n <= 100; n++)

To Review for(integer initialization; condition; iteration) Give the loop its starting condition, if you want to start at 0, let this be equal to zero. Tell it the condition in which the loop terminates. Be careful not to hit an infinite loop Tell it how fast the loop will increment.

Exercise 1 Use a for loop to display hello world exactly 10 times Now do it 10 times but set your starting condition to n = 50;

Exercise 2 Ask a user for input of an integer greater than 100 Exercise 2 Ask a user for input of an integer greater than 100. Use a for loop to count down from the input number to 0. Subtract 5 from the number every time you loop. Display the value of the number every time you loop.

Exercise 3 Use a for loop to count down from 100 to 0. Output: 100 99 98 97 96

Challenge 1 Figure out how many numbers are divisible by 7 from 150 - 300 Challenge 2 Tell us how many factors of 100 can exist between 0-100. Challenge 3 Display the following pattern. 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 Challenge 4: Display the following pattern. 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 10 9 8 7 6 5 4 3 10 9 8 7 6 5 4 10 9 8 7 6 5 10 9 8 7 6 10 9 8 7 10 9 8 10 9 10