Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Introduction to C Programming
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repeating Actions While and For Loops
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
1 Chapter 6 Looping Dale/Weems/Headington. 2 l Physical order vs. logical order l A loop is a repetition control structure based on a condition. l it.
Introduction to a Programming Environment
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
The switch Statement, DecimalFormat, and Introduction to Looping
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”);
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Chapter 6 Looping.
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.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
30/10/ Iteration Loops Do While (condition is true) … Loop.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Introduction to Pascal The Basics of Program writing.
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Programming, an introduction to Pascal
Code The Arduino Environment.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
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.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 6 Looping. 2 l A loop is a repetition control structure. l it causes a single statement or block to be executed repeatedly What is a loop?
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
UNIT 5 Lesson 15 Looping.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Control Structure Senior Lecturer
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Outline Altering flow of control Boolean expressions
CS1100 Computational Engineering
T. Jumana Abu Shmais – AOU - Riyadh
Repetition Statements (Loops) - 2
Variables and Constants
Looping and Repetition
COMPUTING.
Presentation transcript:

Repetition

Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create an infinite loop and you have to press the stop button in the execution window to exit.

Loopexample1.t % The "ManyCircleAreas" program % Computes the areas of circles var radius, area : real const pi :real := loop put "Enter radius ".. get radius area := pi * radius ** 2 put "Area is ", area end loop

Conditional Loops You can stop a loop by putting a condition in your loop. For example, you can make the first example exit the loop when a negative number is entered for the radius. This signal is called a sentinel.. This is done using the exit when command.

Conditional Operators The operators that can be used with the exit when command are: > value “Greater than value” < value “Less than value” = value “equal value” >= value “Greater than or equal to value” <= value “Less than or equal to value” not= value “not equal to a value” Also called BOOLEAN Operator. Returns a value of either True or False.

Loopexample2.t % The "ManyCircleAreas2" program % Compute circle areas until you enter a negative radius var radius, area : real const pi :real := put "Enter a negative radius to stop execution" loop put "Enter radius ".. get radius exit when radius < 0 area := pi * radius ** 2 put "Area is ", area end loop put "That's all folks"

The condition exit when radius < 0, exits the loop when a negative number is entered in for a radius. Change the condition so the loop exits when the radius is equal to -1.

Comparing Strings When comparing strings, computers use their ASCII value. for example the character "A" is 65, "B" is 66, "C" is 67, and so on. Lower case letters have different values: "a" is 97, "b“ is 98, etc. This means that the condition "A" < "B" is true and also that "a" < "B" is false.

Loopexample3.t % The "ComputeAverage" program % Compute the average of a series of marks % Give average to the nearest integer var mark : int var count, sum : int := 0 const sentinel :real := – 1 put "Enter a series of marks" put "End with ", sentinel loop get mark exit when mark = sentinel count := count + 1 sum := sum + mark end loop put "Average mark is ", round (sum / count )

Notice: We initialized the variable sum and count to 0. Notice the command round() will round an answer to an operation in the brackets. Another variable called average should be declared of type real to store the average calculation. average := round(sum/count) Also… try….. average := sum div count

Loopexample4.t % The "Obey" program % Read in a series of words % until the word "stop" is read var word : string put "Enter a series of words, one to a line" put "If you want to stop say so" loop get word exit when word = "stop" end loop put "This is the end"

Counted Loops We have looked at conditional and infinite loops. Counted loops allows you to specify how many times you want the loop to run. CODE: for count : startnumber.. endnumber {Body} end for

Loopexample5.t % The "ComputeAverages" program % Reads marks and computes average var mark : int var sum : int := 0 put "Enter marks" for count : put count get mark sum := sum + mark end for put "Average is ", round (sum / 5)

Good programming practice From the previous example, it is good programming practice to use a variable instead of the number “5”. We could define a constant: ◦ const numMarks : real := 5 And then change the following lines: ◦ for count 1.. numMarks ◦ put "Average is ", round (sum / numMarks)

Loopexample6.t var start, stop : int put "Enter the initial and final values for the loop: ".. get start, stop for i : start.. stop put i end for

Counting by Multiples Loopexample7.t %Starts at 2 and goes up by 2 until it reaches 10 for count : by 2 put count end for %Starts at 1 and goes up by 5 until it reaches 10 for count : by 5 put count end for

Questions Pg. 138/500 on the IPT.pdf Do questions #1-6 LOOK AT THE EXAMPLES FROM THIS NOTE WHEN CODING YOUR PROGRAMS. REMEMBER TO COMMENT!!!