Download presentation
Presentation is loading. Please wait.
1
Simple Branches and Loops
CPSC Pascal Brent M. Dingle Texas A&M University Chapter 3
2
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.’);
3
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’
4
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.
5
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
6
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
7
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.’);
8
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.
9
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.
10
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
11
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 ?
12
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;
13
Infinite Loops How many times will the below loop execute? count := 1; WHILE (count > 0) DO BEGIN … some stuff … count := count + 1; END;
14
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.
15
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 ?
16
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 !
17
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
18
End of Simple Branches and Loops
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.