BRANCHING and LOOPING
BRANCHING
What are Control flow statements?
A program is set of instructions.
By default these instructions are sequentially executed.
Definition: The statements that transfers the control from one part of the program to another part of the
program ,with or without any condition is called as branching statements.
Types of Branching statements/Conditional statements/decision making statements/
Control construct
There are 2 types of branching statements present 1.Conditional Statement
2. Unconditional Statement.
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
1. if control construct(simple if)
2. if else control construct
3. nested if else control construct
4. else if ladder or cascaded
5. switch control construct
The if statement (Simple if/ One way selection)
An if statement is a single selection statement.
It is used to execute a set of statements if the condition is true,
If the condition is false, it skips executing those set of statements. Hence it is called one way selection.
, Syntax: Flowchart:
Statement a1;
……………
Statement a1;
Statement an;
……..
Statement an;
if(condition)
if False
{ condition
Statement t1;
…………..
True
Statement tn;
Statement t1;
} …………….
Statement tn;
Statement b1;
……..
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
Algorithm: Check even number #include<stdio.h>
Input: A number void main()
Output: Even Number {
Step 1: Start int n;
Step 2: [input the number] printf(“ Enter the number\n”)
Read N scanf(“%d”,&n); if(n
Step 3: [compute even number] %2==0)
if n%2 is equals to zero {
Goto step 4 printf(“Even no”);
Otherwise goto step 5 }
Step 4: [display output]
Print “even no” }
Step 5: Stop
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)
BRANCHING
What are Control flow statements?
A program is set of instructions.
By default these instructions are sequentially executed.
Definition: The statements that transfers the control from one part of the program to another part of the
program ,with or without any condition is called as branching statements.
Types of Branching statements/Conditional statements/decision making statements/
Control construct
There are 2 types of branching statements present 1.Conditional Statement
2. Unconditional Statement.
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
1. if control construct(simple if)
2. if else control construct
3. nested if else control construct
4. else if ladder or cascaded
5. switch control construct
The if statement (Simple if/ One way selection)
An if statement is a single selection statement.
It is used to execute a set of statements if the condition is true,
If the condition is false, it skips executing those set of statements. Hence it is called one way selection.
, Syntax: Flowchart:
Statement a1;
……………
Statement a1;
Statement an;
……..
Statement an;
if(condition)
if False
{ condition
Statement t1;
…………..
True
Statement tn;
Statement t1;
} …………….
Statement tn;
Statement b1;
……..
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
Algorithm: Check even number #include<stdio.h>
Input: A number void main()
Output: Even Number {
Step 1: Start int n;
Step 2: [input the number] printf(“ Enter the number\n”)
Read N scanf(“%d”,&n); if(n
Step 3: [compute even number] %2==0)
if n%2 is equals to zero {
Goto step 4 printf(“Even no”);
Otherwise goto step 5 }
Step 4: [display output]
Print “even no” }
Step 5: Stop
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)