English

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:

  1. The if statement checks the specified condition.
  2. If the condition is true, the if block is executed.
  3. If the condition is false, the else block is executed.
  4. 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?
Chapter 7: Statements and their Scope - EXERCISE [Page 163]

APPEARS IN

Dheeraj Mehrotra Computer Science [English] Class 12
Chapter 7 Statements and their Scope
EXERCISE | Q 2. | Page 163
Advertisements
Share
Notifications



      Forgot password?
Use app×