82 Programming and Data Structures
The conditional statements use relational operators, which have been explained in the third chapter.
The relational operators are useful for comparing the two values. They can be used to check whether
they are equal to each other, unequal or which one is smaller/greater than the other.
5.2 THE i f STATEMENT
C uses the keyword i f to execute a set of command lines or one command line when the logical
condition is true. It has only one option. The set of command lines or command lines are executed
only when the logical condition is true.
Syntax for the simplest if statement
If (condition ) /* no semi-colon */
Statement;
The statement is executed only when the condition is true. In case the condition is false the compiler
skips the lines within the i f block. The condition is always enclosed within a pair of parenthesis.
The conditional statements should not be terminated with semi-colons (;). The statements following
the i f statement are normally enclosed with curly braces. The curly braces indicate the scope of the
i f Statement. The default scope is one statement. But it is a good practice to use curly braces even
with a single statement.
Given below is a simple program that demonstrate the use of i f statement.
5.1 Write a program to check whether the entered number is less than 10 ? if yes, display the
same.
mainO
int v;
clrsc r ()?
p rin tf (''Enter th e number :") ;
S c a n f ("%d", &v);
If (v<10)
printf (" Number entered is less than 10");
sleepd); f* process halts for given value in seconds *1
i
OUTPUT : Enter the number : 9
Nsmber entered is less than 10
Or
Enier the number: 11
Explanation In the above program the user can enter the number. The entered number is checked
with i f statem ent. If it is less than 10 a message Number entered is less than 10" is displayed. For
the sake of understanding the flow chart 5.1 is given below for the above program.
Decision Statements 83
5-2 Write a program to check equivalence of two numbers. Use if statement.
9 include <«tdio.h>
# include <conio.h>
▼old MainO
{
int m,n;
clrscr()i
printf ("n Enter Two Numbers
scanf (%d %d",&cm,&cn);
if
(ms=cO)
printf C'n Two numbers are equal/');
getcheO;
OUTPUT;
Enter Two Numbers : 5 5
Two numbers are equal.
84 Programming and Data Structures
Explanation The two numbers are entered. They are checked by i f statement whether their difference
is 0 or not. If yes,one can confirm that they are equal or otherwise unequal. If the numbers are equal a
message will be displayed as shown in the output.
5.3 Write a program to check whether the canditate's age is greater than 17 or not. If yes,
display message "Eligible for Voting".
# inclu d e < etd io.h >
# in clu d e <conio.h>
void mainO
{
in t age;
c l r s c r () ;
printf ("n Enter age : ");
scanf ("% d", Stage);
if(age>17)
printf (" Eligible fo r Voting/');
getchO;
)
OUTPUT;
Enter age : 20
Eligible for Voting.
Explanation In the above program age is entered through the keyboard. If the age is greater than
17 a message will be displayed as shown in the output.
5.4 Write a program to use curly brace in the if block. Enter only the three numbers and
calculate their sum and multiplication.
# In clu d e < std io.h >
# in clu d e < con io.h>
vo id main()
{
int a, b, c , x;
c lr s c r ();
printf ("nEnter Three Numbers:");
x=scanf ("%d %d %W",&«,&*>,&c);
if (x==3)
printf (" Addition : %d",a+b+c);
printf ( Multiplication : %d", a*b*c);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.12.156