If statement in Java

If statement is one of the conditional statements which are used to work on specific condition.

General Syntax for if

if(condition or expression) {

//body or statements


If a condition is true, it will go inside the body of the if statement.

Imagine you need 2 pens of a specific type from a pen shop. The pen shop has 16 pens of that type. So he can give pens to you. Right ?

How will you solve this using programming ?

You can imagine needed_pen as one variable and available_pen as another.

Solve like this below,

public class IfTest {

public static void main(String args[]) {

int pens_needed=2,available_pens=16;


System.out.println("You can take the pen");





Test In Online Compiler


You can take the pen