C Programming Decision Making
C Programming Decision Making
Following is the general form of a typical decision making structure found in most
of the programming languages:
C programming language provides following types of decision making statements. Click the following
links to check their detail.
Statement Description
if statement An if statement consists of a boolean expression
followed by one or more statements.
C - if statement
Syntax:
The syntax of an if statement in C programming language is:
if(condition)
{
//execute your code
}
If the boolean expression evaluates to true, then the block of code inside
the if statement will be executed. If boolean expression evaluates tofalse, then
the first set of code after the end of the if statement(after the closing curly brace)
will be executed.
Flow Diagram:
#include <stdio.h>
void main ()
{
int a = 10;
if( a < 20 )
{
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
}
When the above code is compiled and executed, it produces the following result:
a is less than 20;
value of a is : 10
C - if...else statement
Meaning of If Statement :
It Checks whether the given Expression is Boolean or not!!
If Expression is True Then it executes the statement
otherwise jumps to next_instruction
Syntax
if(expression)
{
Statement(s)-1;
}
Else
{
Statement(s)-2;
}
Next_statements;
If the boolean expression evaluates to true, then the if block of code will be
executed, otherwise else block of code will be executed. C programming language
assumes any non-zero and non-null values as true, and if it is either zero or null,
then it is assumed as false value.
OR
C program to check whether a given number is even
or odd
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter any number:”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“The number is even“);
}
else
{
printf(“The number is odd”);
}
getch();
}
Example 2:
#include <stdio.h>
Void main ()
{
/* local variable definition */
int a = 100;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Solution: By using less than "<" and greater than, ">" sign we can check, Which
---------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
Void main()
int a, b;
clrscr();
printf(" a is greater");
}
else
{
printf("b is greater");
}
getch();
}
or consonant.
vowel or not. If not, then it will print the message "number is consonant".
---------------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
Void main()
char ch;
clrscr();
||ch=='U') )
{
}
else
{
printf("Enter Character Is Consonant");
}
getch();
}
division concept, Here if any number is divisible by 7 then it will give the
value.
---------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
Void main()
int a;
clrscr();
a = a%7; // Using Module Divison Checking and storing the result into
same variable a.
if (a == 0)
{
}
else
{
}
getch();
}
C - Nested if statements
It is always legal in C programming to nest if-else statements, which means
you can use one if or else if statement inside another if or else if statement(s).
Syntax:
The syntax for a nested if statement is as follows:
if(condition_1)
{
if(condition_2)
{
block statement_1;
}
else
{
block statement_2;
}
}
else
{
block statement_3;
}
block statement_4;
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z;
clrscr();
printf("\n Enter the value of x,y,z:");
scanf("%d%d%d",&x,&y,&z);
if(x>y)
{
if(x>z)
{
printf("\n X = %d is Greatest",x);
}
else
{
printf("\n Z = %d is Greatest",z);
}
}
else
{
if(y>z)
{
printf("\n Y = %d is Greatest",y);
}
else
{
printf("\n Z = %d is Greatest",z);
}
getch();
}
Z = 45 is Greatest
Example 2:
Let consider a problem to display the grade obtained by a student based on the
marks. The criteria to find the grade based on marks as follows:
Marks Grade
0 to 34 F
35 to 44 E
45 to 59 D
60 to 69 C
70 to 79 B
80 to 89 A
90 to 100 A+
Void main()
{
int marks;
clrscr();
if(marks<=34)
printf(“Grade F”);
else if(marks<=45)
printf(“Grade E”);
else if(marks<=59)
printf(“Grade D”);
else if(marks<=69)
printf(“Grade C”);
else if(marks<=79)
printf(“Grade B”);
else if(marks<=89)
printf(“Grade A”);
else
printf(“Grade A+”);
getch();
}
Example 3:
#include<stdio.h>
#include<string.h>
void main()
int n;
scanf("%d",&n);
if(n==1)
else if(n==2)
else if(n==3)
}
else if(n==4)
else
getch ();
Example 4:
Void main()
{
int units, custnum;
float charges;
Advantages:
In computer programming, situations involving series of decisions one after the
other, each decision or condition may involve expression of various data types
such as float, integer, char and double. In these situations the if-else-if ladder is
used.
Disadvantages:
The nested ifs are hard to understand and modify.
As the depth of nesting increases, the readability of the program decreases.
In situations involving series of decisions one after the other, each decision
or condition may involve expressions which result in integer value. In these
situations the usage of if-else-if ladder is not recommended. Instead of this
statement, we go for switch statement.
Switch case statement in C:
Switch case statements are used to execute only specific case statements
based on the switch expression.
Below is the syntax for switch case statement.
switch (expression)
{
case label1: statements;
break;
case label2: statements;
break;
default: statements;
break;
}
You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
Code for Program that reads marks of a students and computes and displays
grade in C Programming
#include <stdio.h>
#include<conio.h>
void main ()
{
/* local variable definition */
char grade = 'B';
switch(grade)
{
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
}
Example 3:
Void main()
{
int p,q,result;
char op;
clrscr();
printf(“Enter an expression here:\n”);
scanf(“%d %c %d”,&p,&op,&q);
switch(op)
{
case ‘+’:
result = p + q;
break;
case ‘-’:
result = p - q;
break;
case ‘*’:
result = p * q;
break;
case ‘/’:
result = p / q;
break;
default:
printf(“Invalid expression.\n”);
}
printf(“The result of the expression %d %c %d = %d”,p,op,q,result);
getch();
}
Break statement in C:
Break statement is used to terminate the while loops, switch case loops and
for loops from the subsequent execution.
Syntax: break;
for(i=0;i<10;i++)
{
if(i==5)
{
printf(“\nComing out of for loop when i = 5″);
break;
}
printf(“%d “,i);
}
}
Output:
0 1 2 3 4
Coming out of for loop when i = 5
Continue statement in C:
Continue statement is used to continue the next iteration of for loop, while
loop and do-while loops. So, the remaining statements are skipped within
the loop for that particular iteration.
Syntax : continue;
Output:
0 1 2 3 4
Goto statement in C:
goto statements is used to transfer the normal flow of a program to the
specified label in the program.
Below is the syntax for goto statement in C.
{
…….
go to label;
…….
…….
LABEL:
statements;
}
Output:
0 1 2 3 4
We are using goto statement when i = 5
Now, we are inside label name “hai”