MATLAB® While Loops.

Slides:



Advertisements
Similar presentations
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Debugging Introduction to Computing Science and Programming I.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Chapter 61 Post Test Loop Do statement(s) Loop Until condition Loop is executed once and then the condition is tested. If it is False, the loop is run.
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
Chapter 6 - Visual Basic Schneider
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
5.05 Apply Looping Structures
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Lec 4: while loop and do-while loop. Review from last week if Statements if (num < 0.5){...println("heads"); } if –else Statements if (num < 0.5){...println("heads");
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 5: Control Structures II
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
CS 100 Introduction to Computing Seminar October 7, 2015.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
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.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
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.
Chapter 4 Repetition Statements (loops)
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.
Chapter 5: Control Structures II
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
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.
Iteration: Beyond the Basic PERFORM
Lec 4: while loop and do-while loop
3.5- The while Statement The while statement has the following syntax:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Computing Fundamentals
Repetition Statements (Loops) - 2
Selections and Loops By Sarah, Melody, Teresa.
Presentation transcript:

MATLAB® While Loops

Why While Loops? Consider the following situation: Someone has been stealing your cookies! To figure out who, you are writing a script to act as a security system for your room. You need to take a measurement from a motion sensor every 10 seconds and take a picture if the motion sensor detects someone near the cookie jar. Can you write this program using a For loop? No, you don’t know if/when someone will try to steal your cookies!

While Loops WHILE loops repeat until some condition is no longer met Construction: while conditional expression MATLAB statements end As long as the expression is true, the MATLAB statements in the while loop will continue to execute If the expression is initially false, the while loop will never execute If the expression becomes false, the loop will terminate

While Loops Infinite Loop! Example: while a >= 0 MATLAB statements end a has to be defined in the program before you hit the while loop, otherwise the program won’t run If a is negative, the while loop will not execute at all If a is positive, the while loop will continue to execute as long as it stays positive What happens if a is positive and none of the statements in the loop ever change a? Infinite Loop!

Example 2: Simple WHILE Loop sum = 50; while sum < 100 sum = sum + 8; end disp(sum) sum Loop? 50 Yes 50+8 = 58 58+8 = 66 66+8 = 74 74 + 8 = 82 82 + 8 = 90 90 + 8 = 98 98 + 8 = 106 No Final Value for sum? 106 # Times thru loop? 7

Common Misconception about WHILE Loops Many first-time programmers think that the condition for the while loop is checked continually as the loop executes and as soon as the condition is false, the loop immediately terminates – this isn’t true If the while condition is true, the loop will completely execute to the end statement, even if the condition becomes false somewhere in the middle of the loop Once the loop has reached the end statement, the condition will be re-evaluated to determine whether or not the program should go through the loop again

While it is not raining, I will play outside.

While it is not raining, I will play outside.

Examples What is the output of the following code? x = 1; while x < 10 fprintf('%d \n', x); x = x+1; end Output: 1 2 3 4 5 6 7 8 9

Examples What is the output of the following code? x = 1; exitFlag = true; while exitFlag fprintf('%d \n', x); if x > 10 exitFlag = false; end x = x+1; Output: 1 2 3 4 5 6 7 8 9 10 11

Examples What is the output of the following code? x = 1; while x < 10 if (x < 4 || x > 7) fprintf('%d', x); end fprintf('\n'); x = x+1; Output: 1 2 3 8 9

Examples What is the output of the following code? x = 1; while (x > 1) fprintf('%d \n', x); x = x-1; end Output: Nothing!!!

Examples Using a while loop, you can add error checking to your program to ensure the user enters values correctly Require the user to enter a number between 1 and 10 before continuing: x = 0; while (x < 1) || (x > 10) x = input('Please enter a number: '); if (x < 1) || (x > 10) fprintf('Number must be between 1 and 10.\n'); end

Test Your Understanding