Java: Conditionals And If...else  Statements

Java: Conditionals And If...else Statements

A BRIEF ARTICLE ON CONDITIONS AND IF STATEMENTS IN JAVA

INTRODUCTION

We all make our decisions in life based on the conditions around us. If so and so conditions are satisfied then only I will do so and so thing. For example, If I have $1,299 in my savings then only I can afford to buy the 13-inch MacBook Pro else I have to continue coding on my current laptop. All these are decisions that are made when a certain condition is being met. In Java, we can execute certain code when a specific condition is being met. Let us dive straight into the topic without any further ado...

LetUsBeginLetsStartGIF.gif

Conditionals

Java supports the usual comparison conditions. Those are given below:-

  • Less than condition:- a < b

  • Less than or equal to condition:- a <= b

  • Greater than condition:- a > b

  • Greater than or equal to condition:- a >= b

  • Equal to condition:- a == b

  • Not Equal to condition:- a != b

Java also supports logical conditions. Those are :-

Logical operators are &&, || and ! in Java, were

  1. && --> AND LOGIC

  2. || --> OR LOGIC

  3. ! --> NOT LOGIC

AND LOGIC TABLE

download.png

Here, instead of True and False some truth tables show 1 and 0. It is all one and the same, it makes no difference whether you take 1 and 0 or T and F. Here the logic is that the AND table multiplies the two values!

OR LOGIC TABLE

download (1).png

Here the logic is that the OR table adds the two values. Hence when both the values are False/zero then only we get false/zero as the output! You can remember the OR table in this way.

NOT LOGIC TABLE

Not-Truth-Table.png

Here the output is the complement of the input. It is the easiest logic table to remember.

The if-else Statement

Use the if statement to specify a block of Java code to be executed if a condition is True. Use the else statement to specify a block of code to be executed if the condition is False. Note that "if" is in lowercase letters. Uppercase letters (If or IF) will generate an error.

The syntax of the if-else statement in Java looks like this:

carbon (1).png

Let us see an example to get a clear understanding:-

carbon (3).png

The else condition in the last is not mandatory. In fact, it will not return any error if you don't write the else condition. Let us see this through an example:

carbon (2).png

The above code will not return any error, you can try it out.

The else-if clause

But what if we have multiple conditions to be fulfilled? Does this mean that we have to use multiple if statements? The answer is NO!!

Instead of using multiple if statements, we can use else-if along with if thus forming an if-else-if-else ladder

Since you are now familiar with the syntax of the if-else statement. Let us understand this if-else-if-else ladder with an example:

carbon (4).png

Short Hand If...Else

Also known as the ternary operator because it consists of three operands. This is the shortcut to writing the if-else statement. You can use this method also to derive the answer and you will get the same output. The syntax of this shortcut is as follow:

carbon (5).png

Let us see an example:

1) Using the if-else method

carbon (6).png

We get "Good evening" as the output. Now we will use the shortcut method to bring the same output.

2) Using the Short hand if...else

carbon (7).png

We get "Good evening" as the output!

You can use any method and it depends on the use cases. If you want to keep code short then you can use the Short hand if-else method and the rest of the time you can use the if-else method.

That's a wrap!

Thank you for reading till the end!

Thank you for Reading my Article📄 Hope it adds value to the community💙

Follow me on Hashnode VINAYAK PANCHAL