Sunday, 9 August 2009

Control Statements 1

One of the benefit of programs is that they are flexible.That means,
there will be different outcomes in different situations.
This is achieved with the help of control statements which
alter the flow of program depending on the result of a
expression or state of variable

Lets go through them one by one

Selection statements

if block

General syntax is

if (expression)

{
// statements
}
expression must evaluate to a boolean value (true or false)
It's simple: if outcome is true, statements inside block are
executed otherwise they are skipped.

if else

General syntax is

if (expression)
{
// statements 1
}
else
{
// statements 2
}

Here, if expression evaluates to true if block
is entered and statements 1 are executed.
Otherwise else block is entered and
statements 2 get executed.

You can nest an if block inside another if block

if (cond1) // outer if

{
// processing code

if (cond2) // inner if

{
// processing code
}

}

You can extend the concept of if else to if else if else if .... ladder

if (cond1)
{
}
else if (cond2)
{
}
else if (cond3)
{
}
else
{
}

Expression can use relational operators (> , < ), equality operators (== , !=),
boolean operators (or,and,!) or have a call to function.
Only thing is that output should be boolean

Switch block

When the number of conditions is large, Switch block comes in handy

General syntax is

switch (expression)

{

case 1 :
case 2:
.
.
.
case n :
default :
}
Here expression should evaluate to byte , short , int or char type
Depending on the value of expression, matching case would be executed
If there is no exact match, code in the default block (optional) would be executed

Lets work out a simple example

int weekDay = 3 ;

switch (weekDay )
{
case 1 : { System.out.prinln ("It's Monday") ; break ; }
case 2 : { System.out.prinln ("It's Tuesday") ; break ; }
case 3 : { System.out.prinln ("It's Wednesday") ; break ; }
case 4 : { System.out.prinln ("It's Thursday") ; break ; }
case 5 : { System.out.prinln ("It's Friday") ; break ; }
case 6 : { System.out.prinln ("It's Saturday") ; break ; }
case 7 : { System.out.prinln ("It's Sunday") ; break ; }
default : {System.out.prinln ("Check input") ;} ;
}
When switch block is entered, it checks the value of weekDay which is 3.
It searches for a case with this value and executes it.
If weekDay had value say 10, there would'nt be any matching case so default would
have got executed.
It is always good to enclose the statements under each case and default in curly { }
break is used to exit switch block after a case gets executed.If you omit it, all the following
cases would get executed
Like if block you can have nested switch also

int outer = 1;
int inner = 2 ;
switch (outer)

{
case 1 :
{
switch (inner)
{
case 1 : { // do something }
case 2 : { // do something }

} // inner switch ends

} // case 1 of outer switch ends

case 2

} // outer switch ends

In the outer switch block, value of outer variable is matched with the
cases. Inside case 1 , inner switch block is entered where value of
inner is matched with the cases and case 2 gets executed.

Difference between if - else - if and switch

1 Switch is usually more performance oriented as compared to if else if ladder
2 Switch looks for exact match, whereas in if block we can have any kind of expression
which evaluates to a boolean