Introduction to Computer Science - Java

Separating Program Flow Using "if"

        We previously learned that computer programs execute operations in sequence.     This means that the computer will execute code one line after another.     But what if for some reason we want to skip certain lines of code?     In other words, what if there are certain lines of code we want to do only if a certain condition holds true?     (A condition is a programming concept which means if a certain state of affairs is true.     A condition in everyday life might be "If it is rainy, I will stay indoors today."     A condition in a computer program might be, "If the variable is even, then print on the screen the sentence "Even number." ")     Skipping lines of code in this way is called altering program flow.     Skipping lines of code is called altering program flow because it allows a program to have two entirely different sequences, or program flows.     In one sequence, certain code is skipped over and in the other sequence, the code is not skipped.

        So how do we skip lines of code?     To skip lines of code we use the reserved word (also called a keywordif   .     With the help of the reserved word if we can write a line of code in which we say that if and only if a certain condition is true then execute the following commands.     What I mean by "true" is - if a certain state of affairs is occuring.     If today is Monday.     If you are over 18 years old.     If your salary is less than $1,000 a month.     These are all conditions that may or may not be true.    

        Let's see an example in Java.

int nachumsNumber;
nachumsNumber = 15;
if (nachumsNumber < 10)
{
        System.out.println("Number is");
        System.out.println("smaller than 10");
        nachumsNumber = 20;
}
System.out.println("Number is");
System.out.println("not smaller than 10");

I added in some commands which we have not learned yet, so this should look confusing.     Anyway, let's still try to figure out what is going on in the code.     What is the condition?     Is the condition true or not?     . . . . The condition is "nachumsNumber < 10".     This translates into English as "Is the variable nachumsNumber less than 10."     If nachumsNumber is less than 10, then the condition is true.     But if nachumsNumber is not less than 10, then we have to admit that the condition is not true.     In our case we initialized the variable nachumsNumber to the value 15.     Is the conditon true?     The condition is not true.     Since the condition is not true, the computer will skip over the lines inside the opening and closing braces   {   }  .     Therefore the lines:

        System.out.println("Number is");
        System.out.println("smaller than 10");
        nachumsNumber = 20;

will not be executed.     They will be skipped.     (I am not bothering to explain what these lines do, or what the program does in general.     The only thing it is important to understand is how the condition works.     But you should try to guess what this program does.)

        The above example is really too simple to be of interest.     Since nachumsNumber will always be 15, therefore the lines inside the braces will never be executed.     Later we will see examples in which the variable will change its value and therefore the condition will sometimes be true, and sometimes false.

        The two lines at the end, namely:

System.out.println("Number is");
System.out.println("not smaller than 10");


will always execute.     Why is this?     The reason is that the only section of code that is skipped is the that which lies between the braces.     ( A section of code inside braces is called a block of code.)     Since there is a closing brace before these two lines, they are not within the condition and are therefore not skipped.     They will be executed regardless of whether the condition is true or false.     These lines will write the words "Number is not smaller than 10" onto the screen.     Does it make sense to write this?     In our example the variable is 15 so it makes sense to print "Number is not smaller than 10." since this is true.     But what if nachumsNumber had a different value, say 5 ?     If the variable is smaller than 10 then it would be wrong to write "Number is not smaller than 10" on the screen.     If you look carefully at the example, you will see that if the condition is true, then nachumsNumber gets a new value.     It gets the value 20.     By the time the program prints the sentence "Number is not smaller than 10" on the screen, nachumsNumber is actually 20, so the message is true.

        Sometimes in life we have more complicated conditions.     We have two sided conditions.     For example I might say, "If today is rainy I will stay indoors, otherwise I will play football."     This statement means that only if today is rainy will I stay indoors, but if today is not rainy then I will play football.     If the day is sunny I will play football.     But also if the day is cloudy (but not rainy) I will play football.     If the day is cold I will play football.     Even if the day is snowy I will playfootball.     Only if the day is rainy will I stay indoors.     That is the meaning of otherwise.     Otherwise means, "in all other conditions" I will play football.     In Java we use the keyword else to mean otherwise.

        A simple programming example in English of the if else combition is

If variable is greater than 2,500,000,000 print "Number is too large", else print "Number is acceptable size."

Let us see the above English (also called psuedo-code because it is English which resembles computer languages or codes) translated into Java.

if (variable > 2,500,000,000)
{
       System.out.println("Number is too large.");
}
else
{
       System.out.println("Number is acceptable size.");
}


Notice that the else using braces   {   } the same way the if uses them.     In this example only one of the two senteces will be printed.     Which sentence gets printed depends on whether the condition of the if is true. If the condition is true, then the first sentence will be printed, otherwise ( or else ) the second sentence will be printed.     In any case, both sentences can never be printed.     The program has two options of what to print and the option will be selected in accordance with the truth or falsehood of the condition in the if statement.

        In Java there is also a keyword true and a keyword false. the keyword true is always true in a condition statement and false is always false. Therefore,

if(true)
is always true. There is a primitive data type called boolean which can be assigned true or false. It is useful as a variable in condition statements.

        Ifs can be nested one in side the other.

Table of all condition symbols and their meanings.

        Terms in this chapter:     condition, if, true, psuedo-code, braces, block of code, else, nesting,
© Nachum Danzig September 2003