More

    If-else statements, syntax and examples in java

    If-else statements in java

    When we have to execute a set of statements based on a condition, we need control flow statements. For example, if a number is greater than zero then we want to print “positive number” but if it is less than zero then we want to print “negative number”. In this case we have 2 print statements in the program but only one print statement is executed based on the input value.

    In this notes, we will see how programs are written in Java based on a particular condition using control statements. In this notes we will see 4 types of control statements that you can use in Java program depending on the requirement, in this tutorial we will look at the following statements in detail.

    • if statement
    • nested if statement
    • if-else statement
    • if-else-if statement

    if statement

    The if statement checks the condition as shown below.

    if (condition) {
    statement(s);
    }

    If the condition is true then the statements are executed and if the condition is false then the statement body is completely ignored.

    Examples

    public class IfStatementExample {
    
      public static void main(String args[]){
       int num=70;
       if(num < 100){
    	 /* This println statement will only execute,
    	  * if the above condition is true
    	  */
    	 System.out.println("number is less than 100");
       }
      }
    }

    Output
    number is less than 100

    Nested if statement

    When there is another if statement inside if statement then it is called nested if statement. nested if statement is written as

    if(condition_1) {
    Statement1(s);
    
    if(condition_2) {
    Statement2(s);
    }
    }

    Here statement 1 will execute only if condition1 is true and statement 2 will execute only when both condition1 and condition2 are true.

    Examples

    public class NestedIfExample 
    {
    	public static void main(String args[])
    	{
    		int num=70;
    		if(num < 100)
    		{
    			System.out.println("number is less than 100");
    			if(num > 50)
    			{
    				System.out.println("number is greater than 50");
    			}
    		}
    	}
    }

    Output

    number is less than 100
    number is greater than 50

    Cyber ​​Security Threats and Types

    if-else statement

    The if-else statement is written like this.

    if(condition) 
    {
    	Statement(s);
    }
    else 
    {
    	Statement(s);
    }

    If the condition is true then the statements inside “if” will execute and if the condition is false then the statements inside “else” are executed.

    Examples

    public class IfElseExample{
    	public static void main(String args[]){
    		int num=120;
    		if(num < 50){
    			System.out.println("num is less than 50");
    		}
    		else {
    		System.out.println("num is greater than or equal 50");
    		}
    	}
    }

    Output
    num is greater than or equal 50

    if-else-if statement

    The if-else-if statement is used to test multiple conditions. In this statement we have only one “if” and only one “else” although we can also have multiple “else if” It is also known as if-else-if ladder. The if-else-if statement is written as:

    if(condition_1) {
    /*if condition_1 is true execute this*/
    statement(s);
    }
    else if(condition_2) {
    /* execute this if condition_1 is not met and
    * condition_2 is met
    */
    statement(s);
    }
    else if(condition_3) {
    /* execute this if condition_1 & condition_2 are
    * not met and condition_3 is met
    */
    statement(s);
    }
    .
    .
    .
    else {
    /* if none of the condition is true
    * then these statements gets executed
    */
    statement(s);
    }

    note : – The most important thing to note here is that as soon as the condition is found true in the if-else-if statement then the set of statements is executed and the rest is ignored. If no condition is met, then the statements inside “else” are executed.

    Examples

    public class IfElseIfExample {
    public static void main(String args[]){
    int num=1234;
    if(num <100 && num>=1) {
    System.out.println("Its a two digit number");
    }
    else if(num <1000 && num>=100) {
    System.out.println("Its a three digit number");
    }
    else if(num <10000 && num>=1000) {
    System.out.println("Its a four digit number");
    }
    else if(num <100000 && num>=10000) {
    System.out.println("Its a five digit number");
    }
    else {
    System.out.println("number is not between 1 & 99999");
    }
    }
    }

    Output
    Its a four digit number

    What is a Thumb drive and how does it work

     

     

    Recent Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox