"If" in Java is similar to the 'if' we used in our normal language. Suppose the conditions you state in the "if" statement is met, it will execute a series of codes which you wrote for the particular condition.
The following will be a simple example showing what I meant, showing is often more effective than explaining and putting everything in words. Visualizing is definitely a powerful tool for learning. So let's cut the talk and look at what we have got.
For the above, I have created a java application project with a class for If and Switch illustration purposes. If you have troubles launching the application and creating the project or class, you may refer to my previous tutorial as there will be links and instruction on how to set things up for java programming purposes. If you would like a shortcut, you may click here! For now, let's talk about the code above.
Basically, the beginning of the code would reflect a variable 'x' which I want the variable to be an integer. In this case, I keyed in 'int x' in the program and use the assignment operator '=' to assign an integer 5 to the variable. After which, the ';' will reflect the end of my first line of code. These are basically the basics and if you need to refresh on the basics, you may refer to my previous tutorial as well.
Now, for the if statement, I will need a condition that the program would need to fulfill and sets up how the program would counter the case when the condition is fulfilled. Therefore, in the above case, my condition would be if x is exactly the integer 5, I want the system to print out something to show the user that the number I chose is 5. "if(x == 5)" would mean that if x is the integer 5 is true, then we will print out the string "The number is 5!". Just to refresh a little, in order for us to print a sentence we use the function "System.out.print" or System.out.println".
Here! We have our result for the above program. Since I assigned 5 to the variable x, the condition in the if statement would be true. There are no syntactical errors in the program, hence, we are able to smoothly get the result we expected. However, suppose that you get an error in your program, you will need to debug the lines in which the errors are shown. It may take some time to get used to the Java programming language, however, with more practice, I believe that all of us could do it.
Now, a logical thought would be what if the integer we choose is not 5, what will happen to the program. Well, then let's try choosing an integer that is not 5 and see what will happen to our program.
Here! What will happen to our program in this case? (Guess I will have to answer that on my own, that is one of the cons of doing blog post tutorial, there is no interaction at all.) Since we assigned 6 to the integer variable x, the condition for our if statement will be false now. And so...
Nothing will appear in our console even if we run our code. There is no code for the case when the integer assigned to x is not 5.
We can actually do something to deal with the case when integer chosen is not 5. Let's take a look at the code before I proceed to explain how we do that.
Have you spot the difference? Yes, there is this additional else statement. The else statement allows us to deal with all other conditions that are not stated in the if statement. In this case, it will deal with the cases when the integer assigned to x is not 5. Since else statement deals with all other cases, we will not have to set up a condition for the else statement itself. We will need to a set of codes to deal with the situation. In my case, I would print out the statement "Choose the number again!", you may choose to type in something else.
Let us check our result. Yes, this is expected, as the integer assigned to the variable x is 6, the if condition is not met. Hence, it will proceed to do what we wrote under the else statement.
How about the case where we have different conditions?
Apart from the else statement, we have another statement for dealing with cases with two or more conditions. The else if statement allows you to add as many conditions as you would like to have in your program. To illustrate this, I wrote the above program to deal with all three conditions in this case: x == 5, x < 5 and x > 5. All these three conditions would cover for all the cases we can have in this program, and hence the else statement in this program is unnecessary because the line will never be executed.
So here would be the result for the above program that I wrote:
Since the integer we assign to the variable x is 10, the program would read the if condition x == 5 as fasle and proceed to the next else if condition x > 5. Since the integer chosen is indeed more than 5, it will print out the statement "The number is more than 5."
Hope this tutorial helps you understand the if, else if and else statements in Java programming better. Try to practice writing you own programs with such statements to understand how they work better.
If you have any questions, you may comment or leave a message, I will try my best to reply to it. Of course, I am not Google, don't expect me to know everything. Though, even Google does not know everything as well.
Enjoy your weekend. Have a good rest and enjoy yourself to the maximum before we return to our mundane and meaningless compulsory activity which ensure our survival in the current era.
No comments:
Post a Comment