Java If-else Statement

Java If-else Statement is used to test condition.It boolean true is returned,the code in between the if statement is executed. Java If-el...

Java If-else Statement is used to test condition.It boolean true is returned,the code in between the if statement is executed.

Java If-else Statement  Syntax.

[if(condition)
        {
        
        //code to execute if boolean true is returned
}]

Types of if statements

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

Java if Statement example

[
public class JavaIfStatement {
    public static void main(String args[])
    {
        int age=30;
        if(age>=18)
        {
            System.out.println("Your Illegiable to be a voter");
        }
    }

}
]


Java if-else statement

Java if-else statement executes code if boolean true is returned.If false is returned, else code is executed.

Syntax

[
if(condition to test)
        {
        //execute this code                }
else        {
        //execute this code
}
]

Example:
[
public class JavaIfStatement {
    public static void main(String args[])
    {
        int age=13;
        if(age>=18)
        {
            System.out.println("Your Illegiable to be a voter");
        }
        else        {
            System.out.println("You cannot vote..too young");
        }
    }

}
]

Java if-else-if ladder Statement

In the if-else-if ladder, one condition is executed from multiple statements.

Syntax:

[
if(condition 1)
        {
        //code to execute
        }
else if(condition 2)
        {
        //code to execute        }
else if(condition 3)
        {
        //code to execute        }
.
.
.
 else        {

        //code to execute        }
]

Example:

[
public class JavaIfStatement {
    public static void main(String args[])
    {
        int score=30;
        if(score<50)
        {
            System.out.println("Your grade is D");
        }
        else if(score>50&&score<=60)
        {
            System.out.println("Your grade is C");
        }
        else if(score>60&&score<=70)
        {
            System.out.println("Your grade is B");
        }
        else if(score>70&&score<=80)
        {
            System.out.println("Your grade is A-");
        }
        else        {

            System.out.println("Your grade is A");

        }
    }

}
]

COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Java If-else Statement
Java If-else Statement
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/java-if-else-statement_11.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/java-if-else-statement_11.html
true
5534598864497432585
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy