if else program in java

 If else in java language

If........else

  • It is a selectional control statement which has a particular condition and that condition always be enclosed within a pair of parenthesis.
  • It has a body part and all the body statements are enclosed within a pair of curly brackets.
  • In this section the control will first check the condition,if the condition is true then the control will execute the statements of the if body part  other wise it execute else body.
Syntax 

    if(condition)
    {
        statement 1;
        --------------
        --------------
        statement n;
    }
else
    {
        statement 1;
        --------------
        --------------
        statement n;
    }
Q.A program to check whether a number is even or odd.(using command line arguments)

class Sibani
{
public static void main(String args[])
{
int no;
no=Integer.parseInt(args[0]);
if(no%2==0)
System.out.println("no is even");
else
System.out.println("no is odd");
}
}

Answer
compile:-     D:\>javac Sibani.java
                    D:\>java Sibani 5
Output:- no is odd
Q.A program to check whether a number is positive or negative.(using command line arguments)

class Sibani
{
public static void main(String args[])
{
int no;
no=Integer.parseInt(args[0]);
if(no>0)
System.out.println("no is + ve");
else
System.out.println("no is - ve");
}
}

Answer
compile:-     D:\>javac Sibani.java
                    D:\>java Sibani -5
Output:- no is - ve

Q.find out biggest number among two numbers.(using command line arguments)

class Sibani
{
public static void main(String args[])
{
int no1,no2
no=Integer.parseInt(args[0]);
no=Integer.parseInt(args[1]);
if(no1>no2)
System.out.println("no1 is biggest");
else
System.out.println("no2 is biggest");
}
}

Answer
compile:-     D:\>javac Sibani.java
                    D:\>java Sibani 10 20 
Output:- no2 is biggest










    

No comments: