SlideShare a Scribd company logo
Control Structures (Part 2)
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
10
1
Outline
 The do-while Statement
 The for Statement
 Difference between for, while & do-while
 The switch Statement
 The break Statement
 The continue Statement
The do-while Statement
 When a loop is constructed using the while statement ,
the test for continuation of the loop is carried out at the
beginning of each pass.
 Sometimes, it is desirable to have a loop with the test for
continuation at the end of each pass.
 The general form of the do-while statement is
do statement while (expression) ;
Here, the statement will be executed repeatedly, as long as the
value of expression is true.
The do-while Statement: the Key Point
 For do – while statement, we have to remember that
the statement will always be executed at least once,
since the test for repetition does not occur until the
end of the first pass through the loop.
 For many applications it is more natural to test for
continuation of a loop at the beginning rather than at
the end of the loop. For this reason, the do-while
statement is used less frequently than the while
statement.
The do-while Statement
The do-while Statement: Example 1
// Program to display the consecutive digits 0,1,2,…,9
main()
{
int digit = 0;
do {
printf(“%dn”, digit++);
} while (digit <= 9 );
}
The do-while Statement: Example 2
// Program to find the average of a list of numbers
main()
{
int n, count = 1;
float x, average, sum = 0;
printf(“How many numbers: ”);
scanf(“%d”, &n);
do {
printf(“x = ”);
scanf(“%f”, &x);
sum +=x;
++count;
} while (count <= n );
average = sum / n;
printf(“nThe average is: %fn”, average);
}
The for Statement
 The for statement is the most commonly used looping
statement in C.
 This statement includes an expression that specifies an initial
value for an index, another expression that determines
whether or not the loop is continued, and a third expression
that allows the index to be modified at the end of each pass.
for (expression 1; expression 2; expression 3)
statement
Where, expression 1 is used to initialize some parameter that controls the
looping action, expression 2 represents a condition that
The for Statement (cont..)
 The general form of for statement is:
for (expression 1; expression 2; expression 3)
statement
Where, expression 1 is used to initialize some parameter that
controls the looping action, expression 2 represents a condition
that must be true for the loop to continue execution, and
expression 3 is used to alter the value of the parameter initially
assigned by expression 1.
The for Statement (cont..)
The for Statement to the while statement
In for Statement
for (expression 1; expression 2; expression 3)
statement
Conversion into while statement
expression 1;
while(expression 2)
statement
expression 3 ;
The for Statement (Example)
 Example: Suppose we want to display the consecutive
digits 0,1,2,…..,9, with one digit on each line.This can
be accomplished with the following program.
Solution:
●
main()
●
{
●
int digit;
●
for(digit = 0; digit<=9; ++digit)
●
printf(“%dn”,digit);
●
}
The for Statement (More Examples)
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
for (count = 1;count <= num; count++) {
sum += count;
}
printf("Sum = %d", sum);
}
The for Statement (More Examples)
// Program to calculate the average of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
main()
{
int num, count, sum = 0;
float average;
printf("Enter a positive integer: ");
scanf("%d", &num);
for (count = 1;count <= num; count++)
sum += count;
average = sum/num;
printf(“Average = %f", average);
}
Difference b/n for, while & do-while
No For loop While loop Do while loop
1.
Syntax: for(initialization;
condition;updating),
{ Statements; }
Syntax:
while(condition), { .
Statements; . }
Syntax: do { Statements;
} while(condition);
2.
It is known as entry
controlled loop
It is known as entry
controlled loop.
It is known as exit
controlled loop.
3.
If the condition is not
true first time than
control will never enter in
a loop
If the condition is not
true first time than
control will never
enter in a loop.
Even if the condition is
not true for the first time
the control will enter in a
loop.
4.
There is no semicolon;
after the condition in the
syntax of the for loop.
There is no
semicolon; after the
condition in the
syntax of the while
loop.
There is semicolon; after
the condition in the
syntax of the do while
loop.
5.
Initialization and
updating is the part of
the syntax.
Initialization and
updating is not the
part of the syntax.
Initialization and
updating is not the part
of the syntax
The switch Statement
 The switch statement causes a particular group of
statements to be chosen from several available groups.
 The selection is based upon the current value of an
expression which is included within the switch statement.
 The general form of switch statement is
switch (expression) statement
 Where, expression results in an integer value. Note that
expression may also be of type char, since individual
characters have equivalent integer values.
The switch Statement
The switch Statement (Example 1)
//A simple switch program where choice is char type
switch(choice = getchar()) {
case ‘r’:
case ‘R’:
printf(“Red”);
break;
case ‘w’:
case ‘W’:
printf(“White”);
break;
case ‘b’:
case ‘B’:
printf(“Black”);
}
The switch Statement (Example 3)
//A simple switch program where flag is int. x and y are floating-point.
switch(flag) {
case -1:
y = abs(x);
break;
case 0:
case 1:
y = sqrt(x);
break;
default:
y = 0;
}
The break Statement
 The break statement is used to terminate loops or
to exit from a switch.
 It can only be used within a for, while, do-while or
switch statement.
 The break statement is written simply as
break;
 Without any embedded expression or statement.
The break Statement
The break Statement (example)
//scan positive natural number between 1 to 100
scanf(“%f”, &x);
while(x <= 100){
if(x < 0){
printf(“Error- negative number”);
break;
}
scanf(“%f”,&x);
}
The continue Statement
 The continue statement is used to bypass the remainder of
the current pass through a loop.
 The loop does not terminate when a continue statement is
encountered.
 Rather, the remaining loop statements are skipped and
the computation proceeds directly to the next pass
through the loop.
 The continue statement can be included within a while, do-
while and a for statement
continue;
The continue Statement
The continue Statement
//print 0 to 8 except 4.
for (int j=0; j<=8; j++)
{
if (j==4)
{
continue;
}
printf("%d ", j);
}
Lecture 10 - Control Structures 2
Ad

More Related Content

What's hot (20)

Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
C if else
C if elseC if else
C if else
Ritwik Das
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
Samsil Arefin
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
MomenMostafa
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
MomenMostafa
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
Dr. SURBHI SAROHA
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
Dr. SURBHI SAROHA
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
Vishvesh Jasani
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
Python Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and FunctionsPython Unit 3 - Control Flow and Functions
Python Unit 3 - Control Flow and Functions
DhivyaSubramaniyam
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
Samsil Arefin
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
tanmaymodi4
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
MomenMostafa
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
MomenMostafa
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
Vishvesh Jasani
 

Similar to Lecture 10 - Control Structures 2 (20)

Session 3
Session 3Session 3
Session 3
Shailendra Mathur
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdfModule 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
patcha535
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
baabtra.com - No. 1 supplier of quality freshers
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
Prof Ansari
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
DEEPAK948083
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
Indraprastha Institute of Information Technology
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Dr. Chandrakant Divate
 
Module 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdfModule 2 - Control Structures c programming.pptm.pdf
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
patcha535
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 
Slide07 repetitions
Slide07 repetitionsSlide07 repetitions
Slide07 repetitions
altwirqi
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
chintupro9
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
Tanmay Modi
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
DEEPAK948083
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
Shipra Swati
 
Ad

More from Md. Imran Hossain Showrov (11)

Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
Md. Imran Hossain Showrov
 
Lecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header FileLecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header File
Md. Imran Hossain Showrov
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
Md. Imran Hossain Showrov
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
Md. Imran Hossain Showrov
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
Md. Imran Hossain Showrov
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
Md. Imran Hossain Showrov
 
Lecture 4- Computer Software and Languages
Lecture 4- Computer Software and LanguagesLecture 4- Computer Software and Languages
Lecture 4- Computer Software and Languages
Md. Imran Hossain Showrov
 
Lecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devicesLecture 3 - Processors, Memory and I/O devices
Lecture 3 - Processors, Memory and I/O devices
Md. Imran Hossain Showrov
 
Lecture 2 - Introductory Concepts
Lecture 2 - Introductory ConceptsLecture 2 - Introductory Concepts
Lecture 2 - Introductory Concepts
Md. Imran Hossain Showrov
 
Lecture 1- History of C Programming
Lecture 1- History of C Programming Lecture 1- History of C Programming
Lecture 1- History of C Programming
Md. Imran Hossain Showrov
 
Ad

Recently uploaded (20)

UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 

Lecture 10 - Control Structures 2

  • 1. Control Structures (Part 2) Md. Imran Hossain Showrov (showrovsworld@gmail.com) 10 1
  • 2. Outline  The do-while Statement  The for Statement  Difference between for, while & do-while  The switch Statement  The break Statement  The continue Statement
  • 3. The do-while Statement  When a loop is constructed using the while statement , the test for continuation of the loop is carried out at the beginning of each pass.  Sometimes, it is desirable to have a loop with the test for continuation at the end of each pass.  The general form of the do-while statement is do statement while (expression) ; Here, the statement will be executed repeatedly, as long as the value of expression is true.
  • 4. The do-while Statement: the Key Point  For do – while statement, we have to remember that the statement will always be executed at least once, since the test for repetition does not occur until the end of the first pass through the loop.  For many applications it is more natural to test for continuation of a loop at the beginning rather than at the end of the loop. For this reason, the do-while statement is used less frequently than the while statement.
  • 6. The do-while Statement: Example 1 // Program to display the consecutive digits 0,1,2,…,9 main() { int digit = 0; do { printf(“%dn”, digit++); } while (digit <= 9 ); }
  • 7. The do-while Statement: Example 2 // Program to find the average of a list of numbers main() { int n, count = 1; float x, average, sum = 0; printf(“How many numbers: ”); scanf(“%d”, &n); do { printf(“x = ”); scanf(“%f”, &x); sum +=x; ++count; } while (count <= n ); average = sum / n; printf(“nThe average is: %fn”, average); }
  • 8. The for Statement  The for statement is the most commonly used looping statement in C.  This statement includes an expression that specifies an initial value for an index, another expression that determines whether or not the loop is continued, and a third expression that allows the index to be modified at the end of each pass. for (expression 1; expression 2; expression 3) statement Where, expression 1 is used to initialize some parameter that controls the looping action, expression 2 represents a condition that
  • 9. The for Statement (cont..)  The general form of for statement is: for (expression 1; expression 2; expression 3) statement Where, expression 1 is used to initialize some parameter that controls the looping action, expression 2 represents a condition that must be true for the loop to continue execution, and expression 3 is used to alter the value of the parameter initially assigned by expression 1.
  • 10. The for Statement (cont..)
  • 11. The for Statement to the while statement In for Statement for (expression 1; expression 2; expression 3) statement Conversion into while statement expression 1; while(expression 2) statement expression 3 ;
  • 12. The for Statement (Example)  Example: Suppose we want to display the consecutive digits 0,1,2,…..,9, with one digit on each line.This can be accomplished with the following program. Solution: ● main() ● { ● int digit; ● for(digit = 0; digit<=9; ++digit) ● printf(“%dn”,digit); ● }
  • 13. The for Statement (More Examples) // Program to calculate the sum of first n natural numbers // Positive integers 1,2,3...n are known as natural numbers main() { int num, count, sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); for (count = 1;count <= num; count++) { sum += count; } printf("Sum = %d", sum); }
  • 14. The for Statement (More Examples) // Program to calculate the average of first n natural numbers // Positive integers 1,2,3...n are known as natural numbers main() { int num, count, sum = 0; float average; printf("Enter a positive integer: "); scanf("%d", &num); for (count = 1;count <= num; count++) sum += count; average = sum/num; printf(“Average = %f", average); }
  • 15. Difference b/n for, while & do-while No For loop While loop Do while loop 1. Syntax: for(initialization; condition;updating), { Statements; } Syntax: while(condition), { . Statements; . } Syntax: do { Statements; } while(condition); 2. It is known as entry controlled loop It is known as entry controlled loop. It is known as exit controlled loop. 3. If the condition is not true first time than control will never enter in a loop If the condition is not true first time than control will never enter in a loop. Even if the condition is not true for the first time the control will enter in a loop. 4. There is no semicolon; after the condition in the syntax of the for loop. There is no semicolon; after the condition in the syntax of the while loop. There is semicolon; after the condition in the syntax of the do while loop. 5. Initialization and updating is the part of the syntax. Initialization and updating is not the part of the syntax. Initialization and updating is not the part of the syntax
  • 16. The switch Statement  The switch statement causes a particular group of statements to be chosen from several available groups.  The selection is based upon the current value of an expression which is included within the switch statement.  The general form of switch statement is switch (expression) statement  Where, expression results in an integer value. Note that expression may also be of type char, since individual characters have equivalent integer values.
  • 18. The switch Statement (Example 1) //A simple switch program where choice is char type switch(choice = getchar()) { case ‘r’: case ‘R’: printf(“Red”); break; case ‘w’: case ‘W’: printf(“White”); break; case ‘b’: case ‘B’: printf(“Black”); }
  • 19. The switch Statement (Example 3) //A simple switch program where flag is int. x and y are floating-point. switch(flag) { case -1: y = abs(x); break; case 0: case 1: y = sqrt(x); break; default: y = 0; }
  • 20. The break Statement  The break statement is used to terminate loops or to exit from a switch.  It can only be used within a for, while, do-while or switch statement.  The break statement is written simply as break;  Without any embedded expression or statement.
  • 22. The break Statement (example) //scan positive natural number between 1 to 100 scanf(“%f”, &x); while(x <= 100){ if(x < 0){ printf(“Error- negative number”); break; } scanf(“%f”,&x); }
  • 23. The continue Statement  The continue statement is used to bypass the remainder of the current pass through a loop.  The loop does not terminate when a continue statement is encountered.  Rather, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop.  The continue statement can be included within a while, do- while and a for statement continue;
  • 25. The continue Statement //print 0 to 8 except 4. for (int j=0; j<=8; j++) { if (j==4) { continue; } printf("%d ", j); }
  翻译: