if statement in Java;
In this article let's see the syntax of if, and how it works?
Syntax:
How if works?
- It is used to test the condition.
- If the condition is true then only its body will execute, otherwise it does not execute.
Now let's see examples.
Example 1:
class Easy
{
public static void main(String[] args)
{
int x=10;
if(x>5)
{
System.out.println("x is greater than 5");
}
}
}
/ ### Output ###
x is greater than 5
Example 2: Check given number is Negative or Positive or Zero
import java.util.Scanner;
class Easy
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int no;
System.out.println("Enter any number");
no=in.nextInt();
if(no>0)
System.out.println("number is positive");
if(no<0)
System.out.println("number is negative");
if(no==0)
System.out.println("number is zero");
}
}
/ ### Output ###
Enter any number -5
number is negative /
For Videos Join Our Youtube Channel: Join Now
Thank you!
0 Comments