DECISION & CASE CONTROL STRUCTURE/C/LECTURE-2

DECISION CONTROL STRUCTURE



  • if
  • if else 
  • else if ladder
  • switch
  • goto
      Switch is also called case control structure

In real life we have to make certain decisions, if  weather is good then we will go out else we will
stay at home.. also like if my percentage is above 40% i am pass, else i am fail

>>> if statement 

the statement is executed only if the condition is true. it tells the compiler that if the condition given in the parenthesis( ) is true then execute the very next statements to the if condition.

syntax:

if(condition)
{
   statement 1;
   statement 2;
   statements...
   ...
   ..
}

>>> if else pair


this is the pair of statements
if part is executed when the condition in the parenthesis( ) is true &
else part is executed when the condition in the parenthesis( ) is false

syntax:

if(condition)
{
   statements to be executed when the condition is true...
   ....
   ...
   ..
}
else
{
   statements to be executed when the condition is false
}

>>> else if ladder

this syntax is used when we have more then one condition to be checked
for example :
if the weather is good we will go out
else if it is about to rain we will go out with a umbrella 
else if there is a thunderstorm we will stay at home

each part is executed when the condition given in the respective parenthesis( ) is true

syntax:
if(condition 1)
{
   statements...
}
else if(condition 2)
{
   statements...
}
else if(condition 3)
{
   statements...
}
else if(condition 4)
{
   statements...
}


>>> switch

this is also called case control structure it is a substitute of else if ladder and have more features than else if ladder ....

switch ( integer expression )
{
  case constant 1 :
    do this ;
  case constant 2 :
    do this ;
  case constant 3 :
    do this ;
  default :
     do this ;
}

The integer expression following the keyword switch is any C
expression that will yield an integer value. It could be an integer
constant like 1, 2 or 3, or an expression that evaluates to an
integer. The keyword case is followed by an integer or a character
constant. Each constant in each case must be different from all the
others. The “do this” lines in the above form of switch represent
any valid C statement.

What happens when we run a program containing a switch? First,
the integer expression following the keyword switch is evaluated.
The value it gives is then matched, one by one, against the
constant values that follow the case statements. When a match is
found, the program executes the statements following that case,
and all subsequent case and default statements as well. If no
match is found with any of the case statements, only the
statements following the default are executed. A few examples
will show how this control structure works.


Tips and Tricks for using switch statements

a)
The earlier program that used switch may give you the wrong
impression that you can use only cases arranged in ascending
order, 1, 2, 3 and default. You can in fact put the cases in any
order you please.

b)
You are also allowed to use char values.

(c) 
At times we may want to execute a common set of statements
for multiple cases. Can be used like this.
switch ( ch )
{
  case 'a' :
  case 'A' :
    printf ( "a as in ashar" ) ;
    break ;
  case 'b' :
  case 'B' :
    printf ( "b as in brain" ) ;
    break ;
  case 'c' :
  case 'C' :
    printf ( "c as in cookie" ) ;
    break ;
  default :
    printf ( "wish you knew what are alphabets" ) ;
}

d)
Even if there are multiple statements to be executed in each
case there is no need to enclose them within a pair of braces
(unlike if, and else).

e)
Every statement in a switch must belong to some case or the
other. If a statement doesn’t belong to any case the compiler
won’t report an error. However, the statement would never get
executed.

f)
If we have no default case, then the program simply falls
through the entire switch and continues with the next
instruction (if any,) that follows the closing brace of switch.

g)
Is switch a replacement for if? Yes and no. Yes, because it
offers a better way of writing programs as compared to if, and
no because in certain situations we are left with no choice but
to use if. The disadvantage of switch is that one cannot have a
case in a switch which looks like:
case i <= 20 :
All that we can have after the case is an int constant or a char
constant or an expression that evaluates to one of these
constants. Even a float is not allowed.

The advantage of switch over if is that it leads to a more
structured program and the level of indentation is manageable,
more so if there are multiple statements within each case of a
switch.

h)
We can check the value of any expression in a switch. Thus
the following switch statements are legal.
switch ( i + j * k )
switch ( 23 + 45 % 4 * k )
switch ( a < 4 && b > 7 )
Expressions can also be used in cases provided they are
constant expressions. Thus case 3 + 7 is correct, however,
case a + b is incorrect.

i)
The break statement when used in a switch takes the control
outside the switch. However, use of continue will not take
the control to the beginning of switch as one is likely to
believe.
j)
In principle, a switch may occur within another, but in
practice it is rarely done. Such statements would be called
nested switch statements.

k)
The switch statement is very useful while writing menu
driven programs. 

>>>goto statement

Avoid goto keyword! They make a C programmer’s life miserable

Comments

Popular Posts