Loops.

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

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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:
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
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.
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 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.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
For loops in programming Assumes you have seen assignment statements and print statements.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
ARITHMETIC OPERATORS COMPARISON/RELATIO NAL OPERATORS LOGIC OPERATORS ()Parenthesis>Greater than &&And ^Exponentiation=>=
COMP Loop Statements Yi Hong May 21, 2015.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
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)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
REPETITION CONTROL STRUCTURE
The setw Manipulator The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where.
Introduction To Repetition The for loop
Chapter 2.2 Control Structures (Iteration)
Lecture 4B More Repetition Richard Gesick
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
Arrays, For loop While loop Do while loop
Loops October 10, 2017.
Iteration with While You can say that again.
For & do/while Loops.
Iteration: Beyond the Basic PERFORM
Module 4 Loops.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Building Java Programs
Looping III (do … while statement)
A LESSON IN LOOPING What is a loop?
If-statements & Indefinite Loops
Repetition Statements (Loops) - 2
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Loops

While Loops Loops enable programs to repeat certain sections of code as long as a certain condition is true. while (not_done_with_homework()) { work_on_homework(); }

While Loop Syntax while (condition) { ... }

Problem Write a program which will sum up all the positive integers whose square is less than or equal to 100.

Problem Write a program which will sum up all the positive integers whose square is less than or equal to 100. Strategy: 1. Determine what action is to be repeated and what changes in each iteration of the loop. 2. Determine how to know when to stop repeating this action

Problem Write a program which will print out an entire string by printing out each character one at a time using a while loop.

Problem Write a program which will print out an entire string by printing out each character one at a time using a while loop. 1. The repeated behavior is to print out a character and then update position in the string. 2. We know to exit once we have reached the end of the string.

Program Write a program which will print out the all the characters in a given string up until and including the second ‘a’. Things to consider: 1. What is the behavior that will be repeated via looping? 2. How will we know when to exit the loop?

For Loops The following structure in while loops is very common for iterating over numbers, strings, lists: int i = 0; while (i < n) { do_something(i); i++; }

For loop example The prior example in “for loop” form. for (int i = 0; i < n; i++) { do_something(i); } For loops are composed of 4 parts: 1. Initialization Statement 2. Exit Condition 3. Update Condition 4. Loop Body

For loops Very useful for iterating over structures. The following code iterates over the characters in a string to print out the entire string. string s = “example”; int n = s.length(); for (int i = 0; i < n; i++) { cout << s[i]; }

What do each of the loops print out? string s = “example”; int n = s.length(); for (int i = 0; i < n; i += 2) { cout << s[i]; } for (int i = n-1; i >= 0; i--) for (int i = 0; i > n; i++)

For Loops How to construct a for loop: 1. Determine what you are iterating over (number range, string, list, etc.) 2. Determine where to start (this is the initialization) 3. Determine when to exit the loop (this is the exit condition) 4. Determine what is updated at the end of each loop (this is the update condition) 5. Determine what is to be repeated (this is the body of the loop)

Challenge Programs Write a program which will replace every ‘a’ in a string with an ‘o’. Start with the following base: string s = “This is an example”; for (_____; ______; _____) { } What is being iterated over? Where to start, end, update, and what is being done in the body?

Challenge Program Write a program which will find the sum of all numbers that are divisible by both 3 and 5 that are less than 100. Things to consider: 1. How to know when to stop 2. What should we be doing in the body of the loop.

For Loops Consider the following program which attempts to reverse a string. string s = “example”; int n = s.length(); for (int i = 0; i < n; i++) { s[i] = s[n-i-1]; } cout << s; What will it actually do to the string “example”? What is the content of the string after each loop? How might the program be corrected to actually produce the right result?