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.