Thursday, April 2, 2009

java-switch-break-if-else

Control Flow(Flow-control ):
Flow control describes the order of execution of code at runtime.
i.e it describes the order in which the statement will execute.
Selection statement
(1)if-else
(2)switch
Iteration statement
(1)for (2)while
(3)do-while
Transfer statement
(1)break
(2)continue
(3)return
(4)try-catch-finally
(5)assertion
(1)if –else :
if: {} and else part both are optional. The valid argument for if statement is always Boolean otherwise compile time error.
If Examples:
if(boolean)

Example: int i=10;
if (i) //This is valid in c++, but invalid java
{
System.out.println(“hi”);
}
else
{
System.out.println(“hello”);
}
Ex : int i=10;
if (i==10)
{
System.out.println(“hi”); //hi
}
else
{
System.out.println(“hello”);
}
Ex : int i=10;
if (i=20)
{
System.out.println(“hi”);
else
{
System.out.println(“hello”);
} // Compile time error
Ex: boolean b =true;
if (b=false)
{
System.out.println(“hi”);
else
{
System.out.println(“hello”);
} // hello
Ex : boolean b=true;
if(b)
int i=10; //invalid
System.out.println(i); //Compile time error
Under if with out curly braces only one statement is allowed ,but that statement never be declarative statement violation leads to compile time error.
Ex:- boolean b= true;
if(b)
int i=10; // invalid
Ex:- boolean b=true;
if(b)
{
int i=10; // valid
}

Switch statement:In switch statement {} are mandatory, violation leads to compile time error.
Both case and default are optional.
Eg:- int i=10;
switch(x)
{
……………
……………no case and default statements.
}

The valid arguments of switch statement are byte, int, short, char until 1.4 version, from 1.5 version onwards the corresponding wrapper class objects Byte, Short, Integer, Character are also allowed.switch can take an enum object are also as argument.If we want keep any statement inside switch it must under some case or default, otherwise COMPILE TIME ERROR.
Eg:- int x=4;
switch(x)
{
System.out.println (“hello”); //hello
}
case-labels : All the case –labels must be compile time Constance, otherwise COMPILE TIME ERROR saying Constance expression required.
Ex: int x=40;
int y=10
switch(x)
{
case y: // CE saying Constance expression required.
}
Ex:final int x=40;
int y=10
switch(x)
{
case y: // valid
}
All the case labels must be in the range of switch arguments otherwise compile time error.

Ex: byte b=10;
switch(b)
{
case 10:
case20:
case100:
case10000://CE saying PLP found: int but required: byte
}
The case label should not be duplicated ,violation leads to COMPILE TIME ERROR
Ex:- int x=10;
switch(x)
{
case 98:
case98:
case a:
}//CE
Hence the case labels
(1)must be compile time Constance

(2) must be in the range of switch arguments
(3)must not be duplicated
default case:- The default case we can keep any where, but it is conversion to keep default case is always at last place.
Ex:
switch(x)
case 0:
System.out.println(“0”);
case 1:
System.out.println(“1”);
case 2:
System.out.println(“2”);
default:
System.out.println(“default”);
}
Once any case are default are matched , all the statements will execute from top to bottom until break or end of switch statements. This is called fall-through inside switch.
Ex:- int x=1;
switch(x)
{
default:
System.out.println(“default”);
case 0:
System.out.println(“0”);
case 1:
System.out.println(“1”);
break;
case 2:
System.out.println(“2”);
}

0 comments:

Post a Comment