Saturday, February 4, 2017

Learn Java for Beginner easily ! part 18 : Else If Statement

     What's up Guys ! Welcome back in my Java tutorial series. First, I want you guys to know that this part is not too far away from the previous part . In this part of tutorial I'm gonna show you something else on how to use If statement but It kinda different. It's called else if statement. This kind of trick of how to use if statement will have its own function depend on how you want your program works.But, in my point of view, this one is easier to be learned.

First, as usual guys I want you to type this program below and run it. I'm sure you guys are like kinda understand this easily because if you've understand the previous part . The different is, that in this one we will see less brackets because each condition only have one code to be excecuted.


So this one is kinda simple than the nested if  I guess. So this program works by scanning every condition to match it with the value of the variable. In this program, it has 5 statements + 1 else statement as default code to be excecuted in case if none of the if statement matches the variable's value. I mean, first it's gonna compare the variable's value with the if statement , if  it's matched, then the code inside the if statement will be excecuted and the program stop. If not, then the variable's value will be matched with the else if statements under it and if one of the if statement is matched, then the codes inside the matched statement will be excected then program stop.

The conclusion is the compiler will keep comparing the variable's value with the statements until it found the one that matched the requirement with the value. In the case of codes above, I input the variable's value with 70. So the statement's requirment that match the value will be choosen and the codes inside it will be excecuted. As an example just to make this more clear, I'm gonna change the value with 90. Just like the picture below.


On the codes above, none of the if and else if  statements that matches the variable's value. That's why the one that being excecuted is the else statement as a default codes.

So that's it guys for now. Thank you guys for visiting my blog, hope you guys enjoy it and see you on the next tutorial. If you guys got something to be asked, please leave a comment below and thank you very much.

Greetings from Programmer !

No comments:

Post a Comment