Pages

Saturday, 7 March 2020

Decision Making Statements or Conditional Statements.

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,
1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. Ladder else if statement

1. Simple if statement

if(expression)
 {
  statement inside;  // if expression is true
}
   statement outside;     // when expression is false 

Example:
#include <stdio.h>
void main( )
{
    int a, b;
    a = 5;
    b = 3;
    if (a> b)
     {
         printf("a is greater than b");
 }
}

2. if...else statement

The general form of a simple if...else statement is,
if(expression)
{
   statement-1;
}
else
{
   statement-2;
}
If the expression is true, the statement-1 is executed, else statement-1 is skipped and statement-2 is executed.
Example:
#include <stdio.h> 
void main( )
{
    int a, b;
    a = 5;
    b = 12;
    if (a > b )
    {
        printf("a is greater than b");
    }
else
    {
        printf("b is greater than a");
}
}

3. Nested if....else statement

The general form of a nested if...else statement is,
if( expression )
  {
     if( expression1 )
        {
          statement-1;
        }
   else
       {
         statement-2;
   }
}
else
  {
    statement-3;
   }

if expression is false then statement-3 will be executed, otherwise the execution continues and enters inside the first if to perform the check for the next if block, where if expression1 is true the statement-1 is executed otherwise statement-2 is executed.
Example:
#include <stdio.h>
void main( )
{
    int a, b, c;
    printf("Enter three numbers...");
    scanf("%d%d%d",&a, &b, &c);
    if(a > b)
    {
        if(a > c)
        {
            printf("a is the greatest");
        }
    else
        {
            printf("c is the greatest");
        }
    }
    else
    {
        if(b > c)
        {
            printf("b is the greatest");
        }
        else
        {
            printf("c is the greatest");
        }
}
}

4. else if ladder

The general form of else-if ladder is,
if(expression1)
{
statement-1;
}
else if(expression2)
{
statement-2;
}
else if(expression3 )
 {
statement-3;
}
else
 default statement;

The expression is tested from the top(of the ladder) downwards. As soon as a true condition is found, the statement associated with it is executed.
Example :
#include <stdio.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");
}
}

Switch statement 

switch(expression)
{
    case value-1: statement-1;
                          break;
    case value-2: statement_2;
                          break;
            .
.
.
    default: default_statement;
                 break;
}

Rules for using switch statement

1. The expression (after switch keyword) must yield an integer value i.e the expression should be an integer or a variable or an expression that evaluates to an integer.
2. The case label values must be unique.
3. The case label must end with a colon(:)
4. The next line, after the case statement, can be any valid C statement.
Example:
#include<stdio.h>
void main()
{
 int n;
printf("Enter a number between 1 and 7: ");
scanf("%d",&n);
switch(n)
{
case 1: printf(“Monday”);
   break;
case 2: printf(“Tuesday”);
   break;
case 3: printf(“Wednesday”);
   break;
case 4: printf(“Thursday”);
   break;
case 5: printf(“Friday”); 
   break;
case 6: printf(“Saturday”);
   break;
case 7: printf(“Sunday”);
   break;
default : printf(Invalid Choice. Enter correct choice.);
              break;
}
}

No comments:

Post a Comment

Program for Largest Number among three numbers

Program for Largest Number among three numbers /* c program to find largest number among 3 numbers*/ #include <stdio.h>   in...