Overview. Iteration 4 kinds of loops. Infinite Loops. for while do while foreach

Similar documents
Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

204111: Computer and Programming

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Advanced Computer Programming

Introduction. C provides two styles of flow control:

Chapter 3 - Simple JavaScript - Programming Basics. Lesson 1 - JavaScript: What is it and what does it look like?

Accelerating Information Technology Innovation

1.7 Limit of a Function

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Chapter 6: Using Arrays

Loops. In Example 1, we have a Person class, that counts the number of Person objects constructed.

CS 106 Introduction to Computer Science I

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Repetition CSC 121 Fall 2014 Howard Rosenthal

Introduction to the Java Basics: Control Flow Statements

Increment and the While. Class 15

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Condition Controlled Loops. Introduction to Programming - Python

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CS 106 Introduction to Computer Science I

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

More About WHILE Loops

Loops. CSE 114, Computer Science 1 Stony Brook University

int $0x32 // call interrupt number 50

Expressions and Casting

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

ECE 122. Engineering Problem Solving with Java

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Programming Fundamentals

CIS 3260 Intro to Programming in C#

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Variables and Functions. ROBOTC Software

CSE 113 A. Announcements - Lab

Computer Programming I - Unit 5 Lecture page 1 of 14

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

MARK SCHEME for the May/June 2011 question paper for the guidance of teachers 9691 COMPUTING. 9691/23 Paper 2 (Written Paper), maximum raw mark 75

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Condition Controlled Loops. Introduction to Programming - Python

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

CONDITION CONTROLLED LOOPS. Introduction to Programming - Python

ENGR 1181 MATLAB 09: For Loops 2

CS 177 Week 5 Recitation Slides. Loops

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Conditionals and Loops

LECTURE 5 Control Structures Part 2

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

Computer Programming

CS 1301 Exam 1 Fall 2010

Chapter Goals. Contents LOOPS

Programming for Experimental Research. Flow Control

Java Review. Fundamentals of Computer Science

Repetition, Looping. While Loop

These are notes for the third lecture; if statements and loops.

Text Input and Conditionals

LESSON 3 CONTROL STRUCTURES

The Practice of Computing Using PYTHON. Chapter 2. Control. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Repetition, Looping CS101

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Repeating Instructions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

Test 1: CPS 100. Owen Astrachan. October 1, 2004

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

REPETITION CONTROL STRUCTURE LOGO

Scope of this lecture. Repetition For loops While loops

CompSci 125 Lecture 11

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

EE 3170 Microcontroller Applications

Controls Structure for Repetition

9. MATHEMATICIANS ARE FOND OF COLLECTIONS

DELHI PUBLIC SCHOOL TAPI

CS1102: Macros and Recursion

for all x, the assertion P(x) is false. there exists x, for which the assertion P(x) is true.

DarkRift Server Plugin Tutorial

Flow Control: Branches and loops

Control, Quick Overview. Selection. Selection 7/6/2017. Chapter 2. Control

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

CS 1301 Exam 1 Fall 2010

Lecture Transcript While and Do While Statements in C++

Problem Solving for Intro to Computer Science

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

Lecture 10. Daily Puzzle

Object-Oriented Programming in Java

8. Control statements

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

CS1004: Intro to CS in Java, Spring 2005

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Transcription:

Repetition

Overview Iteration 4 kinds of loops for while do while foreach Infinite Loops

Iteration One thing that computers do well is repeat commands Programmers use loops to accomplish this 4 kinds of loops in C# for loop while loop do while loop foreach loop

Criteria for loops 1.Usually have some initial condition Starting a Beginning in a certain state 2.Must have a test to continue 3.Must make progress towards finishing

Loops in Everyday Life Bad children are told to write sentences on the board I will not pour Clorox in the fish tank Have to write this sentence either A certain number of times Until the teacher is happy As many as you can during break

The for loop Good when you know exactly how many times you need to execute something Has format: for (<initialization>; <test to continue>; <increment>) { // everything in here is what is repeated // over and over again Initialization is where the is given a starting value The test determines whether or not to continue The increment can be any amount, including negative, and occurs after the loop statements execute

Satisfying the Teacher Example: 1000 sentences? No problem int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( I will not pour Clorox ); // Remember, ++ is the same as // = + 1

But I want them numbered! No problem int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works 1 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works true 1 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works 1 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 1 I will not pour Clorox in the Fish Tank

Why this works 2 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works true 2 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works 2 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 2 I will not pour Clorox in the Fish Tank

Why this works 3 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works true 3 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works 3 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 3 I will not pour Clorox in the Fish Tank

Why this works 4 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

When will it end? We see that this will go on for a while It s a little more interesting later around 1000

Why this works true 999 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works 999 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 999 I will not pour Clorox in the Fish Tank

Why this works 1000 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works true for last time 1000 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works (are we finished?) 1000 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); Output: 1000 I will not pour Clorox in the Fish Tank

Why this works 1001 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour );

Why this works false 1001 int ; for ( = 1; <= 1000; ++) { Console.WriteLine ( + I will not pour ); // Jump down here and continue

Final Output 1 I will not pour Clorox in the fish tank. 2 I will not pour Clorox in the fish tank. 3 I will not pour Clorox in the fish tank. 4 I will not pour Clorox in the fish tank.... 999 I will not pour Clorox in the fish tank. 1000 I will not pour Clorox in the fish tank.

The while loop Good for when you don t know how many times to repeat Teacher says Write until I m happy Has format: while (<boolean value>) { // stuff to repeat over and over

Example bool teacherhappy = false; int linenumber = 1; while (!teacherhappy) { Console.WriteLine (linenumber + I will not ); linenumber++; teacherhappy = attitudefunction ( ); // assume attitudefunction can change // teacherhappy

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); Output: 1 I will not pour Clorox in the fish tank

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); Output: 1 I will not pour Clorox in the fish tank

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not );

Example: Re-Writing 1-1000 (using a while loop) int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); Output: 1 I will not pour Clorox in the fish tank

Infinite Loops This loop isn t making a lot of progress! Loops that repeat forever are called infinite loops Apparently lock up Output: 1 I will not pour Clorox in the fish tank 1 I will not pour Clorox in the fish tank 1 I will not pour Clorox in the fish tank 1 I will not pour Clorox in the fish tank.. Continue forever

Problem Solved int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); ++;

Problem Solved int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); ++;

Problem Solved int = 1; 1 while ( < 1000) { Console.WriteLine ( + I will not ); ++; Output: 1 I will not pour Clorox in the fish tank

Problem Solved int = 1; 2 while ( < 1000) { Console.WriteLine ( + I will not ); ++; // Remember, ++ is the same as // = + 1

Example: Re-Writing 1-1000 (using a while loop) int = 1; 2 while ( < 1000) { Console.WriteLine ( + I will not ); ++;

Problem Solved int = 1; while ( < 1000) { Console.WriteLine ( + I will not ); ++; 2

Problem Solved int = 1; while ( < 1000) { Console.WriteLine ( + I will not ); ++; 2 Output: 2 I will not pour Clorox in the fish tank

Problem Solved int = 1; while ( < 1000) { Console.WriteLine ( + I will not ); ++; 3

How does it end? int = 1; while ( < 1000) { 999 Console.WriteLine ( + I will not ); ++;

How does it end? int = 1; while ( < 1000) { 999 Console.WriteLine ( + I will not ); ++;

How does it end? int = 1; while ( < 1000) { 999 Console.WriteLine ( + I will not ); ++;

Problem Solved int = 1; while ( < 1000) { 999 Console.WriteLine ( + I will not ); ++; Output: 999 I will not pour Clorox in the fish tank

How does it end? int = 1; while ( < 1000) { 1000 Console.WriteLine ( + I will not ); ++;

How does it end? int = 1; while ( < 1000) { 1000 Console.WriteLine ( + I will not ); ++;

How does it end? int = 1; now false while ( < 1000) { 1000 Console.WriteLine ( + I will not ); ++; // So we never print out // 1000 I will not pour Clorox in the fishtank

Another Problem Solved int = 1; now true while ( <= 1000) { 1000 Console.WriteLine ( + I will not ); ++;

The do-while loop Similar to while loop Must execute at least one time (test is at bottom) Has format: do { while (<boolean value>);

Example (count from 1 to 3) int = 0; 0 do { ++; Console.WriteLine (); while ( < 3);

Example (count from 1 to 3) int = 0; 0 do { ++; Console.WriteLine (); while ( < 3);

Example (count from 1 to 3) int = 0; 1 do { ++; Console.WriteLine (); while ( < 3);

Example (count from 1 to 3) int = 0; 1 do { ++; Console.WriteLine (); while ( < 3); Output: 1

Example (count from 1 to 3) int = 0; 1 do { ++; Console.WriteLine (); while ( < 3);

Example (count from 1 to 3) int = 0; 2 do { ++; Console.WriteLine (); while ( < 3);

Example (count from 1 to 3) int = 0; 2 do { ++; Console.WriteLine (); while ( < 3); Output: 2

Example (count from 1 to 3) int = 0; 2 do { ++; Console.WriteLine (); while ( < 3);

Example (count from 1 to 3) int = 0; 3 do { ++; Console.WriteLine (); while ( < 3); // Note: is now 3, but we still have // to finish out the loop it doesn t skip

Example (count from 1 to 3) int = 0; 3 do { ++; Console.WriteLine (); while ( < 3); Output: 3

Example (count from 1 to 3) int = 0; 3 do { ++; Console.WriteLine (); while ( < 3); now false, so loop is finished

The foreach loop Often we have a collection of data We d like to do something to each one of the items in the collection Certainly we can do this with a for loop But C# provides a new loop foreach References an item in the collection Provides access to it Loops through all elements in the collection

foreach template foreach (<TYPE> <NAME> in <COLLECTION>) { WORK TO BE DONE ON EACH ITEM

A sample application

foreach example Assuming we had a listbox control called listboxnames with strings in it foreach (string currentname in listboxnames.items) { MessageBox.Show(currentName.ToUpper()); Displays all of the strings in capitalization to the user in MessageBoxes.

foreach vs. for loop The previous foreach loop on the collection using a for loop: for(int i=0; i < listboxnames.items.count; i++) { string currentname = (string)(listboxnames.items[i]); MessageBox.Show(currentName.ToUpper());

Summary for loops good for when you know how many times you want to repeat while and do-while good for when you don t foreach loop useful for processing collections All loops must finish, or they become infinite loops All loops must have a test to continue, or they become infinite loops