Ads 468x60px

Labels

Thursday, 13 September 2012

Switch Case control structure


SWITCH stmt is used for handling n number of decisions at a time.
 Hence it is also known as multi way decision making stmt.
SWITCH STATEMENT can't be applied to float and double.
 Break stmt transfers the control out of the switch stmt.
Syntax

switch(choice)
  {
  case 1:block of stmt:-1;
  break;
  case 2:block of stmt:-2;
  break;
  …………………….
  ……………………..
  ………………………
  case n:    block of stmt:-n;
  break;
  default: invalid stmt;
  break;
  }


sample program 

Logical operators workshop


Logical && and logical || 
logical && : If all are true then only true otherwise false.
logical || : if any one is true then it is true otherwise false.
Program on logical operators 
 A company insures its drivers in the following cases:
− If the driver is married.
− If the driver is unmarried, male & above 30 years of age.
− If the driver is unmarried, female & above 25 years of age.
In all other cases the driver is not insured. If the marital status, sex and age of the driver are

Else-if Control Structure


Else-if:
      A Multipath decision is a chain of ifs in which the statement associated with each else is an if.
Syntax:
  if(condition-1)
  stmt-1;
     else if(condition-2)
         stmt-2;
  else if(condition-3)
     stmt-3;
      ………………….
      else if(condition-n)
        stmt-n;
  else
  defalut stmt;
stmt-x;

Sample Program :

If else control structure


C  uses the keyword if-else to implement the decision control instruction.
Syntax
if(test condition-1)
{
             if(test condition-2)
  {
                block of stmt-1;
  }
  else
  {
                  block of stmt-2;
               }
}
  else
  {
     block of stmt-3;
  }
Stmt-x;

Note: braces are mandatory when, corresponding to if or else statement, multiple steps are to be executed.
Sample program 

If Control Structure


if   
C  uses the keyword if to implement the decision control instruction.
if statement can’t terminate with semicolon, but by mistake if we terminate ,compiler won’t give any error.
Syntax
if (test condition)
{
------------------------------------
------------------------------------
-------------------------------------               block of statements
--------------------------------------
}
Stmt-x;

Sample if program

Schaum's Outline of Programming with C



Head First Programming

Looking for a reliable way to learn how to program on your own, without being overwhelmed by confusing concepts? Head First Programming introduces the core concepts of writing computer programs - variables, decisions, loops, functions, and objects - which apply regardless of the programming language. This book offers concrete examples and exercises in the dynamic and versatile Python language to demonstrate and reinforce these concepts.

Learn the basic tools to start writing the programs that interest you, and get a better understanding of what software can (and cannot) do. When you're finished, you'll have the necessary foundation to learn any programming language or tackle any software project you choose.