Net-informations.com
SiteMap  | About    

Java if-else statement

Decision making is an important part of programming. It is used to specify the order in which statements are executed. In this lesson, you will learn how to create decisions using different forms of if...else statement.

Java if statement

In Java, an if statement is a conditional statement that runs a different set of statements depending on whether an expression is true or false.

Syntax:

In the above syntax, the if statement evaluates the test expression inside parenthesis. If test expression is evaluated to true (nonzero) , statements inside the body of if is executed. If test expression is evaluated to false (0) , statements inside the body of if is skipped.

How to java if else statements

Example:

Output:

In the above code, the if stat evaluate the expression is true or false. In this case the totalMarks>50 is true, then the control goes to the body of if block , that is the program will execute the code inside if block.

Java if...else Statement

The else statement is to specify a block of code to be executed, if the condition in the if statement is false.

Syntax:

The else clause of an if...else statement is associated with the closest previous if statement in the same scope that does not have a corresponding else statement.

Example:

Output:

In the above code, the if stat evaluate the expression is true or false. In this case the totalMarks>50 is false, then the control goes to the body of else block , that is the program will execute the code inside else block.

Java if...else if Statement

If you want to evaluate more than one conditions at the same time , you can use else if statement in Java. Multi selection enables the developer to determine the actions that have to be accomplished in certain conditions by imposing a requisite. You can combine an else and an if to make an else if and test a whole range of mutually exclusive possibilities.

Syntax:

Example:

Output:

Explanation:

  1. 1 : Checking the total marks greater than or equal to 80
  2. 2 : If total marks greater than 80 show message - "Got Higher First Class "
  3. 4 : Checking the total marks greater than or equal to 60 and less than 80
  4. 5 : If total marks greater than 60 show message - "Got First Class "
  5. 7 : Checking the total marks greater than or equal to 40 and less than 60
  6. 8 : If total marks greater than 40 show message - "Just pass only"
  7. 10: If those three conditions failed program go to the next coding block .
  8. 11: If all fails, it will show message "Failed"









net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.