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.

Monday, 27 June 2011

Java program using while loop


import java.util.*;
class loop
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a=10;
while (a<=20)
{
System.out.print ("\n\t a = "+a);
++a;
}

}
}

Remember, while a is less than or equal to 20 body of the loop will be executed. Once a equals to 21, loop will be broken and control goes to the next statement after that loop.

While loop in java


While loop:
                    The while loop is java's most fundamental looping statement. It is very easy and powerful loop of java.It repeats a statement or block while its Boolean expression is true. This Boolean expression controls the loop.

Syntax:
                 while (condition)
                  {
                    //body of the loop
                   }

The body of the loop will be executed till the conditional expression is true. When conditional expression comes false the control passes to the next statement right after the body of the loop. Always remember, in while loop first of all conditional expression is evaluated if it is true then body of the loop will be executed. If it becomes false at the beginning then body of the loop will not be executed, even once.

Curly braces are not important if you have only one statement to repeat but using curly braces is a good habit. Another point you should know that the body of while loop can be empty.

Sunday, 26 June 2011

Nested switch statement in java


Nested switch:
                         We can use a switch as part of the statement sequence of an outer switch. This is called nested switch. We know that each switch defines its own block of code but case constants of outer switch have no conflicts with the inner switch's case constants.

General syntax:
                           switch (value)
                           {
                            case 1:
                               switch (value)
                               {
                                case 1:
                                statement(s)
                                break;
                                .
                                .
                                case n:
                                statement (n)
                                break;
                                default:
                                statement
                                }
                          case 2:
                          statement(s)
                          break;
                          .
                          .
                          case n:
                          statement (n)
                          break;
                          default:
                          statement
                         }    
Here, value can be a variable or any constant value. Remember case 1 of outer switch have no conflicts with case 1 of inner switch. If the value matches with case 1 (outer switch's) then inner switch will be executed otherwise it will be bypassed.


Java program using nested switch


import java.util.*;
class nswitch
{
public static void main (String arg[])
{
Scanner in = new Scanner (System.in);
int a,b;
a=3;
b=2;
switch (a)
{
case 1:
switch (b)
{
case 1:
System.out.println ("\n\t Inner One ");
break;
case 2:
System.out.println ("\n\t Inner Two ");
default:
System.out.println ("\n\t Inner :: Invalid ");
}
case 2:
System.out.println ("\n\t Outer Two ");
break;
case 3:
System.out.println ("\n\t Outer Three ");
break;
default:
System.out.println ("\n\t Outer :: Invalid ");
}

}
}