 Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki Blog Search Turing Chat Room Members
If Statements        Author Message
Cervantes  Posted: Fri Dec 29, 2006 6:57 pm   Post subject: If Statements

If Statements

Introduction

In computer science, much like in the English language, if statements are used to make decisions. For example, "if I have \$2.95 on me, then I'm going to buy a slice of pepperoni pizza from the caf." We're going to take this English statement and migrate into Turing code.

This tutorial aims to teach the semantics of if statements in Turing. We'll lay down some basic rules about the syntax of Turing and from there we will develop a system that allows us to evaluate code in a stepwise manner.

In general, an if statement in Turing looks like the following:
 code: if expression then    action end if

Now, I will lay down two fundamental rules. Assume "<action>" is some Turing code. It could be one line long, or more.

• The following two pieces of code are identical:
 code: if true then     end if

 code:

• The following two pieces of code are identical:
 code: if false then     end if

 code:

That is, the action in code (a) is never executed.

Now, we can write code that matches the code patterns in these two rules, but that won't be very helpful to us. It won't be helpful because this code is as static as the code we could write previously. We still aren't making any decisions. To do this, we need to learn about expressions. I will define an expression, in a kind of wishy-washy way, as anything that can be evaluated, simplified, or reduced down to a value. Values are concrete things: numbers like 5.8, strings like "hello world", true, and false. A mathematical expression such as "5 + 3 / 4" is an expression, because it can be simplified down to a value: 5.75. Here's another example, involving variables:
 code: var my_money : real := 2.95 put my_money

Evaluating the second line of code is not a one step process. Because "my_money" is an expression, we must first substitute in our value for the "my_money" expression. So the first step in evaluating this would be the following:
 code: var my_money : real := 2.95 put 2.95

And then the second line of code can be taken care of by a special rule in Turing's syntax that says "put <value>" can be handled.

We've now encountered one type of expression: a variable that is bound to some value. However, we still aren't able to write dynamic code--code that makes decisions. We have to look at some more complex expressions, expressions that use "operators".

Expressions using Operators

In computer science, an operator is something that is placed between two "operands". Operands are things like numbers or strings or other data. An operator compares the data on its left to the data on its right and returns a value. Some common operators are equals ("="), and ("&"), or ("|"), greater than (">"), less than ("<"), greater than or equal to (">="), and less than or equal to ("<="). All this should be fairly intuitive. Here are some examples:

•  code: true & false

This expression uses the "&" operator to compare the two operands, (1) true and (2) false. It returns false, since & returns true if and only if both operands are true, as is intuitive.

•  code: var my_money : real := 2.95 put my_money = 2.95

Here, the "=" operator cannot be immediately evaluated. This is because its first operand is not yet a value, but rather an expression itself. In a stepwise evaluation of this code, the next step would be to substitute the value for "my_money", to get the following:
 code: var my_money : real := 2.95 put 2.95 = 2.95

Now the "=" operator does its work, comparing 2.95 to 2.95, and it finds that these are equal, so it returns true. In the stepwise evaluation, the next step is this:
 code: var my_money : real := 2.95 put true

And this outputs "true".

•  code: % This expression uses four operators: ">", "<", "|", and "&". ((5.8 < 3.2) | (2 > 0)) & true

Here, the "&" operator cannot do its work right away, since its left operand is an expression. Let's go through a stepwise evaluation of this expression:
 code: ((5.8 < 3.2) | (2 > 0)) & true

 code: (false | (2 > 0)) & true

 code: (false | true) & true

 code: true & true

 code: true

You'll notice that the expression "(false | true)" evaluated to true. This is because the or ("|") operator returns true if at least one of its operands is true. Another way of saying this is that the or ("|") operator returns false if and only if both its operands are false.

Now we know how to simplify expressions. With this new skill, we can use expressions as conditionals to decide whether to perform certain actions or not. Let's now return to the pizza purchasing example presented at the start of this tutorial. We're finally able to code it.

An example using what we've learned so far

I'll reiterate the example we're going to express in Turing code. It goes like this: I need to decide what I'm having for lunch. If I've got \$2.95 or more on my person, I'll buy myself a slice of pizza. In Turing code, this looks like the following:
 code: var my_money : real := 3.70 if my_money >= 2.95 then     put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives."     my_money := my_money - 2.95 end if

The "my_money" variable represents the state of me. It represents how much money I have on my person. Let's step through the execution of this code:
 code: var my_money : real := 3.70 if 3.70 >= 2.95 then     put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives."     my_money := my_money - 2.95 end if

 code: var my_money : real := 3.70 if true then     put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives."     my_money := my_money - 2.95 end if

 code: var my_money : real := 3.70 put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives." my_money := my_money - 2.95

And now we are on familiar ground.

I chose 3.70 arbitrarily as the amount of money I have on my person. You can put a different value into the code and you might get a different result. If the value you use is less than 2.95, the code inside the if statement will never be executed and you won't get any output, and the value of my_money will not be changed. So we've successfully created code that makes a decision! It works differently depending on the state of the program--the values of the variables.

Now I raise the question, "What should I do if I don't have \$2.95 or more on my person? I'm going to starve, surely!" This is the otherwise, or "else", case.

Else

If at lunchtime I didn't have enough money to buy a slice of pizza, I'd probably ask my friends to buy me a slice of pizza. I just gotta have my pizza, you know. Let's incorporate that declaration of my behaviour into our example, and code it.
 code: var my_money : real := 1.50 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 if my_money >= 2.95 then     put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives."     my_order := my_order + 2.95 end if if my_money < 2.95 then     put "Me: Hey, can you buy me a slice of pizza?"     % Assume friend was a true friend and gave me some money     put "Friend: Fine."     put "Friend: I would like two slices of pepperoni pizza."     friends_order := friends_order + 2.95 * 2 end if % As we walk out of the cafeteria we pay for our orders my_money := my_money - my_order friends_money := friends_money - friends_order

Now, this seems a little unnecessary, don't you think? I'm going to trace through this code, and it's a little bit long, but nothing difficult. If the value of "my_money" is less than 2.95, then our trace of the code would look like this:
 code: var my_money : real := 1.50 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 if my_money >= 2.95 then     put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives."     my_order := my_order + 2.95 end if if my_money < 2.95 then     put "Me: Hey, can you buy me a slice of pizza?"     % Assume friend was a true friend and gave me some money     put "Friend: Fine."     put "Friend: I would like two slices of pepperoni pizza."     friends_order := friends_order + 2.95 * 2 end if % As we walk out of the cafeteria we pay for our orders my_money := my_money - my_order friends_money := friends_money - friends_order

 code: var my_money : real := 1.50 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 if 1.50 >= 2.95 then     put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives."     my_order := my_order + 2.95 end if if my_money < 2.95 then     put "Me: Hey, can you buy me a slice of pizza?"     % Assume friend was a true friend and gave me some money     put "Friend: Fine."     put "Friend: I would like two slices of pepperoni pizza."     friends_order := friends_order + 2.95 * 2 end if % As we walk out of the cafeteria we pay for our orders my_money := my_money - my_order friends_money := friends_money - friends_order

 code: var my_money : real := 1.50 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 if false then     put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives."     my_order := my_order + 2.95 end if if my_money < 2.95 then     put "Me: Hey, can you buy me a slice of pizza?"     % Assume friend was a true friend and gave me some money     put "Friend: Fine."     put "Friend: I would like two slices of pepperoni pizza."     friends_order := friends_order + 2.95 * 2 end if % As we walk out of the cafeteria we pay for our orders my_money := my_money - my_order friends_money := friends_money - friends_order

 code: var my_money : real := 1.50 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 if my_money < 2.95 then     put "Me: Hey, can you buy me a slice of pizza?"     % Assume friend was a true friend and gave me some money     put "Friend: Fine."     put "Friend: I would like two slices of pepperoni pizza."     friends_order := friends_order + 2.95 * 2 end if % As we walk out of the cafeteria we pay for our orders my_money := my_money - my_order friends_money := friends_money - friends_order

 code: var my_money : real := 1.50 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 if 1.50 < 2.95 then     put "Me: Hey, can you buy me a slice of pizza?"     % Assume friend was a true friend and gave me some money     put "Friend: Fine."     put "Friend: I would like two slices of pepperoni pizza."     friends_order := friends_order + 2.95 * 2 end if % As we walk out of the cafeteria we pay for our orders my_money := my_money - my_order friends_money := friends_money - friends_order

 code: var my_money : real := 1.50 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 if true then     put "Me: Hey, can you buy me a slice of pizza?"     % Assume friend was a true friend and gave me some money     put "Friend: Fine."     put "Friend: I would like two slices of pepperoni pizza."     friends_order := friends_order + 2.95 * 2 end if % As we walk out of the cafeteria we pay for our orders my_money := my_money - my_order friends_money := friends_money - friends_order

 code: var my_money : real := 1.50 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 put "Me: Hey, can you buy me a slice of pizza?" % Assume friend was a true friend and gave me some money put "Friend: Fine." put "Friend: I would like two slices of pepperoni pizza." friends_order := friends_order + 2.95 * 2 % As we walk out of the cafeteria we pay for our orders my_money := my_money - my_order friends_money := friends_money - friends_order

And now we have simplified our code down to a point where it is static--no more if statements. In our trace, we had to determine the value of the expression "my_money >= 2.95". We determined this to be false. Since the value of "my_money" is not changed anywhere between the time that we test "my_money >= 2.95" and the time that we test "my_money < 2.95", we know that the second expression, "my_money < 2.95" evaluates to true, since it is the opposite of "my_money >= 2.95" and that expression evaluated to false. So, we shouldn't have to go through the work of comparing "my_money" to 2.95 again, should we? To solve this problem, we'll add to our syntax.

I will now introduce two new rules, just like I gave two rules at the start of this tutorial. Assume <action1> and <action2> are two distinct bits of Turing code.

• The following two pieces of code are identical:
 code: if true then     else     end if

 code:

• The following two pieces of code are identical:
 code: if false then     else     end if

 code:

With these new semantics, we can now rewrite the pizza example:
 code: var my_money : real := 1.50 var friends_money : real := 10.00 if my_money >= 2.95 then     put "Me: I would like to purchase a slice of pizza with pepperoni and green pepper; hold the green olives."     my_money := my_money - 2.95 else     put "Me: Hey, can you buy me a slice of pizza?"     % Assume friend was a true friend and gave me some money     put "Friend: Fine."     put "Friend: I would like two slices of pepperoni pizza."     friends_money := friends_money - 2.95 * 2 end if

You'll notice I didn't bother with two variables I had in the previous code: "my_order" and "friends_order". I needed those in the previous code because, unlike when using else, doing the action in the first if statement did not prevent doing the action in the second if statement. So pictures this, in your mind's eye: I write the code that started this section (the one with my_order and friends_order) except I don't use those two variables. Instead, inside the first if statement that tested "my_month >= 2.95", I just decrease my_money by 2.95, then and there. Well then, what would have happened if I had \$4.00 on my person? The first expression, "my_money >= 2.95", would be true, so we would execute the code inside that if statement. That would decrease the value of my_money by 2.95, leaving me with \$1.05. That's less than \$2.95, so when I go to test the second expression, it too is true! Thus, I would have entered both if statements, and my speech would have sounded dumb: "I'll buy myself a slice of pizza. Friend, can you buy me a slice of pizza?" To solve this problem I had to carefully avoid modifying the value of my_money until after both if statements; I did that by introducing new variables: my_order and friends_order. By using else, we don't have any of these problems.

Question Time: Write a program that gets two numbers from the user and determines whether the first number is bigger than, equal to, or less than the second number.
A solution to this problem is found at the end of this tutorial. I suggest you try solving it for yourself before peeking, though. That's how you learn best.

Now I'm going to make the situation in the caf more complicated.    Cervantes  Posted: Fri Dec 29, 2006 6:58 pm   Post subject: (No subject)

Many related choices: Elsif

If I've got \$2.95 or more on my person, I'm getting a slice of pepperoni pizza. That's my primary choice, for certain. But what if I don't have enough for a pizza, but I do have enough for a bagel, which costs \$1.00? I should get the bagel, right? And if I don't even have enough for a begal, then I'll ask my friends to buy me a begal. One way to code this is like so:
 code: var my_money : real := 4.00 var friends_money : real := 10.00 var my_order : real := 0.00 var friends_order : real := 0.00 if my_money >= 2.95 then     put "Me: Pizza, please and thank you!"     my_order := my_order + 2.95 end if if (my_money < 2.95) & (my_money >= 1.00) then     put "Me: I'd like a begal, I suppose."     my_order := my_order + 1.00 end if if my_money < 1.00 then     put "Me: Hey, would you buy me a begal for lunch?"     % Assume friend is a true friend and buys me a begal     put "Friend: One begal and one pizza slice, please!"     friends_order := friends_order + 2.95 + 1.00 end if % Now we pay for our purchases my_money := my_money - my_order friends_money := friends_money - friends_order

This works, but again, it's inefficient. For one, I had to do the business with my_order and friends_order. More importantly, we're doing more tests on "my_money" than we need to. Can you see why? The reasons are the same as in the previous example when I introduced "else". Try doing a trace of it in your head.

To fix this, I'll introduce two new rules.

1. The following two pieces of code are identical:
 code: if true then     elsif then     elsif then     ... else     end if

 code:

2. The following two pieces of code are identical:
 code: if false then     elsif then     elsif then     ... else     end if

 code: if then     elsif then     elsif then     ... else     end if

With these new rules, we can rewrite the pizza and begal example:
 code: var my_money : real := 0.85 var friends_money : real := 10.00 if my_money >= 2.95 then     put "Me: Pizza, please and thank you!"     my_money := my_money - 2.95 elsif my_money >= 1.00 then    put "Me: I'd like a begal, I suppose."    my_money := my_money - 1.00 else    put "Me: Hey, would you buy me a begal for lunch?"    % Assume friend is a true friend and buys me a begal    put "Friend: One begal and one pizza slice, please!"    friends_money := friends_money - 2.95 - 1.00 end if

Notice, once again, that I didn't have to bother with my_order or friends_order.

Now, I'll do a trace on this. It will be rather long, but it's worthwhile, because being able to trace through a program properly is crucial when debugging. Here's the trace:
 code: var my_money : real := 0.85 var friends_money : real := 10.00 if my_money >= 2.95 then     put "Me: Pizza, please and thank you!"     my_money := my_money - 2.95 elsif my_money >= 1.00 then    put "Me: I'd like a begal, I suppose."    my_money := my_money - 1.00 else    put "Me: Hey, would you buy me a begal for lunch?"    % Assume friend is a true friend and buys me a begal    put "Friend: One begal and one pizza slice, please!"    friends_money := friends_money - 2.95 - 1.00 end if

 code: var my_money : real := 0.85 var friends_money : real := 10.00 if 0.85 >= 2.95 then     put "Me: Pizza, please and thank you!"     my_money := my_money - 2.95 elsif my_money >= 1.00 then    put "Me: I'd like a begal, I suppose."    my_money := my_money - 1.00 else    put "Me: Hey, would you buy me a begal for lunch?"    % Assume friend is a true friend and buys me a begal    put "Friend: One begal and one pizza slice, please!"    friends_money := friends_money - 2.95 - 1.00 end if

 code: var my_money : real := 0.85 var friends_money : real := 10.00 if false then     put "Me: Pizza, please and thank you!"     my_money := my_money - 2.95 elsif my_money >= 1.00 then    put "Me: I'd like a begal, I suppose."    my_money := my_money - 1.00 else    put "Me: Hey, would you buy me a begal for lunch?"    % Assume friend is a true friend and buys me a begal    put "Friend: One begal and one pizza slice, please!"    friends_money := friends_money - 2.95 - 1.00 end if

 code: var my_money : real := 0.85 var friends_money : real := 10.00 if my_money >= 1.00 then    put "Me: I'd like a begal, I suppose."    my_money := my_money - 1.00 else    put "Me: Hey, would you buy me a begal for lunch?"    % Assume friend is a true friend and buys me a begal    put "Friend: One begal and one pizza slice, please!"    friends_money := friends_money - 2.95 - 1.00 end if

 code: var my_money : real := 0.85 var friends_money : real := 10.00 if 0.85 >= 1.00 then    put "Me: I'd like a begal, I suppose."    my_money := my_money - 1.00 else    put "Me: Hey, would you buy me a begal for lunch?"    % Assume friend is a true friend and buys me a begal    put "Friend: One begal and one pizza slice, please!"    friends_money := friends_money - 2.95 - 1.00 end if

 code: var my_money : real := 0.85 var friends_money : real := 10.00 if false then    put "Me: I'd like a begal, I suppose."    my_money := my_money - 1.00 else    put "Me: Hey, would you buy me a begal for lunch?"    % Assume friend is a true friend and buys me a begal    put "Friend: One begal and one pizza slice, please!"    friends_money := friends_money - 2.95 - 1.00 end if

 code: var my_money : real := 0.85 var friends_money : real := 10.00 put "Me: Hey, would you buy me a begal for lunch?" % Assume friend is a true friend and buys me a begal put "Friend: One begal and one pizza slice, please!" friends_money := friends_money - 2.95 - 1.00

And now we're on familiar ground.

Question Time: Write a program that gets three numbers from the user and determines the value of the largest number.
A solution to this problem is found at the end of this tutorial

Conclusion

By now, you should be able to write dynamic programs--programs that can make decisions. You can execute different bits of code depending on what state your program is in. This is vital. As you continue to learn about Turing and about computer science in general, you will continue to see if statements making their presence known.

Also, this tutorial taught some basic semantics of Turing. These are the rules by which a Turing program can be traced and clarity made of otherwise confusing code. The ability to trace code is essential when trying to understand code. The code may or may not be your own. You might be trying to debug your code or trying to understand someone else's code. Regardless, tracing code is a fundamental tool that you will need.

Solutions
Here are possible solutions to the two problems given in this tutorial.

• Problem 1: Comparing two numbers:
 code: var num1, num2 : int put "Enter number 1: " .. get num1 put "Enter number 2: " .. get num2 if num1 > num2 then     put num1, " is bigger than ", num2 else     if num1 = num2 then         put num1, " is equal to ", num2     else         put num1, " is smaller than ", num2     end if end if

• Problem 1: Comparing two numbers, using elsif:
 code: var num1, num2 : int put "Enter number 1: " .. get num1 put "Enter number 2: " .. get num2 if num1 > num2 then     put num1, " is bigger than ", num2 elsif num1 = num2 then     put num1, " is equal to ", num2 else     put num1, " is smaller than ", num2 end if

• Problem 2: Finding the biggest of three numbers:
 code: var num1, num2, num3 : int put "Enter number 1: " .. get num1 put "Enter number 2: " .. get num2 put "Enter number 3: " .. get num3 if num1 > num2 then     if num2 >= num3 or num1 >= num3 then         put num1     else         put num3     end if else     if num1 >= num3 or num2 >= num3 then         put num2     else         put num3     end if end if

Disclaimer:
Actual price of slice of pepperoni pizza may vary. My caf sucks, except for the breakfast special. Hackmaster Posted: Fri Dec 29, 2006 7:30 pm   Post subject: (No subject)

Cervantes! yet another awe-inspiring tutorial! glad you got this one out! keep up the good work!! zylum  Posted: Thu Jan 04, 2007 3:08 am   Post subject: (No subject)

very nice you might want to add the xor operator though:

 code: %returns true if an odd number of arguments is true put true xor false put true xor false xor true xor true xor false %returns false since an even amount of arguments is true put true xor true put false xor false put true xor true xor false Cervantes  Posted: Thu Jan 04, 2007 1:59 pm   Post subject: (No subject)

I was thinking about it, but I figured that won't be of any immediate use. Your bitwise operators tutorial handles it, I think. Megatokyoguy Posted: Mon May 14, 2007 9:01 am   Post subject: RE:If Statements

I am currently working on Turing in class and I loved your previous tutorial (even though I know all that already - I am trying to learn about making games because I have a program that came with turing but I do not understand all the components)

I don't get the "Expressions using Operators"

Very confusing for me..... Cervantes  Posted: Mon May 14, 2007 11:11 pm   Post subject: RE:If Statements

Can you be more specific about what you didn't understand? Really, it shouldn't be hard stuff. If you have any experience with basic math, this section should come pretty naturally. If give me some specifics, though, I'll gladly help you out.  bawler27 Posted: Sat Mar 08, 2008 7:52 pm   Post subject: Re: If Statements

nice tut, it helped me out alot! now i can understand half the programs on the forums but there is one thing bothering me.

var+=var

what does that mean or do?    Mackie  Posted: Sat Mar 08, 2008 8:12 pm   Post subject: RE:If Statements

var += var
Is the same as
var := var + var

But, a better example would be.

a += b
a := a + b

There are others as well.

a -= b
a /= b
a *= b
a div= b
a mod= b Sean  Posted: Sat Mar 08, 2008 10:01 pm   Post subject: Re: If Statements

if you were doing var += var then
you are just adding the variable to itself, but you could do..

 Turing: var += 2

Which increases the variable by 2, instead of increasing it by itself. bawler27 Posted: Sun Mar 09, 2008 9:28 am   Post subject: Re: If Statements

oh, it all makes sense now, thx alot guys !!! Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First         Page 1 of 1  [ 11 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: