Make your own free website on Tripod.com

    Lesson 2: If statements

Prev || Home || Next

bar.gif (11170 bytes)

    The if statement is one of the most powerful devices in programming. It allows the program to choose an action based upon input. For example, a program can be written to decide if a user can access the program by using an if statement to check if the password is correct or not.


    Without a conditional statement such as if programs would run almost the exact same way every time. If statements allow the flow of the program to be changed.


    Please note that all Boolean operators or values will be capitalized to differentiate them from normal English. Note, as well, that the actual C++ symbols to the operators are described later, but the C++ symbols are not OR, NOT, and AND. The symbols simply mean the same thing.


    There are many things to understand when using if statements. You must understand Boolean operators such as OR, NOT, and AND. You must also understand comparisons between numbers and most especially, the idea of true and false in the same way computers do.
    True and false in C++ are denoted by a non-zero number, and zero. Therefore, 1 is TRUE and 8.3

is TRUE but 0 is FALSE. No other number is interpreted as being false.
    When programming, the program often will require the checking of one value stored by a variable against another value, to determine which is larger, smaller, or if the two are equal.

To do so, there are a number of operators used.

The relational operators, as they are known, along with examples:
>    greater than 5>4 is TRUE
< less than 4<5 is TRUE
>= greater than or equal 4>=4 is TRUE
<= less than or equal 3<=4 is TRUE


It is highly probable that you have seen these before in some mathematical manifestation, probably with slightly different symbols. They should not present any hindrance understanding.

More interesting are the Boolean operators. They return 0 for FALSE, and a nonzero number for TRUE.


NOT: This just says that the program should reverse the value. For example, NOT (1) would be 0. NOT (0) would be 1. NOT (any number but zero) would be 0. In C and C++ NOT is written as !.
   
AND: This is another important command, it returns a one if 'this' AND 'this' are true. (1) AND (0) would come out as 0 because both are not true, only 1 is true. (1) AND (1) would come out as 1. (ANYREAL NUMBER BUT ZERO) AND (0) would be 0. (ANY REAL NUMBER BUT ZERO) AND (ANY REAL NUMBER BUTZERO) would be 1. The AND is written as && in C++. Do not be confused and think that it checks equality between numbers. It does not.     Keep in mind that AND will be evaluated before OR.

OR: Very useful is the OR statement! If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) would be 1! (0)OR(0) would be0. (ANY REAL NUMBER) OR (ANY REAL NUMBER BUT ZERO) would be 1! The OR is written as || in C++. Those are the pipe characters. On your keyboard, they may look like a stretched colon. On my computer it shares its key with \. Keep in mind that OR will be evaluated after AND.

The next thing to learn is to combine them... What is !(1 && 0)? Of course, it would be TRUE (1). This is because 1 && 0 evaluates two 0 and ! 0 equals 1.

Try some of these...they are not hard. If you have questions about them, you can email meat lallain@concentric.net.
    A. !(1 || 0)             ANSWER: 0    
B. !(1 || 1 && 0)             ANSWER: 0 (AND is evaluated before OR)
    C. !((1 || 0) && 0)         ANSWER: 1 (Parenthesis are useful)

If you find you enjoy this you might want to look more at Boolean Algebra, which is also very helpful to programmers as it is useful for helping program conditional statements.

The structure of an if statement is essentially:

if (TRUE)
Do whatever follows on the next line.

To have more than one statement execute after an if statement (when it evaluates to true) use brackets.

For example,


if (TRUE)
{
    Do everything between the brackets.
}


There is also the else statement. The code after it (whether a single line or code between brackets) is executed if the IF statement is FALSE.

It can look like this:


if(FALSE)
{
    Not executed if its false
}
else
{
    do all of this
}


One use for else is if there are two conditional statements that will both evalueate to true with the same value(but only at times. For example, x<30 and x<50 will both return true if x is less than 30 but not otherwise), but you wish only one of them to be checked. You can use else after the if statement. If the if statement was true the else statement will not be checked. It is possible to use numerous else statements followed by if statements.

Let's look at a simple program for you to try out on your own...

#include <iostream.h>            
           
int main()                 //Most important part of the program!
{
int age;                 //Need a variable...

cout<<"Please input your age: ";    //Asks for age
cin>>age;                 //The input is put in age

if(age<100)                 //If the age is less than 100
{
cout<<"You are pretty young!"; //Just to show it works
}
else if(age==100)        //I use else just to show an example
{
cout<<"You are old";        //Just to show you it works...
}
else if(age>100)
{
cout<<"You are really old";    //Proof that it works for any condition
}
return 0;
}


Now, this program did not use && || ! or anything in it. This is because it did not need too. Its purpose was to demonstrate if statements.
   
bar.gif (11170 bytes)

Prev || Home || Next