Header Ads Widget

Decision making or conditional construct in C

 Decision making are used to :-

  1. Make decision during the program execution based on the certain conditions. It helps in choosing one of the path from among several available path.
  2. These path are nothing but the sequence of programming statement and instructions.
  3. Break the sequential flow of the execution of program.
In c we have some decision making constructs:-
  1. if and else
  2. switch and case construct
  3. goto
If and else construct:-

it is one of the decision making construct in C-language . It choose from among two alternative paths corresponding to the result of the condition . i.e either true or false.
  • Simple if
  • if-else
  • if-else ladder (nested if -else)
Simple if:-

--------
-------
--------
if (conditional expression)
{
   //True
     first statement
     ----------------
     ----------------
     last statement
}
//false
next statement
-----------------
-----------------
explanation:-
If the condition is true then the statement written in the body of the if will executed and if the condition is false then statement next to the if will be executed.
"-----------------" means statements.

If -else:-

  -------------
---------------
if(condition)
{
    //True
     first statement
      --------------
      --------------
     last statement
}
// false
else
{
    first statement
     --------------
     ---------------
     last statement
}
next statement
-----------------
----------------

Explanation:-
                      if condition of the if is true then statement of the body of if will executed and it is false then statement of the else will be executed.

Nested if-else:-
                           Nested if-else is a structure in which one if-else structure (or simple if ) contain another if else (or simple if ) structure.
i.e.

----------------
----------------
if(condition)
{
    if(condition)
    {
         ------------
         ------------
     }
    else
    {
        --------------
        --------------
     }
}
--------------
-------------

P1) Program to find out largest numbers among three distinct numbers .

Code:-

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
printf("Enter three numbers \n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("%d is the largest number",a);
else
printf("%d is the largest",c);
}
else
{
if(b>c)
printf("%d is the largest number",b);
else
printf("%d is the largest",c);
}
return 0;
}

Output:-


Enter three numbers
2 4 5
5 is the largest

For more practice questions :-  Click here


Video lecture:- 

Simple if else:-


Nested If else:-



If else ladder:-









Recommended Post:

Hackerearth Problems:-

Hackerrank Problems:-
Data structure:-

Key points:-

 MCQs:-

Post a Comment

0 Comments