Showing posts with label if else statement. Show all posts
Showing posts with label if else statement. Show all posts

Jul 5, 2020

Conditonal Statements ( if/else in JAVA ) | Java Language | Coding Winds

CONDITIONAL STATEMENTS (If/else)


Conditional statements in a program are set of statements written to check certain conditions. These are quite simple to implement, as they use the keywords ‘if’, ‘else’ and also ‘else if’ sometimes, which makes them as similar to a verbal communication. So let’s see the basic syntax of an if-else statement.

Syntax:

if (condition ) {

}

else {

}

So in the above syntax we can see that first we check a certain condition (after the keyword ‘if’) and if it’s true, we’ll execute the statements within the curly braces followed by the condition and if the condition is false then the compiler will execute the statements within the curly braces after the  ‘else’ keyword. So let’s understand with an example.

Ex:

import java.util.*;

 

public class Conditional_Statements  {

     public static void main(String[] args) {

          

         int num = -3;

        

         if(num<0) {

              System.out.println("Your number is negative");

         }

         else {

              System.out.println("Your number is positive");

         }

     }

     }

//Output: Your number is negative

 

    If else can be manipulated and used in different ways since it doesn’t provide any kind of limitations to the user. Similarly it’s not that we can only use the keywords ‘if’ or ‘else’ only once in our code at a time. See the example below to see  where can we use ‘if’ and ‘else keywords more than once in our code what difference it makes.

 

import java.util.*;

 

public class Conditional_Statements  {

public static void main(String[] args) {

 

int num = -3;

 

if(num<0) {

System.out.println("Your number is negative");

}

if (num<-2) {

System.out.println("Your number is smaller than -2");

}

else {

System.out.println("Your number is positive");

}

}

}

 

So in above example we can see that there are two ‘if’ keywords used, and thus in this case irrespective of the fact that the first condition is true or false , the second condition (after the second ‘if’ keyword) will be checked.

 

If-else Ladder

 

In the if-else ladder we can filter out a specific result or output using bunch of if-else statements. This is used generally if are output needs to go through a number of checks under number of conditions, so let’s understand its implementation with an example.

 

import java.util.*;

 

public class Conditional_Statements    {

     public static void main(String[] args) {

          

         int num = 7;

        

         if(num%2==0 && num!=0) {

              System.out.println("Your number is even and non-zero");

         }

         else if (num==1 || num %2 ==1 ) {

              System.out.println("Your number is odd");

         }

         else if (num<0) {

              System.out.println("Your number is negative");

         }

         else {

              System.out.println("Your number is zero");

         }

     }

     }

/*Your number is odd

 */

 

 

In the above example the first condition is checked (after the ‘if’ keyword) and then, only if the condition is false, the conditions after the remaining ‘else if’ keywords will be checked. This means that if any of the condition after an ‘ else if’  is true only that body of code will be executed which is there followed by the condition. And if all the above conditions will be false the body of code after the ‘else’ keyword will be executed.

 

 

Nesting of Conditional Statements

 

Nesting of conditional statements or if else statements means that we our writing a conditional statement inside a conditional statement.

Ex:

 

import java.util.*;

 

public class Conditional_Statements  {

     public static void main(String[] args) {

          

         int num = 0;

        

         if(num>0) {

              if(num%2==0) {

                   System.out.println("Your number is even ");

              }

              else if (num==1 || num %2 ==1 ) {

                  System.out.println("Your number is odd");

              }

         }

         else if(num<=0) {

         if(num==0) {

              System.out.println("Your number is zero");

         }

         else {

              System.out.println("Your number is negative");

         }

     }

         }

     }



Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!