Decision-making in Java

In today’s topic, we will discuss decision-making in Java. These are very important concepts that are necessary to remember before writing a Java program. Previously we also describe briefly various topics of Java such as Operator in Java, History of Java, Difference between C++ & Java, and many more which is really helpful for your better understanding. So without wasting time let’s dive into our topic:

Decision-making in Java

In Java Decision making statement is quite similar to our day-to-day life. So here also it performs the same as real life. It executes particular lines of code which are based on the result of its provided condition. It is too much important to define and control the flow of execution of a program.

Decision-making in Java has various conditions which are evaluated through the program with one or more statements or a certain block of code that are to be executed when some condition is fulfilled i.e. true or false.

This principle in Java consists of various statements such as if-else statements, continues, break and switch statements, etc. which decide the flow of the program during the execution the program.

There are different types of decision-making principles in Java:

  • if
  • if-else
  • if-else-if
  • nested-if
  • switch-case
  • break, continue

if statement

if statement in Java is one of the simplest decision-making statements used to decide whether the given condition is true or false. After checking the condition if the given condition is true then the body of if block is executed otherwise it is not executed.

The body of if block is defined within a pair of curly braces.

Syntax:-

if(condition) 
{
   // Statements to execute if
   // condition is true
}

Example:-

// Example of If statement in Java by GeektoCode
public class GeektoCode {
        public static void main(String args[]){
            int i = 10;
            if (i < 15) {
                System.out.println("Yes " + i + " is less than 15");
            }
            System.out.println("This is outside the loop");
        }
}

OUTPUT:-

Yes 10 is less than 15
This is outside the loop

So this is all about the decision-making in Java. I hope this is helpful to you. In the next upcoming post we will discuss another new topic of java, so wait for this will meet in the next post. Thank you.

Leave a Reply

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