LOOPS CHAPTER 21 1. Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

CSCI 130 Advanced Program Control Chapter 8. Program Controls so far for loop while loop do…while loop.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Objectives You should be able to describe:
Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.
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.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
The switch Statement, DecimalFormat, and Introduction to Looping
TODAY’S LECTURE Review Chapter 2 Go over exercises.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming with C# Iteration LECTURE 3. Summary of last lecture SequenceSelectionif and switch statementsCastingRandom.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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,
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
CPTG286K Programming - Perl Chapter 4: Control Structures.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 6 Controlling Program Flow with Looping Structures.
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.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Chapter 4 – C Program Control
Chapter 4 Repetition Statements (loops)
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
ITM 352 Flow-Control: Loops
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
CHAPTER 21 LOOPS 1.
PROGRAM FLOWCHART Iteration Statements.
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

LOOPS CHAPTER 21 1

Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2

Four Types of Loops  while –The most basic loop –Checks a condition before each loop; loops if it's true  do…while –Checks a condition after each loop; loops if it's true  for –Most common loop structure –A loop structure that contains three separate statements  foreach –Automatic for loop for enumerable collections 3

Four Types of Loops  while –The most basic loop –Checks a condition before each loop; loops if it's true while (true) { print( "Loop" ); } –This will cause an infinite loop!!! –"Loop" will never appear in the Console pane because the entire Unity process will be frozen –This would necessitate force quitting Unity –On old, single-threaded computers, this would require turning the computer off! 4

Four Types of Loops  while – A better while loop –while loops need an exit condition A condition that will cause the condition to evaluate to false –Checks a condition before each loop; loops if it's true int i=0; while ( i<3 ) { print( "Loop: "+i ); i++; // Increment operator } –i++ will increment i on every pass through the loop –When i reaches 3, the conditional clause will evaluate to false, and the loop will exit 5

Four Types of Loops  do…while –Like a while loop, but checks after the loop has run This allows a guarantee that the loop will run at least once –Checks a condition after each loop; loops if it's true int i=5; do { print( "Loop: "+i ); i++; // Increment operator } while (i<3); –When execute the loop once before checking the conditional clause and then exiting –Note the semicolon after the while clause 6

Four Types of Loops  for –A for loop contains three separate clauses for (int i=0; i<3; i++) { print( "Loop: "+i ); } –Initialization clause: int i=0; –Condition clause: i<3; –Iteration clause: i++ –The i variable only exists within the for loop It is scoped to the for loop –The iteration clause doesn't have to be ++ i-- is another common option for counting down instead of up 7

Four Types of Loops  foreach –Automatically loops for each element in a collection string str = "Hello"; foreach (char chr in str) { print( chr ); } –This will print each character of Hello individually –foreach will be used extensively in the following chapter 8

Jump Statements Within Loops  Jump statements change the execution of a loop –break Breaks out of the loop entirely –continue Breaks out of this iteration of the loop and moves on to the next 9

Jump Statements Within Loops  break –Breaks out of the loop completely string str = "Hello"; foreach (char chr in str) { if (chr == 'l') { break; } print( chr ); } –This will print: HeHe –Once chr becomes 'l', it will break out of the loop –Can be used on any kind of loop 10

Jump Statements Within Loops  continue –Breaks out of the loop completely string str = "Hello"; foreach (char chr in str) { if (chr == 'l') { continue; } print( chr ); } –This will print: HeoHeo –When chr is 'l', the loop continues without printing 11

Chapter 21 – Summary  Of the four types of loops: –while and do…while are somewhat dangerous –for is by far the most common and is very flexible –foreach is very useful for strings, arrays, and Lists We'll talk about it a lot more in Chapter 22  Jump statements can be used to have more control over your loops –A break can be used to break out of an infinite loop as well  Chapter 22 will cover arrays and Lists, two kinds of collections in C# 12