SlideShare a Scribd company logo
CONTROL
STRUCTURES
Chapter 43/8/2016
1
By:-Gourav Kottawar
contents
3/8/2016
2
 4.1 Introduction
 4.2 Selection Statements
If, Nested if, if….else, else if Ladder
ternary operator, switch, Nested switch, conditional
expression
 4.3 Iterative Statements
while loop, do-while loop, for loop
 4.4 Jump Statements
Goto & label, break & continue,
exit() function
 4.5 Compound Statements
 4.6 Null Statements
4.3 Iterative Statements
3/8/2016
3
 Looping
 In looping, a sequence of statements are
executed until some condition is satisfied
which is placed for termination of the loop
 A program loop consists of two segments
a. body of the loop
b. the control statement
 The control is tested always for execution of
the body of the loop
4.3 Iterative Statements
3/8/2016
4
 How loop works
3/8/2016
5
 Loops may classified as
entry-controlled loop
exit-controlled loop
Entry controlled loop
 The control conditions
are tested before the
start of the loop
execution.
 If the conditions are
not satisfied , then the
body of the loop will
not be executed.
 Entry controlled loop
is also known as pre-
test loop.
 Example: – While
Loop
for Loop 3/8/2016
6
Exit Controlled loop
 he Test is performed
at the end of the body
of the loop and there
fore the body is
executed
unconditionally for the
first time.
 Exit controlled loop is
also known as post-
test loop.
 Ex- ->Do-While
3/8/2016
7
Types of loop
3/8/2016
8
while loop
3/8/2016
9
while loop
3/8/2016
10
 Example
1.Program to print first 10 numbers
2. Program to add first 10 numbers
3. Program to add first 10 even numbers
4. Program to add first 10 odd numbers
for loop
3/8/2016
11
for loop
3/8/2016
12
 Examples
1.WAP to accept number and check whether it is
prime or NOT.
2. WAP to accept number and check whether it
is Armstrong or NOT.
for loop
3/8/2016
13
 Nested for loop
for loop
3/8/2016
14
 Example
1.WAP to half print pyramid of numbers
2. Print pattern
* * *
* *
*
do while loop
3/8/2016
15
do while loop
3/8/2016
16
 Example
1. Program to print first ten multiples of 5.
Sentinel loops
3/8/2016
17
 Loops may be classified into two categories
1. Counter controlled loops
2. Sentinel controlled loops
1. Counter controlled loops -
- we know in advance how many times the loop
will be executed.
- use control variable known as counter.
- counter must be initialized, tested and updated
properly
- also known as repetition loop
3/8/2016
18
2. Sentinel controlled loop
- a special value is used to change the loop
control expression from true to false.
- the control variable is called as sentinel
variable.
- also called as indefinite repetition loop
because of number of repetition loop because
the number of repetitions is not known before
the loop begins executing
Selecting a loop
3/8/2016
19
 Analyze the problem and see whether it required
a pre – test or post – test.
 If it requires a post – test loop, then we can use
only one loop, do while
 If it requires a pre – test loop, then we have two
choices : for and while loop
 Decide whether the loop termination requires
counter based control or sentinel – based control.
 Use for loop if the counter based control is
necessary
 Use while loop if the sentinel based control is
required.
Jumps in loop
3/8/2016
20
 Sometimes when executing loop it becomes
desirable to skip a part of the loop or to leave
the loop as soon as certain condition occurs.
 C permits jumps from one statement to
another within a loop as well as a jump out of
loop.
Jumping out of loop
3/8/2016
21
while (------)
{
-----
----
if (condition)
break;
-----
-----
}
-----
Exit
Fro
m
loop
3/8/2016
22
do
{
---
---
if(condition)
break;
----
----
} while(-----);
-----
Exit
Fro
m
loop
3/8/2016
23
for (----)
{
---------
---------
if (error)
break;
-------
-------
}
--------
Exit
Fro
m
loop
3/8/2016
24
for( -------)
{
--------
for(-------)
{
------
if(condition)
break;
------
}
--------
}
Exit
Fro
m
inne
r
loop
The goto statement
3/8/2016
25
 To branch unconditionally from one point to
another in the program
 goto requires a label to identify the palce
where the branch is to be made.
label is variable name followed by colon
label is placed immediately before the
statement where the control is to be
transferred .
goto statement
goto label;
---------
--------
--------
label :
Staement;
forword jump
label :
Statement;
-------
---------
-------
goto label;
backward jump
3/8/2016
26
3/8/2016
27
 Example
Jumping within and exiting from the
loops with goto statement
while(--------)
{
if(error)
goto stop;
-------
if (condition)
goto abc;
-------
-------
abc:
}
Stop:
------
------
for(-------)
{
------
for( -------)
{
------
if(error)
goto error;
------
}
-------
}
error:
---------
--------
3/8/2016
28
Exit
Fro
m
loop
Jump
Within
loop
Exit
Fro
m
two
loop
s
3/8/2016
29
Note –
Avoid goto – because
1. Many compilers may generate less efficient
code.
2. Makes program logic complecated
3. Renders program unreadable

More Related Content

PPTX
Presentation on Loop(C Language) by Dheemaan Daash
PDF
Chapter 9 - Loops in C++
PPTX
Basic for Loop in C
PPTX
Loops Basics
PPTX
Loop(for, while, do while) condition Presentation
PPT
Looping statements in Java
PPTX
Looping (Computer programming and utilization)
PPTX
Comp ppt (1)
Presentation on Loop(C Language) by Dheemaan Daash
Chapter 9 - Loops in C++
Basic for Loop in C
Loops Basics
Loop(for, while, do while) condition Presentation
Looping statements in Java
Looping (Computer programming and utilization)
Comp ppt (1)

Viewers also liked (9)

PPT
Control Structure in C
PPT
Control structure
PPT
Introduction to c programming
DOCX
FUNCIONES EN LENGUAJE C
PPT
Control Structures
PPSX
Control Structures in Visual Basic
DOCX
Librerias en-lenguaje-c
PPT
Funciones en C
Control Structure in C
Control structure
Introduction to c programming
FUNCIONES EN LENGUAJE C
Control Structures
Control Structures in Visual Basic
Librerias en-lenguaje-c
Funciones en C
Ad

Similar to control structures in c if switch for (20)

PPTX
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
PDF
Chapter 3 - Flow of Control Part II.pdf
PPT
Decision making and looping
PDF
Unit II chapter 4 Loops in C
PPTX
Loop (Computer programming and utilization)
PPT
control-statements....ppt - definition
DOCX
loops and iteration.docx
PDF
UNIT 2 PPT.pdf
PPTX
PPTX
Looping Statement And Flow Chart
PPTX
presentation on powerpoint template.pptx
PPTX
C Programming: Looping Statements in C Pgm
PPTX
Loops In C++
PPTX
What is loops? What is For loop?
PDF
forloop-170915182653.pdf
PPTX
Control Statement IN C.pptx
PPTX
Types of loops in c language
PPTX
Computer programming 2 Lesson 8
PPTX
prt123.pptx
LOOPING STATEMENTS, JAVA,PROGRAMMING LOGIC
Chapter 3 - Flow of Control Part II.pdf
Decision making and looping
Unit II chapter 4 Loops in C
Loop (Computer programming and utilization)
control-statements....ppt - definition
loops and iteration.docx
UNIT 2 PPT.pdf
Looping Statement And Flow Chart
presentation on powerpoint template.pptx
C Programming: Looping Statements in C Pgm
Loops In C++
What is loops? What is For loop?
forloop-170915182653.pdf
Control Statement IN C.pptx
Types of loops in c language
Computer programming 2 Lesson 8
prt123.pptx
Ad

More from gourav kottawar (20)

PPTX
operator overloading & type conversion in cpp
PPTX
constructor & destructor in cpp
PPTX
classes & objects in cpp
PPTX
expression in cpp
PPTX
basics of c++
PPT
working file handling in cpp overview
PPT
pointers, virtual functions and polymorphisms in c++ || in cpp
PPTX
exception handling in cpp
PPT
cpp input & output system basics
PPTX
operator overloading & type conversion in cpp over view || c++
PPTX
constructor & destructor in cpp
PPTX
basics of c++
PPTX
classes & objects in cpp overview
PPTX
expression in cpp
PPT
SQL || overview and detailed information about Sql
PPT
SQL querys in detail || Sql query slides
PPT
Rrelational algebra in dbms overview
PPT
overview of database concept
PPT
Relational Model in dbms & sql database
PPTX
DBMS information in detail || Dbms (lab) ppt
operator overloading & type conversion in cpp
constructor & destructor in cpp
classes & objects in cpp
expression in cpp
basics of c++
working file handling in cpp overview
pointers, virtual functions and polymorphisms in c++ || in cpp
exception handling in cpp
cpp input & output system basics
operator overloading & type conversion in cpp over view || c++
constructor & destructor in cpp
basics of c++
classes & objects in cpp overview
expression in cpp
SQL || overview and detailed information about Sql
SQL querys in detail || Sql query slides
Rrelational algebra in dbms overview
overview of database concept
Relational Model in dbms & sql database
DBMS information in detail || Dbms (lab) ppt

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Institutional Correction lecture only . . .
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
2.FourierTransform-ShortQuestionswithAnswers.pdf
Microbial diseases, their pathogenesis and prophylaxis
Institutional Correction lecture only . . .
A systematic review of self-coping strategies used by university students to ...
Chinmaya Tiranga quiz Grand Finale.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
O5-L3 Freight Transport Ops (International) V1.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
01-Introduction-to-Information-Management.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf

control structures in c if switch for