際際滷

際際滷Share a Scribd company logo
Control Statement in C
Lecture : 7
Md. Al-Imran Roton
Lecturer, CSEDIU
Bangladesh
CONTROL STATEMENT
All the statements written in C program executes
from top to bottom one by one
CONTROL STATEMENT
There are some situations where one
may have to
Change the order of execution based on
certain conditions
OR
Repeat a group of statements until certain
conditions are satisfied.
TYPES OF CONTROL STATEMENT
IF SELECTION STRUCTURES
1) A simple if structure is called a single-selection
structure because it either selects or ignores a
single action.
IF/ELSE SELECTION
STRUCTURESThe if / else structure is called a double-
selection structure because it selects
between two different actions.
IF/ELSE SELECTION
STRUCTURES
Diagram of IF and Else
REAL LIFE
EXAMPLENeed to make a decision to go which way
Real life example
If(girl==educated&&beautiful&&smart)
{
then, I agree to marry her.
}
Else
{
I dont agree to marry her.
}
START
Money Received 15000
Received
Money>=10000
?
Holiday Enjoyed
END
Condition not Satisfied
[False] Condition Satisfied
[True]
Holiday Trip Problem
Example code
Nested IF / ELSE
Lecturer : 8
Nested if condition
Real life example
 If(girl==educated)
 {
 If(girl==beautiful)
 {
 If(girl==smart)
 I agree to marry her.
 Else
 Think about consideration.

 }
 Else
 Talk with parents.
 }
 Else
 Rejected .
Elseif condition
Elseif condition
 stucture
Example
Switch statement
Syntax
switch(expression)
{
case value-1:
block-1;
break;
case value-2:
block-2;
break;
..
..
default
block;
break;
}
Statement - X
Switch statement
Switch statement
Example
Example
PRACTICE EXAMPLE
Big Bazzar gives festival discount on purchase of
their products in the following percentages
i.If purchase amount < 1000 than 5% discount
ii.If purchase amount >=1000 than but <3000 then
10% discount
iii.If purchase amount >=3000 but <5000 then 12%
discount
iv.If purchase amount > 5000 then 15% discount
v.Make an university grading system.
Thank you
JUMPING
Lecturer : 9
Break Statement
 It is an jump instruction and can be
used inside the switch and loop
statements.
 The execution of the break statements
causes the control transfer to the
statement immediately after the loop.
Break Statement
Break Statement
Break Statement
Continue Statement
 It is a jump statement.
 It is used only inside the loop.
 Its execution does not exit from the loop but
escape the loop for that iteration and
transfer the control back to the loop for the
new iteration.
Continue Statement
Goto Statement
 It is used the alter the normal sequence of flow by
transferring the control to some other part of the
program unconditionally.
 It is followed by the label statement which
determine the instruction to be handled next after
the execution of the goto statement.
Goto Statement
Goto Statement
Example
Thank You
Repetition/Looping
Lecture : 10
Why Looping
HA HA
HA HA
HA HA
HA HA
HA HA
HA HA
Why Looping
 Printf(HA HA );
 Printf(HA HA );
 Printf(HA HA );
 Printf(HA HA );
 Printf(HA HA );
 Printf(HA HA );
 Need to write 6 time printf() function. (problem)
 Solution : Repitition/Looping.
Why looping
Types of Looping
 While
 Do while
 For
While loop
While loop
Infinity While loop
 while(1)
 {
 statements;
 ........
 }
 Question: How it will stop?
Infinity While loop
 Solution : Use break to exit from the loop
while(1)
{
statements;
........
if(condition)
break;
}
Example
Do while loop
 Difference from while:
Do while loop
Do while loop
Example
Thank you
FOR LOOP
Lecture:11
Syntax
 There are 3 parts in a for loop.
 1. Initialization.
 2. Condition.
 3. Increment / Decrement.
Flowchart For loop
Flowchart For loop
Diagram of for loop
Diagram of for loop
Infinity for loop
 for( ; ; )
 {
 statements;
 ........
 }
 Same question How to stop the loop.
 Answer is also same use break.
Example
 Display 1 to 10.
Example
 Sum 1 to 10.
Example
 Sum 1 to n.
Thank you
NESTED FOR LOOP
Lecture: 12
What is nested loop
 There exits multiple loop like 2,3,4.so on.
 1. Outer loop.
 2. Inner loop.
Outer/Inner loop
Diagram of nested for loop
Example
Example
Example
Example
Some Interesting practice Example
Some Interesting practice Example
Some Interesting practice Example
Some Interesting practice Example
Some Interesting practice Example
Thank you

More Related Content

Controlstatment in c