Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Slides:



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

How SAS implements structured programming constructs
CMPS 1371 Introduction to Computing for Engineers
CS0004: Introduction to Programming Repetition – Do Loops.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Lecture 13 Decision structures
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
Lecture 1: Introduction Lecture series based on the text: Essential MATLAB for Engineers and Scientists By Hahn & Valentine
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
MATLAB® While Loops.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Python Repetition. We use repetition to prevent typing the same code out many times and to make our code more efficient. FOR is used when you know how.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Matlab Programming for Engineers
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
COMP Loop Statements Yi Hong May 21, 2015.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
CSE123 - Lecture 4 Structured Programming- Loops.
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.
Loops & More 1.Conditionals (review) 2.Running totals and Running products 3.the for loop, examples 4.Nesting for loops, examples 5.Common errors 1.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
REPETITION CONTROL STRUCTURE
ECE Application Programming
while Repetition Structure
Repetition Structures Chapter 9
Matlab Training Session 4: Control, Flow and Functions
Think What will be the output?
Programming Fundamentals
Topics Introduction to Repetition Structures
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Control Structure Senior Lecturer
Algorithms Take a look at the worksheet. What do we already know, and what will we have to learn in this term?
Repetition Control Structure
CMPT 102 Introduction to Scientific Computer Programming
CSC115 Introduction to Computer Programming
Topics Introduction to Repetition Structures
62 – Sequences and Series Day 1 Calculator Required
REPETITION Why Repetition?
Presentation transcript:

Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.

Concept of while loops Recall from a previous lecture, loops are MATLAB constructs that allow a sequence of MATLAB commands to be executed more than once. Recall from a previous lecture, loops are MATLAB constructs that allow a sequence of MATLAB commands to be executed more than once. A while loop repeat a block of commands as long as an expression controlling it is true (1). A while loop repeat a block of commands as long as an expression controlling it is true (1).

The while loop construct Construction of a while loop: while expression command #1 command #1 command #2 command #2 command #3 command #3 …end The loop ends when “expression” is false (0); at this point the commands following the end of the loop is executed. code block is repeated while expression is true (1)

A “ while -loop ” example One way of controlling the loop is to define a counting index; in this case we start with n = 0. One way of controlling the loop is to define a counting index; in this case we start with n = 0. After the while statement an expression is defined that allows the loop to execute as long as the expression is true (1); in this case the loop runs until n > 5. After the while statement an expression is defined that allows the loop to execute as long as the expression is true (1); in this case the loop runs until n > 5. The counter must, of course, be changed in the loop, or the loop will run forever. In this case n is increased by 1 each time the block of commands in the loop is executed. The counter must, of course, be changed in the loop, or the loop will run forever. In this case n is increased by 1 each time the block of commands in the loop is executed.

Your output should look like this: The value of n is now 0 The value of n is now 1 The value of n is now 2 The value of n is now 3 The value of n is now 4 The value of n is now 5 >>

Another hands-on example A program to sum a series of numbers input by a user: a = 1; n = 0; myTotal = 0.0; while a >= 0 a = input('Enter a number to add to the running a number to add to the running... total ( neg # to end): '); # to end): '); if a>=0 myTotal = = + a; n = n + 1; n = n + 1; end end fprintf('The sum of the %d numbers you input is sum of the %d numbers you input is... %12.3f \ \ n',n,myTotal); % Note that the if >= 0 and end statements are needed in % this example.

Another Example In this example we want to ensure that the user inputs data in the correct range: In this example we want to ensure that the user inputs data in the correct range: This loop will repeat until the user has entered a non- negative number (n >= 0) This loop will repeat until the user has entered a non- negative number (n >= 0)

Hands on Create a function called fact2 that calculates the factorial of a number using a while loop. Create a function called fact2 that calculates the factorial of a number using a while loop.

Exercise Write a code that approximates the integral of sin(x)/x from 1 to 2. Use a while loop to sum the values of sin(x)/x from 1 to 2. Let the user input the increment to be used. Write a code that approximates the integral of sin(x)/x from 1 to 2. Use a while loop to sum the values of sin(x)/x from 1 to 2. Let the user input the increment to be used. Hint: Use the following approximation: Hint: Use the following approximation: The numerical value of this integral, to within four significant digits, is The numerical value of this integral, to within four significant digits, is

Summary Loops are MATLAB constructs that allow a sequence of MATLAB statements to be executed more than once. Loops are MATLAB constructs that allow a sequence of MATLAB statements to be executed more than once. While loops repeat a block of commands as long as an expression is true (1). While loops repeat a block of commands as long as an expression is true (1). while expression command #1 command #1 command #2 command #2 …end