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

Java -Conditional Statements

The document provides an overview of conditional statements in Java, including simple if, if-else, ladder if-else, nested if-else, and switch statements. It includes syntax examples, sample Java code, and expected output for each type of conditional statement. Additionally, it presents programming exercises to demonstrate the application of these concepts, such as printing the name of the day and checking if a number is positive, negative, or zero.

Uploaded by

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

Java -Conditional Statements

The document provides an overview of conditional statements in Java, including simple if, if-else, ladder if-else, nested if-else, and switch statements. It includes syntax examples, sample Java code, and expected output for each type of conditional statement. Additionally, it presents programming exercises to demonstrate the application of these concepts, such as printing the name of the day and checking if a number is positive, negative, or zero.

Uploaded by

flowerk401
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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 !

You might also like