Explain the working of IF-ELSE with the help of an example program.
Advertisements
Question
Explain the working of IF-ELSE with the help of an example program.
Code Writing
Explain
Advertisements
Solution
The IF-ELSE statement in Java is a decision-making statement that executes one of two blocks of code depending on whether a given condition is true or false.
Working of IF-ELSE:
- The
ifstatement checks the specified condition. - If the condition is true, the
ifblock is executed. - If the condition is false, the
elseblock is executed. - After executing either block, the program continues with the next statement.
Example Program:
class IfElseExample
{
public static void main(String[] args)
{
int num = 10;
if (num % 2 == 0)
{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number");
}
}
}
Output:
Even Number
shaalaa.com
Is there an error in this question or solution?
