Monday, June 1, 2009

Java tutorial if-else statement

Java tutorial if-else statement:

The if-else statement is used when we want to execute a certain statement if a condition is true, and a different statement if the condition is false.

The if-else statement has the form,

if( boolean_expression )
statement;
else
statement;

or can also be written as,

if( boolean_expression ){
statement1;
statement2;
. . .
}
else{
statement1;
statement2;
. . .
}

For example, given the code snippet,

int grade = 68;

if( grade > 60 ) System.out.println("Congratulations!");
else System.out.println("Sorry you failed");

or

int grade = 68;

if( grade > 60 ){
System.out.println("Congratulations!");
System.out.println("You passed!");
}
else{
System.out.println("Sorry you failed");
}

Related Post:

Java tutorial source code get input from keyboard

Java Tutorial Using JOptionPane to get input

Java tutorial Decision Control Structures: if statement

PHP Tutorial

Java programing source code sample printing characters

What is programming?

C programming sample source code: Sum of 2 numbers

Java Programming Features