Decision making are used to :-
- Make decision during the program execution based on the certain conditions. It helps in choosing one of the path from among several available path.
- These path are nothing but the sequence of programming statement and instructions.
- Break the sequential flow of the execution of program.
In c we have some decision making constructs:-
- if and else
- switch and case construct
- 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:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
0 Comments