SlideShare a Scribd company logo
Control Structures (Part 1)
Md. Imran Hossain Showrov (showrovsworld@gmail.com)
9
1
Outline
 Control Statements
 Branching:The if-else statement
 The nested if-else statement
 Looping:The while Statement
Control Statements
 Statement that is used to control the flow of
execution in a program is called control structure.
Branching: The if-else statement
 Lets consider a scenario:
We want to find the biggest out of two numbers.This problem
requires comparison of the two numbers and based on the
comparison result, the bigger number will be found.
 We can solve this problem by using if instruction.
if(expression)
{
statement
}
Branching: The if-else statement (cont..)
 Lets solve this problem by using if instruction.
#include<stdio.h>
main()
{
int first, second;
scanf(“%d %d”, &first, &second);
if(first > second)
{
printf(“First number is bigger”);
}
}
Branching: The if-else statement (cont..)
 In the written program, if the input is higher than the second number, only
than the printf statement will be executed.
 If the input is 20 10
 We will see the output
First number is bigger
 If the input is 10 20
 No output message will appear on the screen.
 We can extend the above program
Branching: The if-else statement (cont..)
 Lets extend the previous program:
#include<stdio.h>
main()
{
int first, second;
scanf(“%d %d”, &first, &second);
if(first > second)
printf(“First number is biggern”);
if(second >first)
printf(“Second number is biggern”);
if(first == second)
printf(“Two numbers are equaln”);
}
Branching: The if-else statement (cont..)
 This will give the following results:
 If the input is 20 10
 We will see the output
First number is bigger
 If the input is 10 20
 We will see the output
Second number is bigger
 If the input is 20 10
 We will see the output
Two numbers are equal
Branching: The if-else statement (cont..)
 We can also rewrite the program like this:
#include<stdio.h>
main()
{
int first, second;
scanf(“%d %d”, &first, &second);
if(first > second)
printf(“First number is biggern”);
else if(second >first)
printf(“Second number is biggern”);
else
printf(“Two numbers are equaln”);
}
Branching: The if-else statement (cont..)
 What we can see is:
 The if-else statement can be written as:
if (testExpression1) {
// statement(s)
}
else if(testExpression2){
// statement(s)
}
………
else {
// statement(s)
}
Branching: The if-else statement (cont..)
The if-else statement (More Examples)
 Example 2: Suppose, we want to calculate the
biggest among three numbers. So, how can we
solve this problem?
The if-else statement (More Examples)
#include<stdio.h>
main(){
int a,b,c, biggest;
scanf(“%d %d %d”, &a, &b, &c);
if(a > b && a > c)
biggest = a;
else if(b> c)
biggest = b;
else
biggest = c;
printf(“biggest = %d”, biggest);
}
The nested if-else statement
 It is possible to nest if-else statement, one within another
 There are several different forms that nested if-else statements
can take.
 The most general form of two-later nesting is
if e1 if e2 s1
else s2
else if e3 s3
else s4
The nested if-else statement (Example)
 Recall the previous Example: Suppose, we want
to calculate the biggest among three numbers.
So, how can we solve this problem?
The if-else statement (Example)
#include<stdio.h>
main(){
int a,b,c, biggest;
scanf(“%d %d %d”, &a, &b, &c);
if(a > b)
if(a>c) biggest = a;
else if(b> c)
biggest = b;
else
biggest = c;
printf(“biggest = %d”, biggest);
}
Looping
 We may encounter situations, when a block of code needs to
be executed several number of times.
 In general, statements are executed sequentially:The first
statement in a function is executed first, followed by the
second, and so on.
 A loop statement allows us to execute a statement or group
of statements multiple times.
Looping (cont..)
 The general form of a loop statement in most of the
programming languages −
Looping (cont..)
C programming language provides the following types of loops to
handle looping requirements.
 while loop: Repeats a statement or group of statements
while a given condition is true. It tests the condition before
executing the loop body.
 for loop: Executes a sequence of statements multiple times
and abbreviates the code that manages the loop variable.
 do...while loop: It is more like a while statement, except that
it tests the condition at the end of the loop body.
 nested loops: You can use one or more loops inside any
other while, for, or do..while loop.
Looping: The while Statement
 Suppose we want to display “hello” on output screen five
times in five different lines, we might think of writing either
five printf statements or one printf statement consisting of
constant string “hellon” five times.
 Now, if we want to display “hello” 500 times, what will
happen?
 To solve this, we need some programming facility to repeat
certain works.
The while Statement
 The while statement is used to carry out looping operation in
which a group of statements is executed repeatedly, until some
condition has been satisfied.
 The general form of the while statement is
while (expression) statement
 The statement will be executed repeatedly, as long as the
expression is true.
The while Statement (cont..)
The while Statement (cont..)
 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 = 0;
●
while(digit<=9){
●
printf(“%dn”,digit);
●
++digit;
●
}
The while Statement (cont..)
 Output:
●
0
●
1
●
2
●
3
●
4
●
5
●
6
●
7
●
8
●
9
The while 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 = 1, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
while (count <= num) {
sum += count;
++count;
}
printf("Sum = %d", sum);
}
The while Statement: Infinite Loop
Infinite loop: var will always have value >=5 so the loop would never end.
int main()
{
int var = 6;
while (var >=5)
{
printf("%d", var);
var++;
}
return 0;
}
The while Statement: Infinite Loop
Infinite loop: var value will keep decreasing because of –- operator, hence it will always be
<= 10.
int main()
{
int var =5;
while (var <=10)
{
printf("%d", var);
var--;
}
return 0;
}
Lecture 9- Control Structures 1

More Related Content

What's hot

Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
Md. Imran Hossain Showrov
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
SURBHI SAROHA
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
SURBHI SAROHA
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
narmadhakin
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
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
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
MomenMostafa
 
7 functions
7  functions7  functions
7 functions
MomenMostafa
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
Rumman Ansari
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
Wingston
 
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
 
C if else
C if elseC if else
C if else
Ritwik Das
 
[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
 
[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
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
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
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 

What's hot (20)

Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Conditional and control statement
Conditional and control statementConditional and control statement
Conditional and control statement
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
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
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
7 functions
7  functions7  functions
7 functions
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
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
 
C if else
C if elseC if else
C if else
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
 
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
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 

Similar to Lecture 9- Control Structures 1

computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
eaglesniper008
 
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
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
Suhail Akraam
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
3. control statement
3. control statement3. control statement
3. control statement
Shankar Gangaju
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Python programing
Python programingPython programing
Python programing
hamzagame
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
Tanmay Modi
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
tanmaymodi4
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
PhanMinhLinhAnxM0190
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
RohitSindhu10
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
AkashdeepBhattacharj1
 
Loops
LoopsLoops
Loops
Kamran
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
sana shaikh
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
Sanjjaayyy
 

Similar to Lecture 9- Control Structures 1 (20)

computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
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
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
3. control statement
3. control statement3. control statement
3. control statement
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Python programing
Python programingPython programing
Python programing
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
Loops
LoopsLoops
Loops
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 

More from Md. Imran Hossain Showrov

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 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
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
 

More from Md. Imran Hossain Showrov (12)

Lecture 22 - Error Handling
Lecture 22 - Error HandlingLecture 22 - Error Handling
Lecture 22 - Error Handling
 
Lecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header FileLecture 21 - Preprocessor and Header File
Lecture 21 - Preprocessor and Header File
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
 
Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language Lecture 5 - Structured Programming Language
Lecture 5 - Structured Programming Language
 
Lecture 4- Computer Software and Languages
Lecture 4- Computer Software and LanguagesLecture 4- Computer Software and Languages
Lecture 4- Computer Software and Languages
 
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
 
Lecture 2 - Introductory Concepts
Lecture 2 - Introductory ConceptsLecture 2 - Introductory Concepts
Lecture 2 - Introductory Concepts
 
Lecture 1- History of C Programming
Lecture 1- History of C Programming Lecture 1- History of C Programming
Lecture 1- History of C Programming
 

Recently uploaded

MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
NelTorrente
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
JezreelCabil2
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 

Recently uploaded (20)

MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
MATATAG CURRICULUM: ASSESSING THE READINESS OF ELEM. PUBLIC SCHOOL TEACHERS I...
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Landownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptxLandownership in the Philippines under the Americans-2-pptx.pptx
Landownership in the Philippines under the Americans-2-pptx.pptx
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 

Lecture 9- Control Structures 1

  • 1. Control Structures (Part 1) Md. Imran Hossain Showrov (showrovsworld@gmail.com) 9 1
  • 2. Outline  Control Statements  Branching:The if-else statement  The nested if-else statement  Looping:The while Statement
  • 3. Control Statements  Statement that is used to control the flow of execution in a program is called control structure.
  • 4. Branching: The if-else statement  Lets consider a scenario: We want to find the biggest out of two numbers.This problem requires comparison of the two numbers and based on the comparison result, the bigger number will be found.  We can solve this problem by using if instruction. if(expression) { statement }
  • 5. Branching: The if-else statement (cont..)  Lets solve this problem by using if instruction. #include<stdio.h> main() { int first, second; scanf(“%d %d”, &first, &second); if(first > second) { printf(“First number is bigger”); } }
  • 6. Branching: The if-else statement (cont..)  In the written program, if the input is higher than the second number, only than the printf statement will be executed.  If the input is 20 10  We will see the output First number is bigger  If the input is 10 20  No output message will appear on the screen.  We can extend the above program
  • 7. Branching: The if-else statement (cont..)  Lets extend the previous program: #include<stdio.h> main() { int first, second; scanf(“%d %d”, &first, &second); if(first > second) printf(“First number is biggern”); if(second >first) printf(“Second number is biggern”); if(first == second) printf(“Two numbers are equaln”); }
  • 8. Branching: The if-else statement (cont..)  This will give the following results:  If the input is 20 10  We will see the output First number is bigger  If the input is 10 20  We will see the output Second number is bigger  If the input is 20 10  We will see the output Two numbers are equal
  • 9. Branching: The if-else statement (cont..)  We can also rewrite the program like this: #include<stdio.h> main() { int first, second; scanf(“%d %d”, &first, &second); if(first > second) printf(“First number is biggern”); else if(second >first) printf(“Second number is biggern”); else printf(“Two numbers are equaln”); }
  • 10. Branching: The if-else statement (cont..)  What we can see is:  The if-else statement can be written as: if (testExpression1) { // statement(s) } else if(testExpression2){ // statement(s) } ……… else { // statement(s) }
  • 11. Branching: The if-else statement (cont..)
  • 12. The if-else statement (More Examples)  Example 2: Suppose, we want to calculate the biggest among three numbers. So, how can we solve this problem?
  • 13. The if-else statement (More Examples) #include<stdio.h> main(){ int a,b,c, biggest; scanf(“%d %d %d”, &a, &b, &c); if(a > b && a > c) biggest = a; else if(b> c) biggest = b; else biggest = c; printf(“biggest = %d”, biggest); }
  • 14. The nested if-else statement  It is possible to nest if-else statement, one within another  There are several different forms that nested if-else statements can take.  The most general form of two-later nesting is if e1 if e2 s1 else s2 else if e3 s3 else s4
  • 15. The nested if-else statement (Example)  Recall the previous Example: Suppose, we want to calculate the biggest among three numbers. So, how can we solve this problem?
  • 16. The if-else statement (Example) #include<stdio.h> main(){ int a,b,c, biggest; scanf(“%d %d %d”, &a, &b, &c); if(a > b) if(a>c) biggest = a; else if(b> c) biggest = b; else biggest = c; printf(“biggest = %d”, biggest); }
  • 17. Looping  We may encounter situations, when a block of code needs to be executed several number of times.  In general, statements are executed sequentially:The first statement in a function is executed first, followed by the second, and so on.  A loop statement allows us to execute a statement or group of statements multiple times.
  • 18. Looping (cont..)  The general form of a loop statement in most of the programming languages −
  • 19. Looping (cont..) C programming language provides the following types of loops to handle looping requirements.  while loop: Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.  for loop: Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.  do...while loop: It is more like a while statement, except that it tests the condition at the end of the loop body.  nested loops: You can use one or more loops inside any other while, for, or do..while loop.
  • 20. Looping: The while Statement  Suppose we want to display “hello” on output screen five times in five different lines, we might think of writing either five printf statements or one printf statement consisting of constant string “hellon” five times.  Now, if we want to display “hello” 500 times, what will happen?  To solve this, we need some programming facility to repeat certain works.
  • 21. The while Statement  The while statement is used to carry out looping operation in which a group of statements is executed repeatedly, until some condition has been satisfied.  The general form of the while statement is while (expression) statement  The statement will be executed repeatedly, as long as the expression is true.
  • 23. The while Statement (cont..)  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 = 0; ● while(digit<=9){ ● printf(“%dn”,digit); ● ++digit; ● }
  • 24. The while Statement (cont..)  Output: ● 0 ● 1 ● 2 ● 3 ● 4 ● 5 ● 6 ● 7 ● 8 ● 9
  • 25. The while 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 = 1, sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); while (count <= num) { sum += count; ++count; } printf("Sum = %d", sum); }
  • 26. The while Statement: Infinite Loop Infinite loop: var will always have value >=5 so the loop would never end. int main() { int var = 6; while (var >=5) { printf("%d", var); var++; } return 0; }
  • 27. The while Statement: Infinite Loop Infinite loop: var value will keep decreasing because of –- operator, hence it will always be <= 10. int main() { int var =5; while (var <=10) { printf("%d", var); var--; } return 0; }