Decision Structure of C PROGRAM

Hello Guys !!!! Welcome Back. Now we will go for the next topic of the C program which is called Decision structure.
                    C program has many complex problems. How can we use our method to solve complex problems? Decision structure is a one of the method which can we easily use in program for solve complex problems. In this chapter, we will study Five methods. Here are :-

  • Simple if statement
  • if..else.. statement
  • multiple if...else statement
  • Nested if ..else statement
  • Switch statement
Now we start from

simple if statement:-

   syntax is:-
                   if(condition)
                              statement ;

means when we use this formula in the program, and if condition will true then statement will print otherwise it will not print.

If.. else statement:-

It is used when there are two statements available.
  syntax is:-
           if(condition)
                    statement 1 ;
           else
                    statement 2 ;

means if condition will true then statement 1 will print otherwise statement 2 will print.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
printf(“enter two values \n”);
scanf(“%d%d',&n1,&n2);
                   if(n1>n2)
                           printf(“n1 is larger\n”);
                   else
                           printf(“n2 is larger\n”);
getch();
}

In this program, if n1 is maximum then n2 then n1 will larger otherwise n2 will larger.

Nested if... else statement:-

It is used when we need to check more then one condition. this statement is used when if statement is used inside another if statement.

Program :-



when we enter three numbers then minimum number will print on output.

Multiple if...else statement :-

The two way decision provide by if...else statement is not sufficient when we are having many choice. So that time we used multiple if..else statement.
syntax is :-
            if(condition1)
                      statement 1;
            else if(condition2)
                      statement 2;
            else if(condition3)
                       statement 3;
            .
            .
            .
            .
           else if(condition N)
                      statement N;
           else
                   default statement;

so, it is clear that if condition1 will true then statement1 will print otherwise it will check next condition and work.

Program :-


Every statement has a value. so if we enter number between (1-7) then statement of the number will print otherwise it will print wrong input.

Switch statement:-

It is used when many alternative values are available. Multiple if...else statement is also used in alternative values but using switch statement, program becomes easy and small.

Syntax is:-
           switch (variable name)
          {
            case 1 : statement 1;
                               break;
            case 2 : statement 2;
                               break;
             .
             .
            case N : statement N;
                               break;
            default : default statement;

           }

The value of variable written after the switch statement must be either character or integer value.
In multiple if...else statement there are more then one condition but in switch statement there is only one condition.

Program:-


Every statement has a value. so if we enter number between (1-7) then statement of the number will print otherwise it will print wrong input.


in case in switch statement, where different values have same statement,then there is not use upper type of syntax . Here is :-

for example, If a is an integer variable,whose valid value are from 1 and 2. and if code for value 1 and 2 are same, then it can be written in :
syntax is :-
    switch(a)
            {
             case 1 :
             case 2 :
                           statement;
                                break;
             case 4 :
             case 5 :
                           statement;
                                break;
             default;
                           statement;
            }

In switch statement, we also used break statement. If it is not written after each case statement,then control passes to the next statement. so remaining statement of next case will also execute even if the case value do not match and the program will not function properly.



Thanks for watching my post . I will be back soon with my new post related to loops until then bye bye..!!! …






Comments