Professional Documents
Culture Documents
IF CONDITION :
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 (<logic>)
{
Statement 1;
Statement 2;
.
.
.
Statement n;
}
void main ()
{
int a;
clrscr();
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();
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();
if (a==’A’)
printf(“The given character is A.”);
getch();
}
if...else condition
if (<logic>)
{
Statement 1;
Statement 2;
.
.
Statement n;
}
else
{
Statement 1;
Statement 2;
.
.
Statement n;
}
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();
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();
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
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”);
void main ()
{
int m1, m2, m3, m4;
float per;
clrscr();
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
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
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
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)
void main ()
{
char ch;
clrscr();
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).
void main ()
{
char ch;
clrscr();
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.
void main ()
{
int a, b;
char ch;
clrscr();
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 :
.
.
.
case <val m>:
Statement 1;
.
Statement n3;
break;
default :
Statement 1;
.
Statement n4;
break;
}
void main()
{
int a;
clrscr();
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 :
void main()
{
char ch;
clrscr();
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.