You are on page 1of 32

CONDITIONAL STATEMENTS

There are generally two type of conditional statements : If


condition and Switch condition. We will discuss them one by
one.

IF CONDITION :

If condition is used when we want an either condition to run


depending upon the situation. There are three different type of
if conditions :

1. Simple if condition
2. if..else condition
3. Nested if condition

Simple if condition

In this condition only one process can take place with the
positive result of the logic given. The syntax of the condition is
given as :

if (<logic>)
statement;

If we want to use more than one statement to execute with the


positive result then a block of the condition must be created. A
block is always created to give more than one statement for
any condition/loop or anything like that.

if (<logic>)
{
Statement 1;
Statement 2;
.
.
.
Statement n;
}

It is not necessary to give block for multi statements only. We


can also give block for a single statement also but it is not a
good habit. It just increases the size of the program.

Let us have an example to check for a number whether it is


greater than 10 or not.
OUTPUT
// Program
: to check whether a given number is greater than
10 or not.
Please enter a number to check : 65
#include<stdio.h>
The given number 65 is greater than 10.
#include<conio.h>

void main ()
{
int a;
clrscr();

printf(“\nPlease enter a number to check : “);


scanf(“%d”,&a);

if (a>10)
printf(“The given number %d is greater than 10.”);

getch();
}
NOTE : If the number is found less than 10 then nothing will be
displayed. We can also use any variable at the place of the
constant (10). Let us check the greater number in two given
numbers. The program will also tell whether the numbers are
equal or not.
OUTPUT
// Program
: to check which of the given number is greater or
equal. enter two numbers to check : 65 78
Please
#include<stdio.h>
The given number 78 is greater than 65.
#include<conio.h>

void main ()
{
int a,b;
clrscr();

printf(“\nPlease enter two numbers to check : “);


scanf(“%d %d”, &a, &b);

if (a>b)
printf(“The given number %d is greater than %d.”,
a, b);

if (b>a)
printf(“The given number %d is greater than %d.”,
b, a);

if (a==b)
printf(“Both of the numbers are equal. %d”, a);

getch();
}
Here, I have used == to check for equal condition. It is not the
assignment operator. For checking the conditions, the ==
operator must be used always. Now, let us check whether a
given character is A or not.
OUTPUT
// Program
: to check whether a given character is ‘A’ or not.
#include<stdio.h>
Please enter a character to check : A
#include<conio.h>
The given character is A.

void main ()
{
clar ch;
clrscr();

printf(“\nPlease enter a character to check : “);


scanf(“%c”,&ch);

if (a==’A’)
printf(“The given character is A.”);

getch();
}
if...else condition

if...else condition is the advanced concept of simple if


condition. In this condition, the else statement is extra. Till the
point we have seen that statements are compiled if the logic is
true but nothing happens when the logic is false. This feature is
the advantage of if...else condition. In this condition, else block
contains the statements that executes when the given logic
given is in false state. The syntax of the condition is given as :

if (<logic>)
{
Statement 1;
Statement 2;
.
.
Statement n;
}
else
{
Statement 1;
Statement 2;
.
.
Statement n;
}

Similar to the one statement conditions, the block(s) can be


removed.
if (<logic>)
Statement;
else
Statement;
Let us take the previous example of checking the greater
number from the two. Here, I am assuming that the given
numbers are not equal.

OUTPUT
// Program
: to check which of the given number is greater out
of two. enter two numbers to check : 65 78
Please
#include<stdio.h>
The given number 78 is greater than 65.
#include<conio.h>

void main ()
{
int a,b;
clrscr();

printf(“\nPlease enter two numbers to check : “);


scanf(“%d %d”, &a, &b);

if (a>b)
printf(“The given number %d is greater than %d.”,
a, b);
else
printf(“The given number %d is greater than %d.”,
b, a);

getch();
}
Let us have another example of checking a number for even or
odd.
OUTPUT
// Program
: to check whether a given number is even or odd.
#include<stdio.h>
Please enter two numbers to check : 87
#include<conio.h>
The given number 87 is odd.

void main ()
{
int a;
clrscr();

printf(“\nPlease enter a number to check for even/odd :


“);
scanf(“%d”, &a);

if (a%2==0)
printf(“The given number %d is even.”,a);
else
printf(“The given number %d is odd.”, a);

getch();
}
So, a complex program has become so easy. Hence using the
else condition is much effective in many manners. In the similar
manners, we can use the condition for the characters also.
Nested if condition

Whenever we say ‘nested’, it always means for the many


conditions. Also, the conditions are applied within other
conditions. Here, if we are saying ‘Nested if’ then it refers to
the if condition in other if condition. So, if we need to apply the
nested conditions, the if condition structure must be in other if
condition(s). It is a little bit complicated to understand. Lets
have a look at this program of finding the grade of a student.
The conditions must be as follows :

1. If the percentage is more than 75, the student must be


given Honours.
2. If the percentage is less than 75 but more than 60 then
the First division must be given.
3. If the percentage is between 45 and 60 then Second
division is given.
4. If the percentage is below 45 but above 33 then he/she is
having third division.
5. If all of the above conditions are false then the student is
failed to pass the examination.

We will form a tree of this structure :


Per
Fa >
Hono
Seco
Fir
Thi
urs
st75
33
45
60
nd
rd
il
So, we can use the if conditions at the nodes and the result
from them can be the statement from the conditions. We can
use the following condition now.

if (per>75)
printf(“Honours”);
else
{
if (per>60)
printf(“First”);
else
{
if (per>45)
printf(“Second”);
else
{
if (per>33)
printf(“Third”);
else
printf(“Fail”);
}
}
}

So, using several blocks is not so good. This can also be further
reduced to:

if (per>75)
printf(“Honours”);
else if (per>60)
printf(“First”);
esle if (per>45)
printf(“Second”);
else if (per>33)
printf(“Third”);
else
printf(“Fail”);

Let us implement this in a program : This program will


check for the division of a student depending upon the
percentage.
OUTPUT
// Program
: to find the division of any student by percentage
calculated
Please enter
bytwo
// input
numbers
marks.
to check : 65 78 67 87
#include<stdio.h>
First
#include<conio.h>

void main ()
{
int m1, m2, m3, m4;
float per;
clrscr();

printf(“\nPlease enter the marks of four subjects : “);


scanf(“%d %d %d %d”, &m1, &m, &m3, &m4);

per = (float)((m1+m2+m3+m4)/4);

if (per>75)
printf(“Honours”);
else if (per>60)
printf(“First”);
esle if (per>45)
printf(“Second”);
else if (per>33)
printf(“Third”);
else
printf(“Fail”);

getch();
}
GATES

Generally, three type of gates are there on the world of


computers. Namely :

1. AND
2. OR
3. NOT

AND Gate :
In this gate, the result is true when two or more than two
conditions are true. All of the conditions that are applied must
be true to get the result as true. In all other cases, the result
will be false. In C, the AND gate is represented by && sign.
Here is the table for the AND gate with two inputs.

A B A && A B A &&
B B

0 0 0 False False False


0 1 0 False True False
1 0 0 True False False
1 1 1 True True True

OR Gate :

In this gate, the result is found true even if one result is true.
This is represented by || in C.

A B A || B A B A || B

0 0 0 False False False


0 1 1 False True True
1 0 1 True False True
1 1 1 True True True

NOT Gate :

NOT Gate means for the opposite i.e. if the result is true, it
inverts it to false and vice versa. In C, it is represented by !
symbol.

A !A A !A

0 1 False True
1 0 True False
These gates are useful when we want to apply more than one
condition in our program. The use of the gates depends upon
the nature of the program. Suppose we want to check whether
the number lies between 50 and 100 or not. Then the condition
will be :

if (a>=50 && a<=100)

To check whether the number is less than 50 or greater than


100 then the condition will be :

if (a<=50 || a>=100)

To check whether the number lies in the range of 1-25 or 50-75


then the condition will be :

if ( (a>=1 && a<=25) || (a>=50 && a<=75) )

Like this we can apply the conditions as per our requirement.

Let us check whether a given character is a vowel or


consonant:
OUTPUT
// Program
: to check whether a given character is vowel or
consonant.
Please enter a character to check : f
#include<stdio.h>
The given character is a consonant.
#include<conio.h>

void main ()
{
char ch;
clrscr();

printf(“\nPlease enter a character to check : “);


scanf(“%c”, &ch);

if (ch==’a’ || ch==’A’ || ch==’e’ || ch==’E’ || ch==’i’ ||


ch==’I’ || ch==’o’ || ch==’O’ || ch==’u’ || ch==’U’)
printf(“The given character is a vowel.”);
else
printf(“The given character is a consonant.”);

getch();
}
However, this program is correct in recognising the vowels but
it is not able to recognise the consonants correctly because a
user may enter a number or a special character also which will
not be a vowel hence they will be also shown as consonants.
Hence the need of ASCII codes is arisen.

To do so, the letters must lie in the range of the characters i.e.
65 to 90 (for capital alphabets) or 97 to 122 (for small
alphabets).

Hence we will use a if condition before the check of vowel as :-

if ( (ch>=65 && ch<=90) || (ch>=97 && ch<=122) )


{
<condition for vowel check>
}

There is no need of this block also as the if condition that is


applied in this condition will be itself considered as a single
statement. So, our program again shorts up to :

if ( (ch>=65 && ch<=90) || (ch>=97 && ch<=122) )


<condition for vowel check>

Let’s have another example of checking whether a given


character is in small letters, Capital letters, Digit or a special
character :
// Program to check whether a given character is vowel small
letter, capital // letter, digit or a special character.
#include<stdio.h>
#include<conio.h>

void main ()
{
char ch;
clrscr();

printf(“\nPlease enter a character to check : “);


scanf(“%c”, &ch);

if (ch>=48 && ch<=57)


printf(“The given character is a Digit.”);
else if (ch>=65 && ch<=90)
printf(“The given character is a Capital Letter.”);
else if (ch>=90 && ch<=122)
printf(“The given character is a small letter.”);

OUTPUT
else:
Please enter
printf(“The
a character
given
to check
letter is
: ~a special character.”);
The given character is a special character.
getch();
}
There is one more example of character: There two numbers
will be asked for user to enter. Then a operator will be asked to
enter (+,-,*,/,%). Depending upon the operator, the calculation
will be performed and the final result will be displayed. If a
wrong choice is entered, a error message will appear.

// Program to get two values and perform desired operation


on them.
#include<stdio.h>
#include<conio.h>

void main ()
{
int a, b;
char ch;
clrscr();

printf(“Please enter two values to perform operation :


”);
scanf(“%d %d”, &a, &b);
printf(“\nPlease enter a operator : “);
scanf(“%c”, &ch);

if (ch==’+’)
printf(“The sum is %d.”,(a+b));
else if (ch==’-’)
printf(“The subtraction is %d.”,(a-b));
OUTPUT
else: if (ch==’*’)
Please enterprintf(“The
two values
multiplication
to perform operation
is %d.”,(a*b));
: 79 4
Please
else
enter
if (ch==’/’)
a operator : %
The modulo printf(“The
is 3 division is %d.”,(a/b));
else if (ch==’%’)
printf(“The modulo is %d”,(a%b));
else
printf(“Illegal input. “);

getch();
}
SWITCH CONDITION :

A switch condition is more advance technique to prevent the


time consumption o the program compilation. In this technique
we uses a variable and then cases are applied on the values of
the variable. The syntax of the condition is as follows :

switch (<variable name>)


{
case <val 1>:
Statement 1;
.
Statement n1;
break;
case <val 2>:
Statement 1;
.
Statement n2;
break;

.
.
.
case <val m>:
Statement 1;
.
Statement n3;
break;

default :
Statement 1;
.
Statement n4;
break;
}

Here <variable name> is the name of the variable on which the


condition is applied. val1, val2, ... , valm are the possible values
of the variable. If any particular val is matched with the value of
the variable then the control directly enters to the body of that
particular case only. It executes up to the break statement.
default is just like the else statement of the if statement. if any
case is not found, default is executed. Just like the if statement,
default is optional. There will be no effect if we don’t apply the
default block.

Lets take the example of weekday. From 1 to 7, the display of


the name of the day will be there. The number must be entered
by the user.

// Program to get the weekday by number from 1 to 7.


#include<stdio.h>
#include<conio.h>

void main()
{
int a;
clrscr();

printf(“Enter the number : “);


scanf(“%d”,&a);

switch (a)
{
case 1:
printf(“Monday”);
break;
case 2:
printf(“Tuesday”);
break;
case 3:
printf(“Wednesday”);
break;
OUTPUT : case 4:
Enter a numberprintf(“Thursday”);
:5
Friday break;
case 5:
printf(“Friday”);
break;
case 6:
printf(“Saturday”);
break;
case 7:
printf(“Sunday”);
break;
default :
printf(“Wrong input”);
break;
}
getch();
}
In the same manners, switch condition can also be applied to
characters. Remind the previous example in which we have
taken any operator as input and then operation is performed on
two floating point numbers depending upon the operator. We
can use switch to do the same task. By using the switch, the
program will be more easy and easy to understand.
// Program to get two values and perform desired operation
on them using switch condition.
#include<stdio.h>
#include<conio.h>

void main ()
{
int a, b;
char ch;
OUTPUTclrscr();
:
Please enter two values to perform operation : 45 6
Pleaseprintf(“Please
enter a operator
enter: two
/ values to perform operation : ”);
Division
scanf(“%d
=3 %d”, &a, &b);
printf(“\nPlease enter a operator : “);
scanf(“%c”, &ch);

switch (ch)
{
case ‘+’:
printf(“Sum = %d”, (a+b));
break;
case ‘-‘:
printf(“Subtraction = %d”,(a-b));
break;
case ‘*’:
printf(“Multiplication = %d”,(a*b));
break;
case ‘/’:
printf(“Division = %d”,(a/b));
break;
case ‘%’:
printf(“Modulo = %d”,(a%b));
default :
printf(“Wrong input”);
break;
getch();
}
Now, we will reprogram the program of checking whether a
given character is vowel or not using the switch condition. This
program will be much easier than before. There is one more
property of switch. If we need to perform the same operation in
different cases then we can write the cases together.
Just have a look :

// program to check whether a given character is vowel or


not using switch // condition.
#include<stdio.h>
#include<conio.h>

void main()
{
char ch;
clrscr();

printf(“\nPlease enter a character to check for vowel :


“);
scanf(“%c”, &ch);

switch (ch)
{
case ‘A’:
case ‘a’:
case ‘E’:
case ‘e’:
case ‘I’:
case ‘i’:
case ‘O’:
case ‘o’:
case ‘U’:
case ‘u’:
printf(“vowel.”);
break;
default:
printf(“Not a vowel.”);
break;
}
getch();
}

OUTPUT :
Please enter a character to check for vowel : x
Not a vowel.

You might also like