Professional Documents
Culture Documents
BRANCHING
What are Control flow statements?
1. Conditional Statement
Definition: The statements that transfers the control from one part of the program to another part of the
program based on a condition is called as Conditional statements.
There are 5 Conditional statements in C
Statement an;
if(condition)
{ if False
condition
Statement t1;
…………..
Statement tn; True
} Statement t1;
…………….
Statement b1; Statement tn;
……..
Statement bn;
Statement b1;
……………
Statement bn;
Explanation
The keyword if must be followed by an expression and expression must be enclosed within parentheses.
First statement a1 to an is executed sequentially(one after another).
The if statement is executed next. The expression inside the parentheses is evaluated. i.e Condition is
checked, which results in either TRUE or FALSE .
If condition is TRUE the statements t1 to tn are executed and then statements b1 to bn are executed.
If condition is FALSE the statements t1 to tn are skipped and then statements b1 to bn are executed.
Advantage of if-statement
Output of if-statement is true or false.
if-statement is used as one way decision/selection statement.
Disadvantage
If one action has to be performed when the condition is true and another action has to be performed
when the condition is false then if-statement is not recommended
This disadvantage is overcome using two- way decision/selection statement called “ if-else statement”
An Example which illustrates if statement: To print given no is an even no.
Algorithm Flowchart Program
Note: Similarly write Algorithm, Flowchart and C Program for the following
i) To print given no is a odd no. Logic: if (n!=0)
ii) To print given no is a positive no. Logic: if(n>0)
iii) To print given no is a negative no.Logic: if(n<0)
The if – else statement (two way selection).
It is used to execute a set of statements if the condition is true, and another set of statements if the
condition is false. Hence it is called two way selection.
Statement an;
if(condition)
{
if False
Statement t1; condition
………….. True
Statement tn;
} Statement t1; Statement E1;
……………. …………….
else
Statement tn; Statement En;
{ Statement E1;
………….. A
Statement En;
} Statement b1;
Statement b1; ……………
Explanation
The keyword if must be followed by an expression and expression must be enclosed within parentheses.
First statement a1 to an is executed sequentially(one after another).
The if statement is executed next. The expression inside the parentheses is evaluated. i.e Condition is
checked, which results in either TRUE or FALSE .
If condition is TRUE the statements t1 to tn are executed and else block is skipped then statements b1 to
bn are executed.
If condition is FALSE the statements if block is skipped and then statements E1 to En are executed and
then statements b1 to bn are executed.
An Example which illustrates if-else statement: To print given no is an even no or odd no.
Algorithm Flowchart Program
Note: Similarly write Algorithm, Flowchart and C Program for the following
i) To check given integer no is a positive no or negative no Logic: if(n>0) its positive else negative.
ii) To find largest of 2 no’s Logic: Let a and b be 2 no’s if(a>b) a is greater else b is greater
Statement a1;
…….. Statement a1;
Statement an; ……………
if(condition 1) Statement an;
{
if (condition 2)
block 1;
False
else If condition
block 2; 1
} True
else
{ If condition False
block 3; 2
} True
Block 2
Statement b1; Block 3
Block 1
……..
Statement bn;
Statement b1;
……………
Statement bn;
Explanation
The keyword if and else must be followed by an expression and expression must be enclosed within
parentheses.
First statement a1 to an is executed sequentially.
First statement1 is executed followed by statement2.
Condition 1 is checked
if condition 1 is true control goes to Statement t1 to tn are executed and then statement b1 to bn
are executed.
If condition 1 is false Condition 2 is checked
if condition 2 is true control goes to Statement e1 to en are executed and then statement b1 to
bn are executed.
If condition 2 is false condition 3 is checked
If condition 3 is true control goes to Statement f1 to fn and are executed ,then followed by
statement b1 to bn.
if condition 3 is false Statement g1 to statement gn are executed and then statement b1 to bn are
executed.
No condition is specified for the else block
Syntax And Flowchart
Statement a1;
………….. Statement a1;
Statement an; …………..
Statement an;
if(condition 1)
{
Statement t1; True
……………. If condition
Statement tn; 1
Block 1 False
}
else if(condition 2) If condition False
{ 2
Statement e1; True
…………..
Block 2 If condition
Statement en; 3
} true false
else if(condition 3) Block 3 Block 4
{
Statement f1;
…………..
Statement fn; A
}
else
{ Statement b1;
Statement g1; ……………
……………
Statement bn;
Statement gn;
}
Block 1: Statement t1 …….. statement tn
Statement a1;
……………. Statement a1;
Statement an; …………….
Statement an;
switch(choice)
{
Choice/Expression
case value1: Block1;
break;
case 1 case 2 case 3 …..……
case n default
case value2: Block 2;
break;
case value3: Block3; Block 1 Block 2 Block 3 ……. Block n Block d
break;
default: Block d;
A
}
Statement b1;
…………….
Statement bn;
Statement b1;
…………….
Statement bn;
Explanation
The switch statement is a multi-way decision that tests whether an expression matches one of a number
of constant integer values, and branches accordingly.
Each case is labeled by one or more integer-valued constants or constant expressions.
If a case matches the expression value, execution starts at that case.
All case expressions must be different.
The case labeled default is executed if none of the other cases are satisfied.
A default is optional; if it isn't there and if none of the cases match, no action takes place. Cases and the
default clause can occur in any order.
The break statement causes an immediate exit from the switch.
More about switch statement:
The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int,
long) or a char.
The expression cannot be a Boolean value or a floating point value (float or double)
No two case labels can have the same constant value.
An Example which illustrates switch control statement:
Program to simulate a simple calculator that performs arithmetic operations only on integers. Error message
should be reported if any attempt is made to divide by 0.
Algorithm Program
Advantages:
1. Improves readability of a program.
2. More Structured way of writing program.
Disadvantages:
1. Used only if the expressions used for checking results in integer value.
2. Cannot be used when a decision is based on range of values.
label statement : The label is a valid ‘C’ identifier followed by a colon. we can precode any statement by a
label in the form
Label : statement ;
This statement immediately transfers execution to the statement labeled with the label identifier.
Examples:
1. Program to print INDIA 5 times.
Disadvantages:
1. Using goto the code is difficult to read and understand.
2. Usage of goto results in unstructured programming i.e it is not a good programming style.
The break statement in C programming language has the following two usages:
• When the break statement is encountered inside a loop, the loop is immediately terminated and program
control resumes at the next statement following the loop.
• It can be used to terminate a case in the switch statement.
• If you are using nested loops (i.e., one loop inside another loop), the break statement will stop the
execution of the innermost loop and start executing the next line of code after the block.
•
switch (option) for(… .......... )
{ {
case 'A': ……………….
aCount++; if(………….)
break; {
case 'B': ……
bCount++; break;
break; ……………..
case 'C': …………..
cCount++; }
break; ………….
} …………..
}
Example: Refer switch statement example program Example:
#include<stdio.h>
void main()
{
int i,n=5;
for(i=0;i<=n;i++)
{
if(i==3)
break;
printf(“%d\n”,i);
}
}
Output: 1 2
The continue statement can be placed only in the body of a for loop, a while loop, or a do...while loop.
The continue statement is used only in the loops to terminate the current iteration and continue with
remaining iterations.
In the while statement and do-while statement, whenever a continue statement is executed, the rest of
the statements within the body of the loop are skipped and the conditional expression in the loop is
executed.
But, when continue is executed in the for loop ,control is transferred to expression 3.
The pictorial representation is shown below:
Example:
LOOPING
Looping constructs: Repetition (Looping constructs)
A set of statements may have to be executed for a specified number of times or untill the condition is
satisfied(true) are called Looping Constructs.
The statements that help us to execute a set of statements repeatedly are called Looping Constructs.
It is implemented using the following statements.
for
while
do-while
1. The while loop
Statement c1;
true }
Statements c2;
Statement b1;
Statement b2; Output:
updation; INDIA
INDIA
INDIA
Statement c1;
INDIA
Statements c2;
INDIA
Definition: A while loop is a looping statement which are used to execute a set of statements
repeatedly.
Working:
Statements a1 to an are executed in sequence.
Condition is checked ,if it is true ,body of while loop, ie statements b1 to bn are executed.
Thus the body of loop is repeatedly executed as long as the condition is true.
Once the condition/expression is evaluated to false ,the control comes out of loop and statements
c1 to c n are executed.
It is a pre testing loop and top testing loop and entry controlled loop.
2. The do while loop
Statement a 1; #include<stdio.h>
Statement a 1; void main( )
Statement a2;
{
Statement a 2;
initialization; int i;
i=1;
do
do
{ initialization {
Statement b1; printf(“INDIA\n”);
Statement b2; i++;
Statement b1;
updation; } while(i<=5);
Statement b2;
} while(Condition check); }
updation;
Statement c 1;
Statements c 2;
Output:
true
INDIA
Condition
check INDIA
INDIA
INDIA
false
INDIA
Statement c 1;
Statements c 2;
Definition: A do while loop is a looping statement which are used to execute a set of statements
repeatedly.
Since a set of statements have to be executed until a condition is true, it is also called condition
controlled loop
The body of the loop is executed first and then the expression is evaluated to true or false. hence it is
called as post test loop/bottom testing loop
As the expression is evaluated at the end ,the do while loop is also called as exit controlled loop.
Syntax:
do and while are keywords.(also called reserved words).
Working:
Statements a1 to an are executed in sequence.
First Statements inside the body of the loop is executed one after the other.
Then the expression/condition is evaluated ,if it is true, body of the do while loop is executed.
Thus the body of loop is repeatedly executed as long as the condition is true.
Once the condition/expression is evaluated to false ,the control comes out of loop and statements
c1 to cn are executed.
The moment expression is evaluated to false, the control comes out of the do while loop.
The body of the loop is executed at least once before the condition is checked , hence it is
also called as post test loop .
Statements C2; }
Statement B1;
Statement B2;
Output:
INDIA
INDIA
Statement C1; INDIA
Statements C2; INDIA
INDIA
Definition
A for loop is a control statement using which the programmer can give instructions to the computer to
execute a set of statements repeatedly for a specified number of times.
Explanation:
First (exp1) is Initialization of the loop .
Next (exp2) condition is checked. If condition is true then the control enters the body of the loop and
statements b1 to b2 are executed.
After executing statements b1 to bn , control goes back and expression 3 (updation) is evaluated.
(exp3)updation of the loop is performed after which again the control goes back to the condition check,
if condition is true, the body is executed again.
This process repeats as long as the condition evaluates to be true.
Once the condition evaluates to be false, the control comes outside the body of the loop.
Syntax Syntax