Java- Conditional Statements K.
S Sir
Simple If condition
Syntax Sample Java Code Output
if(condition)
{
// this block of statements
gets executed when the
condition will be true
}
If-Else condition
Syntax Sample Java Code Output
if(condition)
{
// this block of statements gets
executed when the condition will be
true
}
else
{
// this block of statements gets
executed when the condition will be
false
}
Ladder If Else statement Syntax
if(condition 1) else if ( condition 4)
{ {
// this block gets executed when condition 1 is true and
//rest of the conditions will not be checked // this block gets executed when condition 2 is true
//and rest of the conditions will not be checked
}
}
else if ( condition 2)
else
{
{
// this block gets executed when condition 2 is true and
//rest of the conditions will not be checked // this block gets executed when none of the
condition is true
} }
else if(condition 3)
{
// this block gets executed when condition 3 is true and
//rest of the conditions will not be checked
}
Write a program to print name of the day from the given number Java code
Sample Output:
Sun
Nested If Else statement Syntax
if(condition 1) else
{ {
// this block of statements gets executed when condition
1 is true // this block of statements get executed when
condition 1 is false
if ( condition 2) }
{
// this block gets executed when condition 1 and
condition 2 will be true
}
else
{
// this block of statements get executed when condition
1 is true and condition 2 will be false
}
}
Write a program to check whether given number is positive,
Java code
negative or zero
Sample Output:
Positive
Switch statement Syntax
switch(choice)
case value3:
{
// this block of statements get executed when choice has
value 3
case value1:
// this block of statements get executed when choice has default:
value 1
// this block of statements get executed when choice has
value other than value1, value2 and value3.
case value2:
// this block of statements get executed when choice has }
value 2
Write a program to print name of the day from the given number Java code
Sample Output:
Mon
Thank you !