If Else clause in java

class Condition {
  public static void main(String[] args) {
    boolean learning = true;
    if (learning) {
      System.out.println("Java programmer");
    } else {
      System.out.println("What are you doing here?");
    }
  }
}

Step-by-Step Explanation:

1. Class Declaration:

 class Condition {

 It declares a class named ‘Condition’. It is the class that will contain the logic of the program, which is to check the condition using a boolean variable.

2. Main Method:

public static void main(String[] args) {

This is the ‘main’ method: it is where the program starts to run. In Java, any standalone application has a ‘main’ method as its entry point.

3. Declaring a boolean variable:

boolean learning = true;

This creates a boolean variable ‘learning’ and initializes it to ‘true’. A boolean variable may hold one of two values: ‘true’ or ‘false’.

4. If-Else Statement:

  if (learning) {

      System.out.println(“Java programmer”);

  } else {

      System.out.println(“What are you doing here?”);

  }

  • This ‘if-else’ statement evaluates the value of the boolean variable ‘learning’:
  • If ‘learning’ is ‘true’, then it proceeds with the very first block in which it has a print saying, ‘”Java programmer”.’
  • If ‘learning’ is ‘false’, then, naturally, the ‘else’-block is the one executed saying, ‘ “What are you doing here?”
  • Now since ‘learning’ is also set to a ‘true, the program has printed, ‘”Java programmer””.

5. Program Execution.

In the case under study, as ‘learning’ holds ‘true’, the program would go by the ‘if’ branch.

Output:

Considering ‘learning’ to be ‘true’, the output is going to be:

Java programmer

Summary

  • In the above example, a program declares a variable named ‘learning’ and its value is assessed through an ‘if-else’.
  • If the ‘learning’ variable holds a value ‘true, it will print ‘”Java programmer”.
  • If ‘learning’ is ‘false’ then it will print ‘What are you doing here?’
  • Since learning is ‘true’, the output is ‘Java programmer’.

Leave a Comment

Your email address will not be published. Required fields are marked *