SlideShare a Scribd company logo
1 of 41
Download to read offline
Chapter 4
Control Structures
8/12/2021 1
Control structure:
In a program, a control structure determines the order in which
statements are executed. Control structures refer to the structure
of program that may be sequential or conditional or iterative. C
programs are basically of three structures:
โ€ข In sequential execution, each statement in the source code will be
executed one by one in a sequential order.
โ€ข The selection or conditional statement is used for making decisions
and branching statements.
โ€ข The iterative control structures are used for repetitively executing a
block of code multiple times.
Control statement:
The statement that alters the flow of execution of the program is
known as control statements.
1. Decision Making (conditional) statements
2. Loop statements
3. Jumping Statements
8/12/2021 2
1. Decision Making (conditional)
statements
Conditional statement:
A statement that is executed only when certain condition
within a program has been met is called conditional
expression. These expressions are used for decision
making.
Following statements are used to perform the task of
decision making:
I. IF statement
II. SWITCH statement
III. Conditional statement
8/12/2021 3
I. IF statement:
If the statement is used to control the flow of
execution of statements. It is implemented in
different forms. Depending on the complexity
of conditions to be tested, the different forms
are:
A. Simple if statement
B. Ifโ€ฆ.. else
C. Nested if else
D. Else if ladder
8/12/2021 4
A. Simple if statement
Syntax of simple if statement:
if (condition)
{
//These statements will only execute if the condition is true
}
Flowchart for simple if statement:
8/12/2021 5
Example for simple if statement:
#include <stdio.h>
#include <conio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf(" %d is less than %d",x,y);
}
return 0;
getch();
}
8/12/2021 6
B. If โ€ฆ. else Statement
Syntax of if...else statement:
if (testexpression)
{
// statementsto be executed if the test expression is true
}
else {
// statementsto be executed if the test expression is false
}
8/12/2021 7
Example for if...else statement:
// Check whether an integer is odd or even using ifโ€ฆ. Else statement
#include <stdio.h>
int main()
{
int num;
printf("Enteran integer: ");
scanf("%d", &num);
if (num%2 == 0)
{
printf("%dis an even integer.",num);
}
else
{
printf("%dis an odd integer.",num);
}
return 0;
}
8/12/2021 8
C. Nested if...else statement
Syntax of Nested if...else statement:
if(condition) {
if(condition2) {
//Statement1
}
else {
//Statement2
}
}
else {
//Statement3
}
8/12/2021 9
Flowchart of Nested if...else statement:
8/12/2021 10
Example of Nested if...else statement:
/* C Program to find given number is positive or negative using nested if ...else */
#include <stdio.h>
#include <conio.h>
int main()
{
float num;
printf("Enter a number: ");
scanf("%f",&num);
if (num <= 0)
{
if (num == 0)
printf("Youentered 0.");
else
printf("%fis a negative number.",num);
}
else
printf("%fis a positive number.",num);
return 0;
getch();
}
8/12/2021 11
// Check Largest number among two given numbers
#include <stdio.h>
main()
{
int a, b;
printf("Enter two numbers: n");
scanf("%d %d", &a, &b);
if(a >= b)
{
if (a==b)
{
printf("Both number are equaln");
}
else {
printf("%d is Largestn", a);
}
}
else {
printf("%d is Largestn", b);
}
}
8/12/2021 12
D. Else if ladder
Syntax of else if ladder statement:
if (condition1)
{
//These statements would execute if the condition1 is true
}
else if(condition2)
{
//These statements would execute if the condition2 is true
}
else if (condition3)
{
//These statements would execute if the condition3 is true
}
.
.
else
{
//These statements would execute if all the conditions return false.
}
8/12/2021 13
Flowchart of else if ladder statement:
8/12/2021 14
Example of else if ladder statement:
// Check Largest number among two given numbers
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: n");
scanf("%d %d", &a, &b);
if(a == b)
{
printf("Both numbers are equalโ€œ,a,b);
}
else if (a > b)
{
printf(" %d is largest than %d", a, b);
}
else
{
printf(" %d is largest than %d", b, a);
}
return 0;
}
8/12/2021 15
II. Switch Statement
The switch statement allows us to execute one code block among many alternatives.
A switch statement tests the value of a variable and compares it with multiple cases.
Once the case match is found, a block of statements associated with that particular case is executed.
Each case in a block of a switch has a different name/number which is referred to as an identifier.
The value provided by the user is compared with all the cases inside the switch block until the match is
found.
If a case match is not found, then the default statement is executed, and the control goes out of the
switch block.
Syntax of switch statement:
switch (expression)
โ€‹{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
8/12/2021 16
flowchart of switch statement
8/12/2021 17
#include <stdio.h>
int main()
{
int num=2;
switch(num)
{
case 1:
printf("Case1:Value is: %d", num);
break;
case 2:
printf("Case2:Value is: %d", num);
break;
case 3:
printf("Case3:Value is: %d", num);
break;
default:
printf("Default:Value is: %d", num);
}
return 0;
}
8/12/2021 18
#include <stdio.h>
main()
{
int a, b;
char ch;
printf("What do you want to perform?? n ");
printf(" Enter first letter: A for add, S for subtract, M for multiply, or D for divide?n");
printf("Your choice: ");
ch = getchar();
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
switch(ch)
{
case 'A': printf("%d", a + b);
break;
case 'S': printf("%d", a - b);
break;
case 'M': printf("%d", a * b);
break;
case 'D': if(b!=0) printf("%d", a / b);
break;
default:
printf("choose properly") ;
}
}
8/12/2021 19
III. Conditional(ternary ) statement
If any operator is used on three operands or variable is known as Ternary
Operator.
The first argument is a comparison argument, the second is the result
upon a true comparison, and the third is the result upon a false
comparison.
Ternary operator is shortened way of writing an if-else statement.
syntax:
(condition) ? expression-2 : expression-3
8/12/2021 20
//example program to illustrate the use of ternary statements
#include<stdio.h>
main()
{
int a, b, c, large;
printf("Enter any three number: ");
scanf("%d%d%d",&a,&b,&c);
large=a>b ? (a>c?a:c) : (b>c?b:c);
printf("Largest Number is: %d", large);
}
8/12/2021 21
2. Loop statements
Loop statement:
loops are used to repeat a block of code until a
specified condition is met.
Following statements are used to perform
repetitive task:
I. for loop
II. while loop
III. do while loop
8/12/2021 22
I. for loop
8/12/2021 23
8/12/2021 24
8/12/2021 25
8/12/2021 26
8/12/2021 27
8/12/2021 28
8/12/2021 29
II. while loop
8/12/2021 30
8/12/2021 31
8/12/2021 32
III. do while loop
8/12/2021 33
8/12/2021 34
8/12/2021 35
3 . Jumping statement
There are three different controls used to jump
from one C program statement to another and
make the execution of the programming
procedure fast. These three Jumping controls are:
A. go to statement
B. break statement
C. continue statement
8/12/2021 36
A. go to statement
The go to moves the control on a specified address called label or label name.
Syntax:
goto label_name;
.. ..
label_name: C-statements
Example program of go to statement:
#include <stdio.h>
main()
{
int sum=0;
for(int i = 0; i<=10; i++)
{
sum = sum+i;
if(i==5)
{
goto addition;
}
}
addition:
printf("%d", sum);
}
8/12/2021 37
B. break statement
Break is always used with the decision-makingstatementlike if and switch
statements.
The statementwill quit from the loop when the conditionis true.
Example program using break statement
#include< stdio.h>
main()
{
int i=1;
while(i<=10)
{
if(i==6)
{
break;
}
printf("n I=%d",i);
i++;
}
}
8/12/2021 38
C. continue statement
This statementis skip some part of iteration (loop) and comes to the next looping
step i.e. it will increment / decrement the loop value, when continue occurs.
Example program using continue statement
#include< stdio.h>
main()
{
int i=1;
while(i<=10)
{
if(i==6)
{
i++;
continue;
}
printf("n I=%d",i);
i++;
}
}
8/12/2021 39
Assignment 4(submit up to 22 Jan)
1. WAP to input an integer and check if it is palindrome or not.
2. Differentiate between
โ€ข pre-test and post-test loop.
โ€ข Break and continue statement
โ€ข Switch and nested if else
3. WAP to generate all prime numbers from 1 to 100.
4. WAP to evaluate the following power series:
ex =1+x/1!+x2/2!+x3/3!+โ€ฆโ€ฆ.xn/n!, 0<x<1
3. WAP to display the following output :
5
5 4
5 4 3
8/12/2021 40
Thank you ๏Š
8/12/2021 41

More Related Content

Similar to chapter-4 slide.pdf

Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguageTanmay Modi
ย 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
ย 
Chapter 2 - Flow of Control Part I.pdf
Chapter 2 -  Flow of Control Part I.pdfChapter 2 -  Flow of Control Part I.pdf
Chapter 2 - Flow of Control Part I.pdfKirubelWondwoson1
ย 
Control Structures
Control StructuresControl Structures
Control StructuresGhaffar Khan
ย 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.pptManojKhadilkar1
ย 
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 languagechintupro9
ย 
C programming decision making
C programming decision makingC programming decision making
C programming decision makingSENA
ย 
UNIT-1 notes(Data Types โ€“ Variables โ€“ Operations โ€“ Expressions and Statements...
UNIT-1 notes(Data Types โ€“ Variables โ€“ Operations โ€“ Expressions and Statements...UNIT-1 notes(Data Types โ€“ Variables โ€“ Operations โ€“ Expressions and Statements...
UNIT-1 notes(Data Types โ€“ Variables โ€“ Operations โ€“ Expressions and Statements...RSathyaPriyaCSEKIOT
ย 
C fundamental
C fundamentalC fundamental
C fundamentalSelvam Edwin
ย 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)Niket Chandrawanshi
ย 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsPrabu U
ย 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2yasir_cesc
ย 
Programming in C
Programming in CProgramming in C
Programming in CNishant Munjal
ย 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programmeDhilip Prakash
ย 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programmeDhilip Prakash
ย 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
ย 
Password protected diary
Password protected diaryPassword protected diary
Password protected diarySHARDA SHARAN
ย 

Similar to chapter-4 slide.pdf (20)

Decision statements in c laguage
Decision statements in c laguageDecision statements in c laguage
Decision statements in c laguage
ย 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
ย 
Chapter 2 - Flow of Control Part I.pdf
Chapter 2 -  Flow of Control Part I.pdfChapter 2 -  Flow of Control Part I.pdf
Chapter 2 - Flow of Control Part I.pdf
ย 
Control Structures
Control StructuresControl Structures
Control Structures
ย 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ย 
C programming session3
C programming  session3C programming  session3
C programming session3
ย 
C programming session3
C programming  session3C programming  session3
C programming session3
ย 
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
ย 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
ย 
UNIT-1 notes(Data Types โ€“ Variables โ€“ Operations โ€“ Expressions and Statements...
UNIT-1 notes(Data Types โ€“ Variables โ€“ Operations โ€“ Expressions and Statements...UNIT-1 notes(Data Types โ€“ Variables โ€“ Operations โ€“ Expressions and Statements...
UNIT-1 notes(Data Types โ€“ Variables โ€“ Operations โ€“ Expressions and Statements...
ย 
C fundamental
C fundamentalC fundamental
C fundamental
ย 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
ย 
Decision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, StringsDecision Making Statements, Arrays, Strings
Decision Making Statements, Arrays, Strings
ย 
[C++][a] tutorial 2
[C++][a] tutorial 2[C++][a] tutorial 2
[C++][a] tutorial 2
ย 
Lecture 3
Lecture 3Lecture 3
Lecture 3
ย 
Programming in C
Programming in CProgramming in C
Programming in C
ย 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
ย 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
ย 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
ย 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
ย 

More from cricketreview

FINAL PRESENTATION.pptx
FINAL PRESENTATION.pptxFINAL PRESENTATION.pptx
FINAL PRESENTATION.pptxcricketreview
ย 
DREM_chapter 6.pptx
DREM_chapter 6.pptxDREM_chapter 6.pptx
DREM_chapter 6.pptxcricketreview
ย 
URBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdfURBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdfcricketreview
ย 
Estimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptxEstimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptxcricketreview
ย 
Chapter 3.4_ Art Deco.pdf
Chapter 3.4_  Art Deco.pdfChapter 3.4_  Art Deco.pdf
Chapter 3.4_ Art Deco.pdfcricketreview
ย 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptxcricketreview
ย 
3e- Persian Gardens.pptx
3e- Persian Gardens.pptx3e- Persian Gardens.pptx
3e- Persian Gardens.pptxcricketreview
ย 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptxcricketreview
ย 
Vernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptxVernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptxcricketreview
ย 
URBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdfURBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdfcricketreview
ย 

More from cricketreview (10)

FINAL PRESENTATION.pptx
FINAL PRESENTATION.pptxFINAL PRESENTATION.pptx
FINAL PRESENTATION.pptx
ย 
DREM_chapter 6.pptx
DREM_chapter 6.pptxDREM_chapter 6.pptx
DREM_chapter 6.pptx
ย 
URBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdfURBAN DESIGN-lecture-4.pdf
URBAN DESIGN-lecture-4.pdf
ย 
Estimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptxEstimating and costing I Chapter 2.pptx
Estimating and costing I Chapter 2.pptx
ย 
Chapter 3.4_ Art Deco.pdf
Chapter 3.4_  Art Deco.pdfChapter 3.4_  Art Deco.pdf
Chapter 3.4_ Art Deco.pdf
ย 
chapter-6 slide.pptx
chapter-6 slide.pptxchapter-6 slide.pptx
chapter-6 slide.pptx
ย 
3e- Persian Gardens.pptx
3e- Persian Gardens.pptx3e- Persian Gardens.pptx
3e- Persian Gardens.pptx
ย 
chapter-7 slide.pptx
chapter-7 slide.pptxchapter-7 slide.pptx
chapter-7 slide.pptx
ย 
Vernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptxVernacular Architecture of Nepal.pptx
Vernacular Architecture of Nepal.pptx
ย 
URBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdfURBAN DESIGN-lecture-6.pdf
URBAN DESIGN-lecture-6.pdf
ย 

Recently uploaded

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
ย 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
ย 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
ย 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
ย 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
ย 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
ย 
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Call Girls in Nagpur High Profile
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
ย 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
ย 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
ย 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
ย 

Recently uploaded (20)

Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
ย 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ย 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
ย 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
ย 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
ย 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
ย 
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth โŸŸ 6297143586 โŸŸ Call Me For Genuine Se...
ย 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
ย 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
ย 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
ย 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
ย 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
ย 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
ย 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
ย 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
ย 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
ย 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
ย 
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar  โ‰ผ๐Ÿ” Delhi door step de...
Call Now โ‰ฝ 9953056974 โ‰ผ๐Ÿ” Call Girls In New Ashok Nagar โ‰ผ๐Ÿ” Delhi door step de...
ย 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
ย 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
ย 

chapter-4 slide.pdf

  • 2. Control structure: In a program, a control structure determines the order in which statements are executed. Control structures refer to the structure of program that may be sequential or conditional or iterative. C programs are basically of three structures: โ€ข In sequential execution, each statement in the source code will be executed one by one in a sequential order. โ€ข The selection or conditional statement is used for making decisions and branching statements. โ€ข The iterative control structures are used for repetitively executing a block of code multiple times. Control statement: The statement that alters the flow of execution of the program is known as control statements. 1. Decision Making (conditional) statements 2. Loop statements 3. Jumping Statements 8/12/2021 2
  • 3. 1. Decision Making (conditional) statements Conditional statement: A statement that is executed only when certain condition within a program has been met is called conditional expression. These expressions are used for decision making. Following statements are used to perform the task of decision making: I. IF statement II. SWITCH statement III. Conditional statement 8/12/2021 3
  • 4. I. IF statement: If the statement is used to control the flow of execution of statements. It is implemented in different forms. Depending on the complexity of conditions to be tested, the different forms are: A. Simple if statement B. Ifโ€ฆ.. else C. Nested if else D. Else if ladder 8/12/2021 4
  • 5. A. Simple if statement Syntax of simple if statement: if (condition) { //These statements will only execute if the condition is true } Flowchart for simple if statement: 8/12/2021 5
  • 6. Example for simple if statement: #include <stdio.h> #include <conio.h> int main() { int x = 20; int y = 22; if (x<y) { printf(" %d is less than %d",x,y); } return 0; getch(); } 8/12/2021 6
  • 7. B. If โ€ฆ. else Statement Syntax of if...else statement: if (testexpression) { // statementsto be executed if the test expression is true } else { // statementsto be executed if the test expression is false } 8/12/2021 7
  • 8. Example for if...else statement: // Check whether an integer is odd or even using ifโ€ฆ. Else statement #include <stdio.h> int main() { int num; printf("Enteran integer: "); scanf("%d", &num); if (num%2 == 0) { printf("%dis an even integer.",num); } else { printf("%dis an odd integer.",num); } return 0; } 8/12/2021 8
  • 9. C. Nested if...else statement Syntax of Nested if...else statement: if(condition) { if(condition2) { //Statement1 } else { //Statement2 } } else { //Statement3 } 8/12/2021 9
  • 10. Flowchart of Nested if...else statement: 8/12/2021 10
  • 11. Example of Nested if...else statement: /* C Program to find given number is positive or negative using nested if ...else */ #include <stdio.h> #include <conio.h> int main() { float num; printf("Enter a number: "); scanf("%f",&num); if (num <= 0) { if (num == 0) printf("Youentered 0."); else printf("%fis a negative number.",num); } else printf("%fis a positive number.",num); return 0; getch(); } 8/12/2021 11
  • 12. // Check Largest number among two given numbers #include <stdio.h> main() { int a, b; printf("Enter two numbers: n"); scanf("%d %d", &a, &b); if(a >= b) { if (a==b) { printf("Both number are equaln"); } else { printf("%d is Largestn", a); } } else { printf("%d is Largestn", b); } } 8/12/2021 12
  • 13. D. Else if ladder Syntax of else if ladder statement: if (condition1) { //These statements would execute if the condition1 is true } else if(condition2) { //These statements would execute if the condition2 is true } else if (condition3) { //These statements would execute if the condition3 is true } . . else { //These statements would execute if all the conditions return false. } 8/12/2021 13
  • 14. Flowchart of else if ladder statement: 8/12/2021 14
  • 15. Example of else if ladder statement: // Check Largest number among two given numbers #include <stdio.h> int main() { int a, b; printf("Enter two numbers: n"); scanf("%d %d", &a, &b); if(a == b) { printf("Both numbers are equalโ€œ,a,b); } else if (a > b) { printf(" %d is largest than %d", a, b); } else { printf(" %d is largest than %d", b, a); } return 0; } 8/12/2021 15
  • 16. II. Switch Statement The switch statement allows us to execute one code block among many alternatives. A switch statement tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is not found, then the default statement is executed, and the control goes out of the switch block. Syntax of switch statement: switch (expression) โ€‹{ case constant1: // statements break; case constant2: // statements break; . . . default: // default statements } 8/12/2021 16
  • 17. flowchart of switch statement 8/12/2021 17
  • 18. #include <stdio.h> int main() { int num=2; switch(num) { case 1: printf("Case1:Value is: %d", num); break; case 2: printf("Case2:Value is: %d", num); break; case 3: printf("Case3:Value is: %d", num); break; default: printf("Default:Value is: %d", num); } return 0; } 8/12/2021 18
  • 19. #include <stdio.h> main() { int a, b; char ch; printf("What do you want to perform?? n "); printf(" Enter first letter: A for add, S for subtract, M for multiply, or D for divide?n"); printf("Your choice: "); ch = getchar(); printf("Enter first number: "); scanf("%d", &a); printf("Enter second number: "); scanf("%d", &b); switch(ch) { case 'A': printf("%d", a + b); break; case 'S': printf("%d", a - b); break; case 'M': printf("%d", a * b); break; case 'D': if(b!=0) printf("%d", a / b); break; default: printf("choose properly") ; } } 8/12/2021 19
  • 20. III. Conditional(ternary ) statement If any operator is used on three operands or variable is known as Ternary Operator. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. Ternary operator is shortened way of writing an if-else statement. syntax: (condition) ? expression-2 : expression-3 8/12/2021 20
  • 21. //example program to illustrate the use of ternary statements #include<stdio.h> main() { int a, b, c, large; printf("Enter any three number: "); scanf("%d%d%d",&a,&b,&c); large=a>b ? (a>c?a:c) : (b>c?b:c); printf("Largest Number is: %d", large); } 8/12/2021 21
  • 22. 2. Loop statements Loop statement: loops are used to repeat a block of code until a specified condition is met. Following statements are used to perform repetitive task: I. for loop II. while loop III. do while loop 8/12/2021 22
  • 33. III. do while loop 8/12/2021 33
  • 36. 3 . Jumping statement There are three different controls used to jump from one C program statement to another and make the execution of the programming procedure fast. These three Jumping controls are: A. go to statement B. break statement C. continue statement 8/12/2021 36
  • 37. A. go to statement The go to moves the control on a specified address called label or label name. Syntax: goto label_name; .. .. label_name: C-statements Example program of go to statement: #include <stdio.h> main() { int sum=0; for(int i = 0; i<=10; i++) { sum = sum+i; if(i==5) { goto addition; } } addition: printf("%d", sum); } 8/12/2021 37
  • 38. B. break statement Break is always used with the decision-makingstatementlike if and switch statements. The statementwill quit from the loop when the conditionis true. Example program using break statement #include< stdio.h> main() { int i=1; while(i<=10) { if(i==6) { break; } printf("n I=%d",i); i++; } } 8/12/2021 38
  • 39. C. continue statement This statementis skip some part of iteration (loop) and comes to the next looping step i.e. it will increment / decrement the loop value, when continue occurs. Example program using continue statement #include< stdio.h> main() { int i=1; while(i<=10) { if(i==6) { i++; continue; } printf("n I=%d",i); i++; } } 8/12/2021 39
  • 40. Assignment 4(submit up to 22 Jan) 1. WAP to input an integer and check if it is palindrome or not. 2. Differentiate between โ€ข pre-test and post-test loop. โ€ข Break and continue statement โ€ข Switch and nested if else 3. WAP to generate all prime numbers from 1 to 100. 4. WAP to evaluate the following power series: ex =1+x/1!+x2/2!+x3/3!+โ€ฆโ€ฆ.xn/n!, 0<x<1 3. WAP to display the following output : 5 5 4 5 4 3 8/12/2021 40