If Statement
If Statement
If Statement
CONTROL STATEMENTS:
Control statements - Category of statements, Selection, Iteration, Jump, Label, Expression and Block.
Control statements - Category of statements, Selection, Iteration, Jump, Label, Expression and
Block.
Control Statements
Category of Statements:
We know that the code in the C program sis executed sequentially from the first line of the
program to its last line. The order in which the program statements are executed is known as ‘Flow of
Program control’. By default, the program control flows sequentially from top to bottom. Many practical
situations like decision making, repetitive execution of a certain task, etc. require deviation or alteration
from the default flow of program control. This can be altered by using Flow Control Statements. They
are of two types:
1. Branching Statements
a. Selection Statements
b. Jump Statements
2. Iteration Statements
Branching Statements
Branching statements are used to transfer the program control from one point to another. They are
categorized as
a. Conditional Branching: It is also called as Selection Statements, program control
is transferred from one point to another based upon the outcome of a certain
condition. The following selection statements are available in C: if statement, if –
else statement, switch statement.
b. Unconditional Branching: It is also called as Jumping, program control is
transferred from one point to another without checking any condition. The following
jump statements are available in C: goto statement, break statement, continue
statement and return statement.
Conditional Branching or Selection Statements
Based upon the outcome of a particular condition, selection statements transfer control from one point to
another. The selection statements available in C are as follows:
1. if statement
2. if – else statement
3. if – else if statement
4. switch statement
if statement:
Syntax of if
if(Expression) // if header
Statement 1; //if body
Statement X;
(Or)
if(Expression) // if header
{
Statement 1; //if body
:
Statement n;
}
Statement X;
//if statement
#include<stdio.h>
main()
{
int a=5, b=10;
if(a>b&&a>10)
{
printf(“a is greater than 10”);
printf(“a is greater than b”);
}
}
if – else statement:
Most of the problems require one set of actions to be performed if a particular condition is true, and
another set of actions to be performed if the condition is false. To implement such a decision, C language
provides an if – else statement.
Syntax:
if(Expression) // if header
Statement 1; //if body
else // else clause
Statement 2; // else body
(Or)
if(Expression) // if header
{
Statement 1; //if body
:
Statement n;
}
else // else clause
{
Statement 1; //else body
:
Statement n;
}
1. An if-else statement consists of an if -else header, if body, else clause and else body.
2. An if-else header consists of an if clause followed by an if – else controlling expression enclosed
within parentheses.
3. An if-else statement is executed as follows:
a. The if – else controlling expression is evaluated.
b. If the if – else controlling expression evaluates to true, the statement constituting if body
is executed and the else body is skipped.
c. If the if - else controlling expression evaluates to false, if body is skipped and the else
body is executed.
d. After the execution of the if body or else body, the execution continues from the
statement following the if – else statement.
Example:
//if – else statement
#include<stdio.h>
main()
{
int a=11;
if(a>10)
printf(“The value of a is %d”,a);
printf(“Value a is greater than 10”);
else
printf(“Value a is less than 10”);
}
O/P:
Compilation error “Misplaced else in function main”
Nested if Statement
The body of the if statement is another if statement or contains another if statement, then we say that if’s
are nested and the statement is called Nested if statement.
Syntax:
if(expression)
if statement // nested if statement
(or)
if(expression)
{
Statement
------------
If statement // nested if statement
------------
}
(or)
if(expression1)
if(expression2)
-------------
if(expression n)
statement
The above structure seems to form a ladder and is known as if ladder.
Nested if – else statement
In a Nested if – else statement the if body or else body of an if – else statement is or contains, another if
statement or if - else statement. This will be demonstrated with the help of following program.
Finding the greatest among the three given numbers.
#include <stdio.h>
main()
{
int a, b,c;
printf("Enter Three different values\n");
scanf("%d %d %d", &a, &b, &c);
if(a > b)
{
if(a>c)
printf("%d is Largest\n", a);
else
printf("%d is Largest\n", c);
}
else
{
if(b>c)
printf("%d is Largest\n", b);
else
printf("%d is Largest\n", c);
}
}
In Nested if – else statement we have an ambiguity referred as Dangling else ambiguity. When a
statement contains more number of if clauses than else clauses, then there exists ambiguity regarding with
which if clause does the else clause properly matches up this ambiguity is known as Dangling else
Problem.
Example:
// Dangling else Problem
#include<stdio.h>
main()
{
int a=10,b=20;
if(a==100)
if(b==20)
printf(“Match - I”);
else
printf(“Match - II”);
}
O/P: No output
The dangling problem is solved in two ways:
1. Implicitly by compiler: The dangling problem is implicitly resolved by the compiler by matching
the else clause with the last occurring unmatched if.
2. Explicitly by the user: The dangling problem is explicitly removed by the user by using braces.
Write a C program to enter a number from 1 to 7 and display the corresponding day of the
week using switch statement
#include <stdio.h>
main()
{
int week;
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}
}
Un Conditional Branching (or) Jump Statements
Jump statements transfers the control from one point to another without checking any condition i.e.,
unconditionally. The following Jump statements are present in C language.
1. goto statement
2. break statement
3. continue statement
4. return statement
1. goto Statement
It is used to branch unconditionally from one point to another within a function. It often makes the
program control difficult to understand and modify. Thus the use of goto statement is discouraged in
structured programming languages.
Syntax:
goto label;
Example:
Forward Jump
goto label;
------------
------------
label:
statement;
Backward Jump
label:
statement;
------------
------------
goto label;
Multiple goto statements are allowed
goto label;
------------
------------
goto label;
------------
label:
statement;
------------
------------
goto label;
label:
statement;
------------
------------
goto label;
------------
------------
label:
statement;
2. break Statement:
The syntactic form of a break statement is:
break;
1. A break statement can appear only inside or a body of a switch statement, or a loop.
2. A break statement terminates its execution of the nearest enclosing switch or the nearest
enclosing loop. The execution resumes with the statement present next to the terminated switch or
terminated loop.
Example:
#include<stdio.h>
main()
{
int a=100;
if(a==100)
{
printf(“If controlling expression evaluates to true”);
break;
printf(“After break”);
}
}
O/P:Compilation error “Misplaced break in function main”.
Reason: The break statement should appear only in a switch or a loop.
3. continue Statement:
The syntactic form of a continue statement is:
continue;
1. It appears only inside the body or a loop.
2. It terminates the current iteration of the nearest enclosing loop.
4. return Statement:
The syntactic form of a return statement is:
return; or
return expression;
1. A return statement without an expression can appear only in a function whose return type is
void.
2. A return statement with an expression should not appear in a function whose return type is
void.
3. A return statement terminates the execution of a function and returns the control to the calling
function.
Iteration statements
Iteration is a process of repeating the same set of statements again and again until the specified condition
holds true. The C language provides the following three iteration statements:
1. for statement.
2. while statement.
3. do – while statement.
In general, loops are classified as:
1. Counter – controlled loops: It is a form of looping in which the number of iterations to be
performed is known in advance.
2. Sentinel – controlled loops: Here the number of times the iteration is to be performed is not
known in advance.
for loop
A for loop is a repetition/ iteration control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
While loop
A while loop in C programming repeatedly executes a target statement as long as a given condition is
true.
Syntax:
while(condition)
{
Statement(S);
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately following the
loop.
Flow Diagram
Write a C program to find factors of a given number
#include<stdio.h>
void main()
{
int n,i=1;
printf("enter the number\n");
scanf("%d",&n);
printf("The factors are\n");
while(i<=n)
{
if(n%i==0)
{
printf("%d\t",i);
}
i++;
}
}
do – while
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact is guaranteed to execute at least one
time.
Syntax:
do
{
Statement(S);
} while(condition);
The conditional expression appears at the end of the loop, so the statement(s) in the loop
executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop
executes again. This process repeats until the given condition becomes false.
Flow Diagram
Write a C program to calculate average of numbers entered by the user using do-while.
#include<stdio.h>
void main()
{
int i=1,n,sum=0;
float avg;
printf("enter the number\n");
scanf("%d",&n);
do
{
sum=sum+i;
i++;
} while(i<=n);
avg=(float)sum/n;
printf("Average=%f\n",avg);
}