Simple Branches and Loops

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
J. Michael Moore Structured Programming CSCE 110 Drawn from James Tam's material.
Making Decisions In Python
Loops Brent M. Dingle Texas A&M University Chapter 7 – part D (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
CS101 Computer Programming I Chapter 4 Extra Examples.
Loops Brent M. Dingle Texas A&M University Chapter 7 – part B (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
A Simple Program CPSC Pascal Brent M. Dingle Texas A&M University 2001, 2002.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Learning Javascript From Mr Saem
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Loops Brent M. Dingle Texas A&M University Chapter 6 – Section 6.3 Multiway Branches (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)
The for Statement A most versatile loop
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
© 2006 Pearson Education Chapter 3 Part 2 More about Strings and Conditional Statements Loops (for and while) 1.
FOP: While Loops.
Chapter 5: Structured Programming
CprE 185: Intro to Problem Solving (using C)
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Unit 3 Lesson 9 Repetition Statements (Loops)
Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code. Repetition in Pascal:
Introduction To Repetition The for loop
Repetition Structures Chapter 9
Brent M. Dingle Texas A&M University Chapter 6, Sections 1 and 2
EGR 2261 Unit 4 Control Structures I: Selection
IF statements.
Chapter 5: Control Structures II
Loop Structures.
Chapter 5: Control Structure
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Control Structures - Repetition
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.
Control Structure Senior Lecturer
Lecture 4A Repetition Richard Gesick.
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
Iteration with While You can say that again.
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
LOOPS BY: LAUREN & ROMEO.
Module 4 Loops.
Chapter 6: Repetition Statements
Selection Statements.
Chapter 4: Control Structures I (Selection)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Relational Operators.
Chapter 3: Selection Structures: Making Decisions
ECE 103 Engineering Programming Chapter 18 Iteration
Repetition Statements (Loops) - 2
The Selection Structure
Just Enough Java 17-May-19.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

Simple Branches and Loops CPSC 110 - Pascal Brent M. Dingle Texas A&M University Chapter 3

Simple Branching Often we will want the computer to do something only if some criteria is met. In programming this is accomplished by using an “if-then” statement. For example: if (x = 2) then writeln(‘There are two of them.’);

Simple Branching (cont) Other times we will want one thing done sometimes and something else done at other times. This is done by using an “if-then-else” statement (also called a conditional statement). For example: if (x = 1) then writeln(‘There is only one.’) else writeln(‘There could be more than one.’); NOTICE: There is NO semicolon before the ‘else’

Boolean Expressions Notice that in the statement: if (x = 1) then the (x = 1) part is referred to as a Boolean Expression Boolean Expressions evaluate to TRUE or FALSE. In Chapter 6 we will get into more complex Boolean expressions and discover what the words: AND, OR, NOT have to do with such expressions. You may want to peek ahead.

Comparison Operators Pascal English Example = equal to Ans = ‘Y’ <> not equal to x <> y < less than z < 5 <= less than or equal to z <= 10 > greater than q > -9 >= greater than or equal to q >= 12

Equality of Characters When doing comparisons on characters or strings you should be aware that capital letters are NOT equal to lowercase letters. For example the comparison: if (‘a’ = ‘A’) would evaluate to FALSE

Compound Statement Sometimes we will want to do more than one command under a certain condition. For example we may wish to write two lines of output if a score is >= 60: IF ( score >= 60 ) then BEGIN writeln(‘You managed to pass.’); writeln(‘Your score is: ‘, score); END ELSE writeln(‘You did not pass.’);

Compound Statement So you will notice the two writelns after the if-then. That is an example of a compound statement. So, a compound statement is a list of statements enclosed in a begin-end pair and separated with semicolons.

Simple Loops Every now and again, it is useful to have the computer do exactly the same thing repeatedly, while some condition is true. In Pascal this is accomplished with a while-do loop. Like the if-then-else, this will be one of the constructs you use the most.

while statement Suppose you wanted to print Howdy 10 times to the screen. You could use 10 writeln statements, but there is an easier way: count := 1; WHILE (count <= 10) DO BEGIN writeln(‘Howdy’); count := count + 1; END; Notice the use of a compound statement

while statement (cont) Or perhaps you just want to display the numbers 1 to 20 on the screen: num := 1; WHILE (num <= 20) DO BEGIN writeln(num); num := num + 1; END; Can you modify the above to just display the ODD numbers from 1 to 20 ? Can you modify the above to display the numbers 10 to 20 ?

while statement (cont 2) while statements do NOT have to just operate on numerical conditions, they can operate on any boolean expression. For example, you may wish to repeatedly ask the users if they wish to continue: reply = ‘y’; WHILE (reply = ‘y’) DO BEGIN … do some stuff … write(‘Do you wish to continue (y/n) -> ‘); readln(reply); END;

Infinite Loops How many times will the below loop execute? count := 1; WHILE (count > 0) DO BEGIN … some stuff … count := count + 1; END;

Infinite Loops (cont) Loops that do not end (usually because their exit condition never becomes false) are called infinite loops. There are quite a few ways to accidentally create infinite loops so always be sure to check that the condition on the while will eventually become false.

Uninitialized Variables A major problem in programming is using variables that are not assigned an initial value This shows up quite often when doing loops. For example: WHILE (count < 5) DO writeln(‘Bevo Burger anyone?’); will execute how many times ?

Uninitialized Variables (cont) The answer is we don’t know. It depends on what the value of count was set to BEFORE the while loop was encountered. If count was >= 5 the loop executed ZERO times. If count was = 0 then it executed 5 times. If count was = 4 then it executed 1 time. If count was = -1 then it executed 6 times. ALWAYS INITIALIZE YOUR VARIABLES !

while loop – problems For practice I would encourage you to do the problems (8, 9, 10, 11, 12, 13) on page 95 of the textbook

End of Simple Branches and Loops