2.4 The if Statement

So far our programs have started at the first line of code and then executed each statement sequentially until we get to the end of the program. Sometimes we want our program to choose between 2 (or more) possible courses of action. That is where the if statement comes in. Here is an example.

var num : int
put "Enter a number larger than 0"
get num
if num > 0 then
   put "Thanks for paying attention."
else
   put "Can't you follow simple directions?"
end if 

If the user types an int greater than 0 (num > 0 is true) then the message after the then gets printed. If they enter an int 0 or less the message after the else gets printed. Note that this is not exactly what is meant by a user-friendly program!

After the if statement and before the then there needs to be a boolean expression. If this expression is true then the statement(s) after then and before else are executed. If the expression is false the statement(s) after else and before end if are executed.

Here is a slightly more complicated example.

const a := 12
const b := 5
const c := 37
const d := 11
var answer, response, count : int

count := 0

put "What is the value of ", a, " mod ", b, "?"
get response
answer := a mod b
if response = answer then
   put "You are correct"
   count := count + 1
else
   put "Sorry, the correct answer is ", answer
   put "Perhaps you should review chapter two"
end if

put "What is the value of ", c, " mod ", d, "?"
get response
answer := c mod d
if response = answer then
   put "You are correct"
   count := count + 1
else
   put "Sorry, the correct answer is ", answer
   put "Perhaps you should review chapter two!"
end if

put "You answered correctly ", count, " time" ..
if count not = 1 then
   put "s" ..
end if
put " out of 2 attempts." 

If you typed 1 for the first answer and 4 for the second here is what the output would look like

What is the value of 12 mod 5?
1
Sorry, the correct answer is 2
Perhaps you should review chapter two!
What is the value of 37 mod 11?
4
You are correct
You answered correctly 1 time out of 2 attempts.

The third if statement is used so that the program uses proper grammer when printing the number of correct responses. It will print "1 time" but "0 times" or "2 times" depending on how many correct responses there are.


Exercise 2.4

    1. Rewrite the following program using the indentation style in the text.

      const minAge:=18 var age:int
      var voter:boolean get age if age>=minAge then voter:=true else
      voter := false end if if voter then put "eligible to vote"
      else put "not eligible to vote" end if 
    2. What will the program print given each of the following inputs:
      1. 23
      2. 10
      3. 18
  1. Write a statement that sets the boolean variable leftSide to true if the int variable page is even, and to false if it is odd.
  2. Write a program that reads two positive integers and then determines whether or not the first number is a multiple of the second. The program should print both numbers and an appopriate message.
  3. Write statements to perform the following tasks.
    1. Add one to the value of zeroCount if the variable Score has the value zero.
    2. Add one to the value of pageCount if the variable lineCount is greater than pageLength.
    3. Read a value into variable age only if the variable endOfData does not have the value "T".
  4. Simplify each statement

    1. if x not= y then
      else
         put "Values are equal"
      end if 
    2. if answer <= maximumValue then
      else
         put "Answer is wrong"
      end if 
  5. Write a program to read the coefficients and then solve, for x, an equation of the form
    ax + b = 0

    Your program should take appropriate action if a is zero.