UNIT - 2
if' Statement in C
What is Decision Making Statement?
In the C programming language, the program execution flow is line by line from top to bottom.
That means the c program is executed line by line from the main method. But this type of
execution flow may not be suitable for all the program solutions. Sometimes, we make some
decisions or we may skip the execution of one or more lines of code. Consider a situation, where
we write a program to check whether a student has passed or failed in a particular subject. Here,
we need to check whether the marks are greater than the pass marks or not. If marks are greater,
then we decide that the student has passed otherwise failed. To solve such kind of problems in c
we use the statements called decision making statements.
Decision-making statements are the statements that are used to verify a given condition
and decide whether a block of statements gets executed or not based on the condition
result.
In the c programming language, there are two decision-making statements they are as follows.
1. if statement
2. switch statement
if statement in c
In c, if statement is used to make decisions based on a condition. The if statement verifies the
given condition and decides whether a block of statements are executed or not based on the
condition result. In c, if statement is classified into four types as follows...
1. Simple if statement
2. if-else statement
3. Nested if statement
4. if-else-if statement (if-else ladder)
Simple if statement
Simple if statement is used to verify the given condition and executes the block of statements
based on the condition result. The simple if statement evaluates specified condition. If it is
TRUE, it executes the next statement or block of statements. If the condition is FALSE, it skips
the execution of the next statement or block of statements. The general syntax and execution
flow of the simple if statement is as follows.
Simple if statement is used when we have only one option that is executed or skipped based on a
condition.
Example Program | Test whether given number is divisible by 5.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%5 == 0 )
printf("Given number is divisible by 5\n") ;
printf("statement does not belong to if!!!") ;
}
Output 1:
Output 2:
if-else statement
The if-else statement is used to verify the given condition and executes only one out of the two
blocks of statements based on the condition result. The if-else statement evaluates the specified
condition. If it is TRUE, it executes a block of statements (True block). If the condition is
FALSE, it executes another block of statements (False block). The general syntax and execution
flow of the if-else statement is as follows.
The if-else statement is used when we have two options and only one option has to be executed
based on a condition result (TRUE or FALSE).
Example Program | Test whether given number is even or odd.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%2 == 0 )
printf("Given number is EVEN\n") ;
else
printf("Given number is ODD\n") ;
}
Output 1:
Output 2:
Nested if statement
Writing a if statement inside another if statement is called nested if statement. The general syntax
of the nested if statement is as follows...
The nested if statement can be defined using any combination of simple if & if-else statements.
Example Program | Test whether given number is even or odd if it is below 100.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n < 100 )
{
printf("Given number is below 100\n") ;
if( n%2 == 0)
printf("And it is EVEN") ;
else
printf("And it is ODD") ;
}
else
printf("Given number is not below 100") ;
}
Output 1:
Output 2:
if-else-if statement (if-else ladder)
Writing a if statement inside else of an if statement is called if-else-if statement. The general
syntax of the if-else-if statement is as follows...
The if-else-if statement can be defined using any combination of simple if & if-else statements.
Example Program | Find the largest of three numbers.
#include<stdio.h>
#include<conio.h>
void main(){
int a, b, c ;
clrscr() ;
printf("Enter any three integer numbers: ") ;
scanf("%d%d%d", &a, &b, &c) ;
if( a>=b && a>=c)
printf("%d is the largest number", a) ;
else if (b>=a && b>=c)
printf("%d is the largest number", b) ;
else
printf("%d is the largest number", c) ;
}
Output:
switch' statement in C
Consider a situation in which we have many options out of which we need to select only one
option that is to be executed. Such kind of problems can be solved using nested if statement. But
as the number of options increases, the complexity of the program also gets increased. This type
of problem can be solved very easily using a switch statement. Using the switch statement, one
can select only one option from more number of options very easily. In the switch statement, we
provide a value that is to be compared with a value associated with each option. Whenever the
given value matches the value associated with an option, the execution starts from that option. In
the switch statement, every option is defined as a case.
The switch statement has the following syntax and execution flow diagram.
The switch statement contains one or more cases and each case has a value associated with it. At
first switch statement compares the first case value with the switchValue, if it gets matched the
execution starts from the first case. If it doesn't match the switch statement compares the second
case value with the switchValue and if it is matched the execution starts from the second case.
This process continues until it finds a match. If no case value matches with the switchValue
specified in the switch statement, then a special case called default is executed.
When a case value matches with the switchValue, the execution starts from that particular case.
This execution flow continues with the next case statements also. To avoid this, we use the
"break" statement at the end of each case. That means the break statement is used to terminate
the switch statement. However, it is optional.
Example Program | Display pressed digit in words.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Enter any digit: ") ;
scanf("%d", &n) ;
switch( n )
{
case 0: printf("ZERO") ;
break ;
case 1: printf("ONE") ;
break ;
case 2: printf("TWO") ;
break ;
case 3: printf("THREE") ;
break ;
case 4: printf("FOUR") ;
break ;
case 5: printf("FIVE") ;
break ;
case 6: printf("SIX") ;
break ;
case 7: printf("SEVEN") ;
break ;
case 8: printf("EIGHT") ;
break ;
case 9: printf("NINE") ;
break ;
default: printf("Not a Digit") ;
}
getch() ;
}
Output 1:
Output 2:
Conditional or Ternary Operator (?:) in C
The conditional operator in C is kind of similar to the if-else statement as it follows the same
algorithm as of if-else statement but the conditional operator takes less space and helps to write
the if-else statements in the shortest way possible. It is also known as the ternary operator in
C as it operates on three operands.
Syntax of Conditional/Ternary Operator in C
The conditional operator can be in the form
variable = Expression1 ? Expression2 : Expression3;
Or the syntax can also be in this form
variable = (condition) ? Expression2 : Expression3;
Or syntax can also be in this form
(condition) ? (variable = Expression2) : (variable = Expression3);
Conditional/Ternary Operator in C
It can be visualized into an if-else statement as:
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Since the Conditional Operator ‘?:’ takes three operands to work, hence they are also
called ternary operators.
Note: The ternary operator have third most lowest precedence, so we need to use the
expressions such that we can avoid errors due to improper operator precedence management.
Working of Conditional/Ternary Operator in C
The working of the conditional operator in C is as follows:
Step 1: Expression1 is the condition to be evaluated.
Step 2A: If the condition(Expression1) is True then Expression2 will be executed.
Step 2B: If the condition(Expression1) is false then Expression3 will be executed.
Step 3: Results will be returned.
Flowchart of Conditional/Ternary Operator in C
To understand the working better, we can analyze the flowchart of the conditional operator
given below.
Flowchart of conditional/ternary operator in C
Examples of C Ternary Operator
Example 1: C Program to Store the greatest of the two Numbers using the ternary
operator
C
// C program to find largest among two
// numbers using ternary operator
#include <stdio.h>
int main()
int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is %d > %d",
m, n)
: printf("n is greater than m that is %d > %d",
n, m);
return 0;
Output
m is greater than n that is 5 > 4
goto statement
The goto statement is used to jump from one line to another line in the program.
Using goto statement we can jump from top to bottom or bottom to top. To jump from one line
to another line, the goto statement requires a label. Label is a name given to the instruction or
line in the program. When we use a goto statement in the program, the execution control directly
jumps to the line with the specified label.
Example Program for goto statement.
#include<stdio.h>
#include<conio.h>
void main(){
clrscr() ;
printf("We are at first printf statement!!!\n") ;
goto last ;
printf("We are at second printf statement!!!\n") ;
printf("We are at third printf statement!!!\n") ;
last: printf("We are at last printf statement!!!\n") ;
getch() ;
}
Output:
'while' statement in C
Consider a situation in which we execute a single statement or block of statements repeatedly for
the required number of times. Such kind of problems can be solved using looping statements in
C. For example, assume a situation where we print a message 100 times. If we want to perform
that task without using looping statements, we have to either write 100 printf statements or we
have to write the same message 100 times in a single printf statement. Both are complex
methods. The same task can be performed very easily using looping statements.
The looping statements are used to execute a single statement or block of statements
repeatedly until the given condition is FALSE.
C language provides three looping statements...
while statement
do-while statement
for statement
while Statement
The while statement is used to execute a single statement or block of statements repeatedly as
long as the given condition is TRUE. The while statement is also known as Entry control
looping statement. The while statement has the following syntax...
The while statement has the following execution flow diagram...
At first, the given condition is evaluated. If the condition is TRUE, the single statement or block
of statements gets executed. Once the execution gets completed the condition is evaluated again.
If it is TRUE, again the same statements get executed. The same process is repeated until the
condition is evaluated to FALSE. Whenever the condition is evaluated to FALSE, the execution
control moves out of the while block.
Example Program | Program to display even numbers upto 10.
#include<stdio.h>
#include<conio.h>
void main(){
int n = 0;
clrscr() ;
printf("Even numbers upto 10\n");
while( n <= 10 )
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}
getch() ;
}
Output:
do-while' statement in C
The do-while statement is used to execute a single statement or block of statements repeatedly as
long as given the condition is TRUE. The do-while statement is also known as the Exit control
looping statement. The do-while statement has the following syntax...
The do-while statement has the following execution flow diagram...
At first, the single statement or block of statements which are defined in do block are executed.
After the execution of the do block, the given condition gets evaluated. If the condition is
evaluated to TRUE, the single statement or block of statements of do block are executed again.
Once the execution gets completed again the condition is evaluated. If it is TRUE, again the
same statements are executed. The same process is repeated until the condition is evaluated to
FALSE. Whenever the condition is evaluated to FALSE, the execution control moves out of the
while block.
Example Program | Program to display even numbers upto 10.
#include<stdio.h>
#include<conio.h>
void main(){
int n = 0;
clrscr() ;
printf("Even numbers upto 10\n");
do
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}while( n <= 10 ) ;
getch() ;
}
Output:
for' statement in C
The for statement is used to execute a single statement or a block of statements repeatedly as
long as the given condition is TRUE. The for statement has the following syntax and execution
flow diagram...
At first, the for statement executes initialization followed by condition evaluation. If the
condition is evaluated to TRUE, the single statement or block of statements of for statement are
executed. Once the execution gets completed, the modification statement is executed and again
the condition is evaluated. If it is TRUE, again the same statements are executed. The same
process is repeated until the condition is evaluated to FALSE. Whenever the condition is
evaluated to FALSE, the execution control moves out of the for block.
Example Program | Program to display even numbers upto 10.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Even numbers upto 10\n");
for( n = 0 ; n <= 10 ; n++ )
{
if( n%2 == 0)
printf("%d\t", n) ;
}
getch() ;
}
Output:
break, continue and goto in C
In c, there are control statements that do not need any condition to control the program execution
flow. These control statements are called as unconditional control statements. C programming
language provides the following unconditional control statements...
break
continue
goto
The above three statements do not need any condition to control the program execution flow.
break statement
In C, the break statement is used to perform the following two things...
1. break statement is used to terminate the switch case statement
2. break statement is also used to terminate looping statements like while, do-while
and for.
When a break statement is encountered inside the switch case statement, the execution control
moves out of the switch statement directly. For example, consider the following program.
Example Program to perform all arithmetic operations using switch statement.
#include<stdio.h>
#include<conio.h>
void main(){
int number1, number2, result ;
char operator;
clrscr() ;
printf("Enter any two integer numbers: ") ;
scanf("%d%d", &number1, &number2) ;
printf("Please enter any arithmetic operator: ");
operator = getchar();
switch(operator)
{
case '+': result = number1 + number2 ;
printf("Addition = %d", result) ;
break;
case '-': result = number1 - number2 ;
printf("Subtraction = %d", result) ;
break;
case '*': result = number1 * number2 ;
printf("Multiplication = %d", result) ;
break;
case '/': result = number1 / number2 ;
printf("Division = %d", result) ;
break;
case '%': result = number1 % number2 ;
printf("Remainder = %d", result) ;
break;
default: printf("\nWrong selection!!!") ;
}
getch() ;
}
Output:
When the break statement is encountered inside the looping statement, the execution control
moves out of the looping statements. The break statement execution is as shown in the following
figure.
For example, consider the following example program...
Example Program for break statement.
#include<stdio.h>
#include<conio.h>
void main(){
char ch ;
clrscr() ;
do
{
printf("Enter Y / N : ") ;
scanf("%c", &ch) ;
if(ch == 'Y')
{
printf("Okay!!! Repeat again !!!\n") ;
}
else
{
printf("Okay !!! Breaking the loop !!!") ;
break ;
}
} while( 1 ) ;
getch() ;
}
Output:
continue statement
The continue statement is used to move the program execution control to the beginning of the
looping statement. When the continue statement is encountered in a looping statement, the
execution control skips the rest of the statements in the looping block and directly jumps to the
beginning of the loop. The continue statement can be used with looping statements like while,
do-while and for.
When we use continue statement with while and do-while statements the execution control
directly jumps to the condition. When we use continue statement with for statement the
execution control directly jumps to the modification portion (increment/decrement/any
modification) of the for loop. The continue statement execution is as shown in the following
figure.
Example Program Program to illustrate continue statement.
#include<stdio.h>
#include<conio.h>
void main(){
int number ;
clrscr() ;
while( 1 )
{
printf("Enter any integer number: ") ;
scanf("%d", &number) ;
if(number%2 == 0)
{
printf("Entered number is EVEN!!! Try another number!!!\n") ;
continue ;
}
else
{
printf("You have entered ODD number!!! Bye!!!") ;
exit(0) ;
}
}
getch() ;
}
Output: