Decision Making
In programming very often we need to execute a statement or a group of statements based on some logical condition. Consider a flow chart :
The set of statements in the rectangle will be executed only when the logical condition return true. The syntax for if statement is like this
-----------------------------------
some statements before if statements
-----------------------------------
if (logical condition)
{
//Set of statements that execute only if logical condition return true
..................................................
---------------------------------
---------------------------------
}
------------------------------------
some statements after if statements
------------------------------------
A program which demonstrate the use of if statement
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
printf("Outer radius of washer : ");
scanf("%d",&a);
printf("Inner radius of washer : ");
scanf("%d",&b);
if (a>b)
{
printf("Washer design is possible.");
}
getch();
}
#include <conio.h>
void main()
{
int a,b;
clrscr();
printf("Outer radius of washer : ");
scanf("%d",&a);
printf("Inner radius of washer : ");
scanf("%d",&b);
if (a>b)
{
printf("Washer design is possible.");
}
getch();
}
The above has a bug that it provides no information if the washer design is not possible. Such bug can be eliminated by using if else statement. The syntax for if else statement is like this.
-----------------------------------
some statements before if statements
-----------------------------------
if (logical condition)
{
//Set of statements that execute only if logical condition return true
..................................................
---------------------------------
---------------------------------
}
else
{
//Set of statements that execute only if logical condition return false
..................................................
---------------------------------
---------------------------------
}
------------------------------------some statements after if statements
------------------------------------
The previous program can be rewritten as
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
printf("Outer radius of washer : ");
scanf("%d",&a);
printf("Inner radius of washer : ");
scanf("%d",&b);
if (a>b)
{
printf("Washer design is possible.");
}
else
#include <conio.h>
void main()
{
int a,b;
clrscr();
printf("Outer radius of washer : ");
scanf("%d",&a);
printf("Inner radius of washer : ");
scanf("%d",&b);
if (a>b)
{
printf("Washer design is possible.");
}
else
{
printf("Washer design is not possible.");
}
getch();
}
printf("Washer design is not possible.");
}
getch();
}
After executing one of the blocks either true or false, the control comes on the statements just after the else block. in above program getch() is the candidate statement on which control comes after if else statement.
Few words about logical conditions.
in above programs a>b is the logical condition which return true or false on basis of values of a and b.
to make such logical conditions other operators which may be used are these
Relational Operators | |
Mathematical Operator | C/C++ Operator |
< | < |
> | > |
≤ | <= |
≥ | >= |
= | = = |
≠ | != |
a>b can be written as a>b in c/c++
a≥b can be written as a>=b in c/c++
a<b can be written as a<b in c/c++
a≤b can be written as a<=b in c/c++
a=b can be written as a==b in c/c++
a≠b can be written as a!=b in c/c++There are few Logical Operators which are used to combine relational expressions like (a<b), (a<=b) etc.
Logical Operators performs logical operations on relations and returns true and false based on the candidate relational expressions. Explanation of these is as below.
Logical Operators | |||
Operator | Operation Performed | Example | Description |
&& | AND | (a<b)&&(a<c) | Returns true if a<b and a<c both returns true other wise false |
|| | OR | (a<b)||(a<c) | Returns true if any one or both (a<b), (a<c) returns true otherwise false. |
! | NOT | !(a<b) | Returns true if a<b returns false, Returns false if a<b return true. |
Let we want to find greater value among three integers assuming all a,b and c have different values, the required c program is
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Value of a : ");
scanf("%d",&a);
printf("Value of b : ");
scanf("%d",&b);
printf("Value of c : ");
scanf("%d",&c);
if ((a>b) && (a>c)) // Logical operator combining two conditions a>b and a>c
{
printf("%d is greater",a);
}
else
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Value of a : ");
scanf("%d",&a);
printf("Value of b : ");
scanf("%d",&b);
printf("Value of c : ");
scanf("%d",&c);
if ((a>b) && (a>c)) // Logical operator combining two conditions a>b and a>c
{
printf("%d is greater",a);
}
else
{
if (b>c)
{ printf("%d is greater ",b); }
else
{ printf("%d is greater ",c);
}
getch();
}
if (b>c)
{ printf("%d is greater ",b); }
else
{ printf("%d is greater ",c);
}
getch();
}