What are If Statements?

"If statements" are a very easy way of determining how a computer chooses between certain chunks of code. Obviously when we write programs, most of the time we don’t want a computer to process all of its code, only certain parts! That’s why we use "if statements"! "If statements" are exactly what the name suggests; if a certain thing happens, then other things happen.

Scanner input = new Scanner(System.in);
System.out.println (“Enter a number”);
int number = input.nextInt();
if (number <= 10) {
System.out.println (“Less than or equal to ten!”);
}
else {
System.out.println (“Greater than ten!”);
}

This is a really basic program, all we want is for the computer to print out one of two different statements to the user. Either a statement saying “Less than or equal to ten!”, or a statements saying “Greater than ten!”. Neither of these are particularly important, but what is important is the reasons why the computer differentiates.

Firstly we started with a Scanner Class! If you’re unfamiliar with this then all a Scanner Class does is allow for the programmer to input a number, or characters from the keyboard, into either an integer or a String!

We’re using it because we want the user to type in a number. If the number typed is greater than ten we want the computer to tell the user that the number is greater than ten! If the number is less than or equal to ten, we want the computer to tell the user that the number is less than or equal to ten, very simple.

After we have this Scanner set up we ask the user to input a number (this is called a prompt).

System.out.println (“Enter a number”);

We then store that number in an integer!

Int number = input.nextInt();

If you’re unfamiliar with data types, please look at the data type article. We then give the number an assigned value of whatever the user types, which is why we say input.nextInt(). We’re simply asking the computer to use the Scanner (called “input”) and then whatever the next integer entered is equal to. Then we get to the "if statements"!

You’ll notice we have split the "if statements" into type different types:

if (number <= 10) {
System.out.println (“Less than or equal to ten!”);
}
else {
System.out.println (“Greater than ten!”);
}

“If” and “else” are the two different types we are using. Except they’re not different types, they’re two different parts to an if statement! Let’s start with the top statement. On the line “if (number <=10)” we are really telling the computer to check with the value of number is. Seeing as the user entered the number, if the user entered a 24 then we know the number ISN’T less than or equal to ten. This means that the statement is false. If the user entered a 9, and we know that 9 is less than ten, then the statement is true! If the statement is true, what the computer will do, is run the code in the brackets!

System.out.println (“Less than or equal to ten!”);

This will give the user an output of the following:

Less than or equal to ten!

This is what we want if the number is less than or equal to ten. What happens if the statement is false? Instead of running the code inside the curly brackets, we move past them entirely! So the code “Less than or equal to ten!” isn’t run at all. What does this mean for our program? Well, we move down to the next line the computer is allowed to process:

else {
System.out.println (“Greater than ten!”);
}

This is interesting, because we know that the number isn’t less than or equal to ten, therefore this code MUST have to run in order for this program to be correct. It doesn’t say “if” though, it says “else”. That’s because “else” is in fact a different statement altogether. Notice how after “else” there aren’t brackets with calculations like there is in fact statements. This is because what else means is that if the “if statement” isn’t true, then else will run in its place! It’s easier to understand if we think about these two statements in plain English.

java “If A is true then do A, else do B.”

So we know that whenever an “if statement” is false, we do “else” instead. This is great because this means we don’t have to check what the value of number is. If we have a number that isn’t less than ten and isn’t equal to ten, then the only available option left is a number that is greater than ten! In this case, we run the code in the else statement:

System.out.println(“Greater than ten!”);

This prints out the message

Greater than ten!

which in our case where the user entered 24, is correct!

So why do we use If Statements?

We use them because fairly often we need to distinguish all the possible variants that users will input into the program. We can’t leave this program with only one “if statement” that says “less than or equal to ten!”. If the user enters anything above a ten, then the program wouldn’t run. Sometimes, it might seem pointless to write these things, we can even make the argument that it’s so highly unlikely that certain choices would be made by users that writing more statements than we think we need is just a waste of memory.

Ultimately, it depends on the program you have and we need to do our best to account for any possible outcomes. If we have bugs that arise from certain decisions then an “if statement” is a great way to do some simple true or false statements to stop the program from running with an unintended bug.

Like anything we all learn in programming, practice makes perfect (get used to seeing that phrase). Do some research, do some homework, and keep trying "if statements".

Comments

blog comments powered by Disqus