0% found this document useful (0 votes)
128 views

If Else Statement in Java

The document discusses if-else statements in Java. It explains that an if-else statement can have an optional else block that executes when the boolean expression is false. The syntax is shown as an if block executing when the boolean expression is true, and an else block executing when it is false. An if statement can also be followed by optional else if blocks to test multiple conditions using a single if-else statement. The else if blocks are tested in order, and once one succeeds none of the remaining else if or else blocks will be executed. An example is provided to demonstrate an if-else if-else statement.

Uploaded by

danilo sugot
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

If Else Statement in Java

The document discusses if-else statements in Java. It explains that an if-else statement can have an optional else block that executes when the boolean expression is false. The syntax is shown as an if block executing when the boolean expression is true, and an else block executing when it is false. An if statement can also be followed by optional else if blocks to test multiple conditions using a single if-else statement. The else if blocks are tested in order, and once one succeeds none of the remaining else if or else blocks will be executed. An example is provided to demonstrate an if-else if-else statement.

Uploaded by

danilo sugot
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

IF ELSE STATEMENT IN

JAVA
If else statement can be followed by an optional
ELSE statement, which execute when the
boolean expression is false.
SYNTAX:
The syntax of an if else:
if (boolean_expression){
//Execute when the boolean expression is
true
}else{
//execute when the boolean expression is
false
}

If the boolean expression evaluate to true , then


the if block of code will be execute , otherwise else
block of code will be executed.
Example:
Public class test {
public static void main (string args[]{
int x= 30;
if ( x < 20 ){
system.out.print (this is if statement);
}else{
system.out.print (this is else statement);
}

The if. Else if. else statement


If statement can be followed by an optional else
if else statement , which is very useful to test
various condition using single if else if
statement.
An if can have zero or one else and it must
come after any else if.
An if have zero to many else if an they most
come before the else
Once an else if succeeds , none of the
remaining else if or else will be test.

SYNTAX:
if (boolean_expression 1 ){
//Execute when the boolean expression 1 is true
}else if (boolean_expression 2){
//Execute when the boolean expression 2 is true
}else if (boolean_expression 3){
//Execute when the boolean expression 3 is true
}else {
//Execute when the none of the above condition is
true.
}

EXAMPLE:
public class test {
public static void main (string args[]){
int x = 30;
if ( x == 10 ){
system.out.print( value of x is 10);
}else if ( x == 20 ) {
system.out.print( value of x is 20);
}else if ( x == 30 ) {
system.out.print( value of x is 30);
}else {
system.out.print(this is else statement);

You might also like