Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

Saturday, 25 June 2011

Nested if statement of java and its uses


Nested if:
                 It can be defined as an if statement containing another if statement with in its body/block. When we have to make one decision from multiple decisions we use nested if. A nested if is an if statement that is the target of another if or else. When you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. 

General syntax:
                           if (condition)      
                           {
                            if (condition)     
                            if (condition)         /* this if is associated with */
                            statement / block
                            else                      /* this else*/
                            statement / block
                            }
                            else                     /*and this else is associated with the most first if*/
                           statement / block
Here, in the block of if the second if in it is associated with the first if within the block. Remember an else always refers to the nearest if without an else and within the same block.

Java program using nested if


import java.util.*;
class nestedif
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b,c,d;
System.out.print ("\n\t Enter first number = ");
a=in.nextInt();


System.out.print ("\n\t Enter second number = ");
b=in.nextInt();

System.out.print ("\n\t Enter third number = ");
c=in.nextInt();

 if (a<b)
{
if (a<c)
System.out.println ("\n\t First number is smallest ");
else
System.out.println ("\n\t First number is not smallest");
}
else if (b<a)
{
if (b<c)
System.out.println ("\n\t Second number is smallest ");
else
System.out.println ("\n\t Second number is not smallest ");
}
else
System.out.println ("\n\t Third number is smallest ");

}
}

Note: 
          In the above program, after entering the values, computer will check that either a is less than b or not. If it is than it will check the other if condition in its block. If it is true then it will show the message 
"First number is smallest" otherwise the statement associated with else will be executed and rest of the code will be bypassed.

What is type casting?


Type casting:
            If we want to store an integer value in a byte or short type variable. What we will do? When we need to store a large value in a small data type variable we use type casting. In type casting we explicitly make a value narrow so that it will fit into the targeted variable. 

General syntax:
                  Two incompatible/different data types can be converted into same. Its general syntax is
  • (target-type) value;
Here, target-type is the casting type, that data type in which your given value will be converted and value can be any variable or constant value.

Click here to see a program of type casting

Java program to cast integer into short and byte data type


import java.util.*;
class casting
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a;
short b;
byte c;

System.out.print ("\n\t Enter the value = ");
a=in.nextInt();

b=(short)a;
c=(byte)a;

System.out.println ("\n\t value of a in short form = "+b);
System.out.println ("\n\t Value of a in byte form= "+c);

}
}

Note:
         In the above program you might have noticed these two statements
  • b=(short)a;
  • c=(byte)a;
these are the actual statements that are casting the data type of a into short and byte.

Friday, 24 June 2011

Java operator precedence chart


Highest
Operator
()  []  .
++  --  ~  !
*/  %
+  -
>>  >>>  <<
>  >=  <  <=
==  !=
&
^
|
&&
||
?:
Lowest
=  *=  /=  %=  +=  -=



Wednesday, 22 June 2011

Logical operators and their uses in java programming


Operator
Name
||
OR
&&
AND
==
Equal to
!=
Not equal to
?:
Ternary operator
  • Click here to see a program using EQUAL TO operator
  • Click here to see a program using NOT EQUAL TO operator
  • Click here to see a program using TERNARY operator
 

Use of TERNARY operator in java program


import java.util.*;
class ternary
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b,c;

System.out.print ("\n\t Enter first number = ");
a=in.nextInt();

System.out.print ("\n\t Enter second number = ");
b=in.nextInt();

a==b ? c=1:c=5;
System.out.println ("\n\t c = "+c);

}
}

Note:
          After entering values, in the above program, computer will check that whether 'a' is equal to 'b' or not. If 'a' is equal to 'b' than
  • c = 1 will be displayed
Otherwise " c = 5 " will be displayed

Using NOT EQUAL TO operator in java program


import java.util.*;
class notequal
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b;

System.out.print ("\n\t Enter first number = ");
a=in.nextInt();

System.out.print ("\n\t Enter second number = ");
b=in.nextInt();

if (a!=b)
System.out.println ("\n\t First number is not equal to second ");

else
System.out.println ("\n\t Both numbers are equal ");

}
}

Note:
          After you have entered values of both variables, computer will evaluate the condition
  • If value of 'a' isn't equal to value of 'b' then
" First number is not equal to second " will be displayed otherwise
" Both numbers are equal " will be displayed.

Java program using EQUAL TO operator


import java.util.*;
class usingequal
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b;

System.out.print ("\n\t Enter first number = ");
a=in.nextInt();

System.out.print ("\n\t Enter second number = ");
b=in.nextInt();

if (a==b)
System.out.println ("\n\t Both numbers are equal ");

else
System.out.println ("\n\t First number is not equal to second ");

}
}

Note: 
         After entering values, computer will check the condition 
  • If 'a' is equal to 'b' then
" Both numbers are equal " will be displayed. Otherwise 
" First number is not equal to second " this message will be displayed on screen.
  

Java program using AND operator


import java.util.*;
class usingand
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a,b,c;

System.out.print ("\n\t Enter value of a = ");
a=in.nextInt();

System.out.print ("\n\t Enter value of b = ");
b=in.nextInt();

System.out.print ("\n\t Enter value of c = ");
c=in.nextInt();

if (a>b && a>c)
System.out.println ("\n\t a is greatest ");

else if (b>a && b>c)
System.out.println ("\n\t b is greatest ");

else
System.out.println ("\n\t c is greatest ");

}
}

Note:
          In this program, once you have entered values, first statement will be executed
  • If 'a' is greater than both 'b' and 'c'
if our first condition comes false than computer will execute second statement
  • If 'b' is greater than both 'a' and 'c'
if both conditions come false than third statement will be executed automatically i.e.
"c is greatest"

Java program using OR opeator


import java.util.*;
class usingor
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);

int a,b;

System.out.print ("\n\t Enter value of a = ");
a=in.nextInt();

System.out.print ("\n\t Enter value of b = ");
b=in.nextInt();

if (a<=10 || a<b)
System.out.println ("\n\t a is less then b or 10 ");
if (b>=25 || b==a)
System.out.println ("\n\t b is greater than 25 or equal to a ");

}
}

Note:
          In the above program, after you entered values of variables, first statement will be executed 
  • If 'a' is less than or equal to 10 
          OR
  • 'a' is less than 'b'
          and second statement will be executed in this case
  • If 'b' is greater than or equal to 25
          OR
  • 'b' is equal to 'a'