For Loops October 12, 2017.

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

The "if structure" is used to execute statement(s) only if the given condition is satisfied.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
CPS120 Introduction to Computer Science Iteration (Looping)
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
 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.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Loops  Did you get the point behind a loop?  Why is there a need for loops? Code JunkiesLoops 1.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Overview Go over parts of quiz? Another iteration structure for loop.
CPS120 Introduction to Computer Science Iteration (Looping)
For Code Next For Code Next A loop is a segment of a code that repeats (changing slightly each time)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
1 For Loops l From Chapter 9 l A shorthand way of coding count 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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
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.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Follow up from lab See Magic8Ball.java Issues that you ran into.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 9 Repetition.
Review 1.
Control Structures.
Branching Constructs Review
Programming Fundamentals
Chapter 5 Structures.
Chapter 5 Repetition.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Loops October 10, 2017.
Iteration with While You can say that again.
For & do/while Loops.
LRobot Game.
Chapter 9 Control Structures.
Counting Loops.
LOOPS BY: LAUREN & ROMEO.
Lab5 PROGRAMMING 1 Loop chapter4.
Control Structures Part 1
Let’s all Repeat Together
2. Second Step for Learning C++ Programming • Data Type • Char • Float
A LESSON IN LOOPING What is a loop?
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
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.
Presentation transcript:

For Loops October 12, 2017

For Loops A for loop repeats for a specified interval—until some condition is met. The syntax is as follows: for (initialization; condition; increment/decrement) { //statement }

For Loops for (initialization; condition; increment/decrement) { //statement } You can declare the variable inside this portion of the loop: both x = 0; and int x = 0; are valid. Unlike with a while loop, the variable must be numerical (typically an int).

For Loops for (initialization; condition; increment/decrement) { //statement } The loop repeats as long as this condition is true. The condition is checked at the beginning of each execution. Examples: x < 10; x >= 5;

For Loops for (initialization; condition; increment/decrement) { //statement } This is the same as the increment/decrement in a while loop: x++, x--, and their ilk. The increment/decrement takes place after the loop executes. Unlike with a while loop, don’t put this statement in the body of the loop!

For Loops Consider this sample for loop: for (int x = 0; x < 5; x++) { cout << "Hello! “; } This code segment will output “Hello!” five times.

For Loops Consider this sample for loop: for (int x = 0; x > 5; x++) { cout << "Hello! "; } How many times will this code segment output “Hello!”?

For Loops Consider this sample for loop: for (int x = 0; x < 5; x++) { cout << "Hello! "; x++; } How many times will this code segment output “Hello!”?

For Loops Consider this sample for loop: for (int x = 0; x <= 5; x++) { cout << x << " "; } What is the output of this code?