What is Decision Making Statement?
In the C programming language, the program execution flow is line by line from top to bottom. That means the c program is executed line by line from the main method. But this type of execution flow may not be suitable for all the program solutions. Sometimes, we make some decisions or we may skip the execution of one or more lines of code. Consider a situation, where we write a program to check whether a student has passed or failed in a particular subject. Here, we need to check whether the marks are greater than the pass marks or not. If marks are greater, then we decide that the student has passed otherwise failed. To solve such kind of problems in c we use the statements called decision making statements.
Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
In the c programming language, there are two decision-making statements they are as follows.
- if statement
- switch statement
In c, if statement is used to make decisions based on a condition. The if statement verifies the given condition and decides whether a block of statements are executed or not based on the condition result. In c, if statement is classified into four types as follows...
- Simple if statement
- if-else statement
- Nested if statement
- if-else-if statement (if-else ladder)
#include<conio.h>
void main()
{
int number=0;
printf("Enter a number:");
scanf("\n %d",&number);
if(number%2==0)
{
printf("\n %d is even number",number);
}
getch();
}
#include<conio.h>
void main()
{
int number=0;
printf("\n Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("\n %d is even number",number);
}
else
{
printf("\n %d is odd number",number);
}
getch();
}
#include<conio.h>
void main()
{
int number=0;
printf("\n Enter a number:");
scanf("%d",&number);
if(number<100)
{
printf("\n Given number is below 100\n") ;
if(number%2==0)
{
printf("\n %d is even number",number);
}
else
{
printf("\n %d is odd number",number);
}
}
else
{
printf("Given number is not below 100");
}
getch();
}
#include<conio.h>
void main()
{
int marks;
printf("\n Enter your marks:");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("\n Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("\n You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("\n You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("\n You scored grade C ...");
}
else
{
printf("\n Sorry you are fail ...");
}
getch();
}
- if(10) - is TRUE
- if(x) - is FALSE if x value is zero otherwise TRUE
- if(a = 99) - is TRUE because a value is non-zero
- if(a+b) - is FALSE if a+b value is zero otherwise TRUE
- if(0) - is FALSE
- if(10, 5, 0) - is FALSE because it considers last value
- if(a=20, b=5, c=0) - is FALSE because last value is zero
No comments:
Post a Comment