DEV Community

Cover image for Program Control Statements in Java
Kumar Sanskar
Kumar Sanskar

Posted on

Program Control Statements in Java

Hello readers, it has been a wonderful journey of learning Java till today and today is day eight of my learning Java. By now since you must have read the title of the post you must be aware that I must have studied Program Control statements today and so it is but since it contains a lot of topics I will be posting about them in parts.
Today I will discuss about the various if statements with their examples.

The if statement -

  • like we use the if in our normal English language in order to show and make use of the condition like for example - If Ram finishes his homework in time, his mother will reward him.

Here we show a condition that if Ram does his homework in time he would be rewarded so is the case with the if statements in Java programming it is used in programming to control the flow of program and decision making.

  • Syntax -
    if(condition)
    {
      statements
    }
Enter fullscreen mode Exit fullscreen mode
  • example : -
public class UsingIfStatement {
    public static void main(String[] args) {
        // here we demonstrate using the if statement in a program.

        int numberOfBoys = 29;
        if (numberOfBoys == 29) {
            System.out.println("There are twenty-nine male students in the class.");
        }
        System.out.println("This statement is general statement and is independent of if block .");
    }
}
Enter fullscreen mode Exit fullscreen mode

The if-else Statement -

  • it is similar to the 'if' statement discussed above but the only difference is that it contains an else so if the provided condition evaluates to false then other set of statements will be executed.
  • Syntax -
  if(condition)
    {
      statements
    }
  else(condition)
   {
      statements
   }
Enter fullscreen mode Exit fullscreen mode
  • exapmle -

public class UsingIfElseStatement {
    /*
     * here we make a simple program which checks for the number to be even or odd using if else block.
     */
    public static void main(String[] args) {
        int checkNumber = 7;
        System.out.println("Check whether number 7 is even or odd? ");

        if (checkNumber % 2 == 0) {
            System.out.println("Number is even.");
        } else {
            System.out.println("Number is odd.");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Nested if Statements -

  • an if statement is said to be nested if statement when it is a result of another if statement in an if-else block
  • exapmle -
public class NestedIfStatements {
    /*in this program we learn how to use nested if statement in java
     *
     * here if first condition evaluates to true then second condition is checked to produce an output.
     */

    public static void main(String[] args) {
        int number1 = 4, number2 = -1;
        if (number1 > 10) {
            if (number2 > 0) {
                System.out.println("Number1 is = " + number1 + " and " + "Number2 is = " + number2);
            }
        }
        else{
            System.out.println("Sorry number1 and number2 didn't fall  in range of 10 and greater than 20.");
        }

    }
}
Enter fullscreen mode Exit fullscreen mode

if else-if else ladder -

  • it is also like if and if else and nested if conditions but differs in a fact that as soon as condition evaluating to 'true' is found the statement related to it is executed and rest is discarded and if none of the conditions are evaluated as true then 'else' condition is executed. +
public class UsingIfElseifElseStatement {
    /*
     *here we check that whether a number is positive, negative or equal to zero.
     */
    public static void main(String[] args) {
        int checkNumber = 7;
        System.out.println("Check the number 7 whether it id positive, negative or zero. ");


        if (checkNumber > 0) {
            System.out.println("Number is Positive.");
        } else if (checkNumber < 0) {
            System.out.println("number is Negative.");
        } else {
            System.out.println("Number is zero.");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

So, that's it for the day in program control statements tomorrow we will be discussing the other remaining topics in that, till then keep learning keep growing.
If you encounter any discrepancies feel free to point out or give any suggestions.

Top comments (0)