Loops October 10, 2017.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

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.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
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.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
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,
Repetition Statements while and do while loops
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Fundamental Programming Fundamental Programming More on Repetition.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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.
Computer Programming -1-
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 9 Repetition.
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
CS161 Introduction to Computer Science
Control Structures.
CiS 260: App Dev I Chapter 4: Control Structures II.
Programming Fundamentals
Chapter 5 Repetition.
For Loops October 12, 2017.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Loop Control Structure.
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Unit 3 - The while Loop - Extending the Vic class - Examples
LOOPS BY: LAUREN & ROMEO.
Repetition Control Structure
Lec 4: while loop and do-while loop
Chapter 6: Repetition Statements
What output is produced by the following fragment?
Control Structures Part 1
Looping III (do … while statement)
A LESSON IN LOOPING What is a loop?
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Building Java Programs
CSCI 1100/1202 February 6, 2002.
Presentation transcript:

Loops October 10, 2017

Loops A portion of a program that repeats a statement or group of statements is called a loop. Each repetition of the statement or group of statements (called the body of the loop) is called an iteration. Know this for the AP! Three loops you will frequently see are: For loops While loops Do-while loops

While Loops A while loop repeats as long as (while) a certain Boolean expression is true: while (x > 0) { //statement } If the expression in parentheses is never true, the body of the loop will never execute.

While Loops Consider the following code: int x = 0; while (x < 10) { cout << "Hello! "; } How many times will this loop output "Hello!"?

While Loops A loop that never ends (like on the previous slide) is called an infinite loop. If the Boolean condition involves a number, be sure to increase or decrease (increment/decrement) the number every time the loop executes! Handy shortcuts: x++; is the same as x = x + 1; x--; is the same as x = x - 1;

While Loops Consider the following code: int x = 0; while (x < 10) { cout << "Hello! "; x++; } How many times will this loop output "Hello!"?

Do-While Loops A do-while loop is similar to a while loop, but the Boolean condition is checked after the loop executes—so it will always execute at least once. The syntax is as follows: do { //statement } while(condition);

Do-While Loops Consider the following code: char ans; do { cout << "Hello!\n"; cout << "Would you like another greeting? (Y/N)\n"; cin >> ans; } while(ans == 'y' || ans == 'Y');