Decision Making
C conditional statements allow you to make a decision, based upon the result of a condition. These statements are called Decision Making Statements or Conditional Statements.
Flow Chart of Control Statement:
Conditional Statements in C
- if statement
- if-else statement
- Nested if-else statement
- else if-statement
- goto statement
- switch statement
- Conditional Operator
If Statement :
If statements in C is used to control the program flow based on some condition, it's used to execute some statement code block if the expression is evaluated to true. Otherwise, it will get skipped.
Following is the syntax of if statements:
if ( Condition )
{
statement 1;
statement 2;
...
}
Flow Chart of if Statement:
Example of if Statement:
#include<stdio.h>
int main()
{
int a = 15, b = 20;
if ( b>a ) {
printf("b is greater");
}
return 0;
}
Output:
b is greater
if-else Statement:
If else statements in C is also used to control the program flow based on some condition, only the difference is: it's used to execute some statement code block if the expression is evaluated to true, otherwise executes else statement code block.
Syntax of if-else Statement:
if( condition )
{
Statement;
}
else
{
Statement;
}
Flow Chart of if-else Statement:
Example of if-else Statement:
#include<stdio.h>
int main()
{
int a, b;
printf("Please enter the value for a: \n");
scanf("%d", &a);
printf("Please the value for b: \n");
scanf("%d", &b);
if (a > b) {
printf("a is greater \n ");
}
else {
printf("b is greater \n");
}
}
Output of Program:-
Please enter the value for a : 5
Please enter the value for b : 10
b is greater
************Stay Tuned for upcoming Tutorial***********
0 Response to "Decision Making"
Post a Comment