You are on page 1of 5

Conditional Statement

A conditional statement allows us to control whether a program segment will be executed or not.
Conditional Statements in C programming are used to make decisions based on the conditions.
Statements in C execute sequentially when there is no condition around the statements.
If you put some condition for a block of statements, the execution flow may change based on the
result evaluated by the condition. This process is called decision making in C.
Two constructs for conditional statements:
IF statements
IF
IF... ELSE
IF...ELSE IF...ELSE

SWITCH statements

IF statement:
The syntax for if statement is as follows:
if (condition)
instruction;

The condition evaluates to either true or false. True is always a non-zero value, and false is a
value that contains zero. Instructions can be a single instruction or a code block enclosed by
curly braces{}.
Example:
#include<stdio.h>
int main()
{
int num1=1;
int num2=2;
if(num1<num2) //test-condition
{
printf("num1 is smaller than num2");
}
return 0;
}

Output: num1 is smaller than num2

IF ... ELSE statement:


The if-else is statement is an extended version of If. The general form of if-else is as follows:
if (test-expression)
{
True block of statements
}
Else
{
False block of statements
}
Statements;

Here, if the value of test-expression is True, then the True block of statements will be executed.
If the value of test-expression is False, then the False block of statements will be executed. In
any case, after the execution, the control will be automatically transferred to the statements
appearing outside the block of If.

Example:

#include<stdio.h>
int main()
{
int num=19;
if(num<10)
{
printf("The value is less than 10");
}
else
{
printf("The value is greater than 10");
}
return 0;
}

Output: The value is greater than 10

Nested IF ... ELSE Statements:


When a series of decision is required, nested if-else is used. Nesting means using one if-else
construct within another one.
Example:
#include<stdio.h>
int main()
{
int num=1;
if(num<10)
{
if(num==1)
{
printf("The value is:%d\n",num);
}
else
{
printf("The value is greater than 1");
}
}
else
{
printf("The value is greater than 10");
}
return 0;
}

Output: The value is:1

IF ... ELSE IF ... ELSE statements:


This conditional statement is known as ELSE IF LADDER or NESTED ELSE...IF in different
books. This conditional statement is when multipath decisions are required.
Basic syntax:
if (test - expression 1) {
statement1;
} else if (test - expression 2) {
Statement2;
} else if (test - expression 3) {
Statement3;
} else if (test - expression n) {
Statement n;
} else {
default;
}
Statement x;

This chain generally looks like a ladder hence it is also called as an else-if ladder. The test-
expressions are evaluated from top to bottom. Whenever a true test-expression is found,
statement associated with it is executed. When all the n test-expressions become false, then the
default else statement is executed.
Example:
#include<stdio.h>
int main()
{
int marks=83;
if(marks>75){
printf("First class");
}
else if(marks>65){
printf("Second class");
}
else if(marks>55){
printf("Third class");
}
else{
printf("Fourth class");
}
return 0;
}

Output: First class


SWITCH statements:
A switch statement allows a variable to be tested for equality against a list of values. Each value
is called a case, and the variable being switched on is checked for each switch case.
The syntax for a switch statement in C programming language is as follows:
switch(expression) {

case constant-expression :
statement(s);
break; /* optional */

case constant-expression :
statement(s);
break; /* optional */

/* you can have any number of case statements */


default : /* Optional */
statement(s);
}

The following rules apply to a switch statement:

1. The expression used in a switch statement must have an integral or enumerated type, or
be of a class type in which the class has a single conversion function to an integral or
enumerated type.
2. You can have any number of case statements within a switch. Each case is followed by
the value to be compared to and a colon.
3. The constant-expression for a case must be the same data type as the variable in the
switch, and it must be a constant or a literal.
4. When the variable being switched on is equal to a case, the statements following that case
will execute until a break statement is reached.
5. When a break statement is reached, the switch terminates, and the flow of control jumps
to the next line following the switch statement.
6. Not every case needs to contain a break. If no break appears, the flow of control will fall
through to subsequent cases until a break is reached.
7. A switch statement can have an optional default case, which must appear at the end of
the switch. The default case can be used for performing a task when none of the cases is
true. No break is needed in the default case.

Example:

#include <stdio.h>

int main () {

/* local variable definition */


char grade = 'B';

switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}

printf("Your grade is %c\n", grade );

return 0;
}

Output:

Well done
Your grade is B

Homework:
1. Write a C program to show whether a number is negative, positive or zero.
2. Write a C program to check whether an integer is odd or even.
3. Write a C program to find the largest number among three numbers.
4. Write a C program to check whether a number is divisible by 5 and 11 or not.
5. Write a C program to check whether a year is leap year or not.
6. Write a C program to check whether a character is alphabet or not.
7. Write a C program to input any alphabet and check whether it is vowel or consonant.
8. Write a C program to take mark as input and output grade based on following
conditions:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F

You might also like