Friday, 5 May 2017

Decision making



Decision making in C

Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. C language handles decision-making by supporting the following statements,
  • if statement
  • switch statement
  • goto statement

Decision making with if statement

The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are,
  1. Simple if statement
  2. If....else statement
  3. Nested if....else statement
  4. else if statement

Simple if statement

The general form of a simple if statement is,
if( expression )    
{     
statement inside;    
}   
statement outside;  
If the expression is true, then 'statement-inside' it will be executed, otherwise 'statement-inside' is skipped and only 'statement-outside' is executed.
c-if
Example :
#include <stdio.h>    
void main( )    
{     
int x,y;     
x=15;     
y=13;     
if (x > y )     
{      
printf("x is greater than y");     
}    
}  
Output
x is greater than y  

if...else statement

The general form of a simple if...else statement is,
if( expression )    
{     
statement block1;    
}    
else    
{     
statement block2;    
}  
If the 'expression' is true, the 'statement-block1' is executed, else 'statement-block1' is skipped and 'statement-block2' is executed.
c-if-else
Example :
#include <stdio.h>    
void main( )    
{     
int x,y;     
x=15;     
y=18;     
if (x > y )     
{      
printf("x is greater than y");     
}     
else     
{      
printf("y is greater than x");     
}    
}  
Output
y is greater than x  

Nested if....else statement

The general form of a nested if...else statement is,
if( expression )    
{      
if( expression1 )       
{         
statement block1;       
}      
else        
{         
statement block2;       
}    
}    
else    
{     
statement block3;    
}  
if 'expression' is false the 'statement-block3' will be executed, otherwise it continues to perform the test for 'expression 1' . If the 'expression 1' is true the 'statement-block1' is executed otherwise 'statement-block2' is executed.
Example :
#include <stdio.h>    
#include <conio.h>    
void main( )    
{     
int a,b,c;     
clrscr();     
printf("enter 3 number");     
scanf("%d%d%d",&a,&b,&c);     
if(a>b)     
{      
if( a > c)      
{        
printf("a is greatest");      
}      
else       
{        
printf("c is greatest");      
}     
}     
else     
{      
if( b> c)       
{         
printf("b is greatest");       
}      
else       
{         
printf("c is greatest");       
}      
}    
getch();    
}   

else-if ladder

The general form of else-if ladder is,
if(expression1)    
{     
statement block1;    
}    
else if(expression2)     
{     
statement block2;    
}    
else if(expression3 )     
{     
statement block3;    
}    
else      
default statement;  
The expression is tested from the top(of the ladder) downwards. As soon as the true condition is found, the statement associated with it is executed.
Example :
#include <stdio.h>    
#include <conio.h>    
void main( )    
{     
int a;     
printf("enter a number");     
scanf("%d",&a);     
if( a%5==0 && a%8==0)     
{      
printf("divisible by both 5 and 8");     
}       
else if( a%8==0 )     
{      
printf("divisible by 8");     
}     
else if(a%5==0)     
{      
printf("divisible by 5");     
}     
else      
{      
printf("divisible by none");     
}    
getch();    
}  

Points to Remember

  1. In if statement, a single statement can be included without enclosing it into curly braces { }
    int a = 5;    if(a > 4)      printf("success");  
    No curly braces are required in the above case, but if we have more than one statement inside if condition, then we must enclose them inside curly braces.
  2. == must be used for comparison in the expression of if condition, if you use = the expression will always return true, because it performs assignment not comparison.
  3. Other than 0(zero), all other values are considered as true.
    if(27)   printf("hello");  
    In above example, hello will be printed.

Switch statement is used to solve multiple option type problems for menu like program, where one value is associated with each option. The expression in switch case evaluates to return an integral value, which is then compared to the values in different cases, where it matches that block of code is executed, if there is no match, then default block is executed. The general form of switch statement is,
switch(expression)    
{     
case value-1:  
block-1;      	 
 break;     
case value-2:      	 
 block-2;      	 
 break;     
case value-3:      	 
 block-3;  
break;     
case value-4:     
block-4;
break;     
default:  
default-block;      	 
 break;    
}         
flow of switch statement in c

Points to Remember

  1. We don't use those expressions to evaluate switch case, which may return floating point values or strings.

  2. It isn't necessary to use break after each block, but if you do not use it, all the consecutive block of codes will get executed after the matching block.
    int i = 1;    switch(i)    {     case 1:           printf("A");          // No break     case 2:       printf("B");          // No break     case 3:       printf("C");       break;    }  
    Output : A B C
    The output was supposed to be only A because only the first case matches, but as there is no break statement after the block, the next blocks are executed, until the cursor encounters a break.

  3. default case can be placed anywhere in the switch case. Even if we don't include the default case switch statement works.
Valid Switch Invalid Switch Valid Case Invalid Case
switch(x) switch(f) case 3; case 2.5;
switch(x>y) switch(x+2.5) case 'a'; case x;
switch(a+b-2) case 1+2; case x+2;
switch(func(x,y)) case 'x'>'y'; case 1,2,3;


Example of Switch Statement

#include<stdio.h>    
#include<conio.h>    
void main( )     
{      
int a,b,c,choice;      
clrscr( );       
while(choice!=3)      
{       
printf("\n 1. Press 1 for addition");       
printf("\n 2. Press 2 for subtraction");       
printf("\n Enter your choice");       
scanf("%d",&choice);       
switch(choice)       
{        
case 1:          
printf("Enter 2 numbers");          
scanf("%d%d",&a,&b);          
c=a+b;          
printf("%d",c);          
break;        
case 2:          
printf("Enter 2 numbers");          
scanf("%d%d",&a,&b);          
c=a-b;          
printf("%d",c);          
break;        
default:          
printf("you have passed a wrong key");          
printf("\n press any key to continue");        
}      
}      
getch();     
}     

C goto statement

The goto statement is known as jump statement in C language. It is used to unconditionally jump to other label. It transfers control to other parts of the program.
It is rarely used today because it makes program less readable and complex.
Syntax:
  1. goto label;  

goto example

Let's see a simple example to use goto statement in C language.
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void main() {  
  4.   int age;  
  5.   clrscr();  
  6.    ineligible:  
  7.    printf("You are not eligible to vote!\n");  
  8.   
  9.    printf("Enter you age:\n");  
  10.    scanf("%d", &age);  
  11.    if(age<18)  
  12.         goto ineligible;  
  13.    else  
  14.         printf("You are eligible to vote!\n");  
  15.   
  16.    getch();  
  17. }  
Output:
You are not eligible to vote!  Enter you age:  11  You are not eligible to vote!  Enter you age:  44  You are eligible to vote!  

No comments:

Post a Comment

Please write your view and suggestion....