SlideShare a Scribd company logo
1 of 25
Control Structure in C
Made By: Komal Kotak
C.K.Pithawala College of Engineering and Technology
C Programming Definition
C is a high-level and general-
purpose programming language that is ideal for developing
firmware or portable applications. Originally intended for
writing system software, C was developed at Bell Labs by
Dennis Ritchie for the Unix Operating System (OS) in the
early 1970s
Conditions in C Programming
o C executes program sequentially.
o Sometimes a program requires checking of certain
conditions in program execution.
o C provides various key condition statements to check
conditions and execute statements according conditional
criteria.
o This statements are called decision making statements and
control statements.
Control Structure in C
Decision making structures require that the
programmer specifies one or more conditions to be
evaluated or tested by the program, along with a
statement or statements to be executed if the
condition is determined to be true, and optionally,
other statements to be executed if the condition is
determined to be false.
Show here is the general form of a typical decision
making structure found in most of the
programming languages −
if-else statement (1)
The if-else statement can exist in two
forms: with or without the else. The two
forms are:
if(expression)
statement
or
if(expression)
statement1
else
statement2
int a, b;
// ...
if(a < b) a = 0;
else b = 0;
Example: Odd Or Even No. Program
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
if-else statement (2)
Output
Enter an integer: 7
7 is an odd integer.
Nested if-else statement (1)
o The if...else statement
executes two different codes
depending upon whether the
test expression is true or false.
Sometimes, a choice has to be
made from more than 2
possibilities.
o The nested if-else statement
allows you to check for
multiple test expressions and
execute different codes for
more than two conditions.
Nested if-else statement (2)
Example: Program to relate two integers using =, > or <
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if(number1 == number2)
{
printf("Result: %d = %d",number1,number2); }
else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2) }
else
{
printf("Result: %d < %d",number1, number2); }
return 0; }
Output
Enter two integers: 12
23
Result: 12 < 23
C Programming for Loop
Loops are used in programming to repeat a specific block until some
end condition is met.There are three loops in C programming:
1. for loop
2. while loop
3. do...while loop
For Loop (1)
o The initialization statement is executed only
once.
o Then, the test expression is evaluated. If the test
expression is false (0), for loop is terminated. But
if the test expression is true (nonzero), codes
inside the body of for loop is executed and the
update expression is updated.
o This process repeats until the test expression is
false.
o The for loop is commonly used when the number
of iterations is known.
For Loop (2)
Example: Program to calculate the sum of first n natural numbers
#include <stdio.h>
int main()
{
int num, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
for(i = 1; i <= num; i++)
{
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
Output
Enter a positive integer: 10
Sum = 55
while Loop (1)
o The while loop evaluates the test expression.
o If the test expression is true (nonzero), codes inside the body
of while loop is evaluated. The test expression is evaluated
again.The process goes on until the test expression is false.
o When the test expression is false, the while loop is
terminated.
o The syntax of a while loop is:
while (testExpression)
{
//codes
}
o where, testExpression checks the condition is true or false
before each loop.
while Loop (2)
Example: Program to find factorial of a number
#include <stdio.h>
int main()
{
int number;
int factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
while (number > 0)
{
factorial *= number;
--number;
}
printf("Factorial= %d", factorial);
return 0; }
Output
Enter an integer: 5
Factorial = 120
do-while Loop (1)
o The do..while loop is similar to the while loop with one important
difference.The body of do...while loop is executed once, before
checking the test expression. Hence, the do...while loop is executed at
least once.
o do...while loop Syntax
do
{
// codes
}
while (testExpression);
o The code block (loop body) inside the braces is executed once.
o Then, the test expression is evaluated. If the test expression is true,
the loop body is executed again.This process goes on until the test
expression is evaluated to 0 (false).
o When the test expression is false (nonzero), the do...while loop is
terminated.
do-while Loop (2)
Example: Program to add numbers until user enters zero
#include <stdio.h>
int main()
{
float number, sum = 0;
do
{
printf("Enter a number: ");
scanf("%f", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %f",sum);
return 0;
}
Output
Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70
break Statement (1)
o It is sometimes desirable to skip some statements inside the loop or terminate
the loop immediately without checking the test expression.
o In such cases, break and continue statements are used.
o The break statement terminates the loop (for, while and do...while loop)
immediately when it is encountered.The break statement is used with decision
making statement such as if...else.
o Syntax of break statement
break;
break Statement (2)
continue Statement
o The continue statement skips
some statements inside the loop.
The continue statement is used
with decision making statement
such as if...else.
o Syntax of continue Statement
continue;
Switch case Statement (1)
o The switch case statement evaluates the value of an
expression and a block of code is selected on the basis
of that evaluated expression.
o Each case refers back to the original expression.
o The data type that can be used in switch expression is
integer type only.
o Each case has a break statement.
o The switch case takes decision on the basis of
equality.
Switch case Statement (2)
int main() {
char op;
int num1,num2;
printf("Enter an operator (+,-,*,/):");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%d %d",&num1, &num2);
switch(op)
{
case '+':
printf("%d + %d =
%d",num1, num2, num1+num2);
break;
case '-':
printf("%d - %d = %d",num1,
num2, num1-num2);
break;
case '*':
printf("%d * %d = %d",num1,
num2, num1*num2);
break;
case '/':
printf("%d / %d = %d",num1,
num2, num1/num2);
break;
default:
printf("Error! op is not
correct");
}
return 0;
}
Switch case Statement (3)
Output
Enter an operator (+, -, *,): -
Enter two operands: 32
12
32 - 12 = 20
goto Statement (1)
o The goto statement is used to alter the normal sequence of a C program.
o Syntax of goto statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
o The label is an identifier. When goto statement is encountered, control of the
program jumps to label: and starts executing the code.
goto Statement (2)
Example: To check whether you can
vote or not
int main()
{
int age;
Vote:
printf("you are eligible for
voting");
NoVote:
printf("you are not eligible to
vote");
printf("Enter your age:");
scanf("%d", &age);
if(age>=18)
goto Vote;
else
goto NoVote;
return 0;
}
Output
Enter your age:15
You are not eligible to vote
Reason to avoid goto Statement
o The use of goto statement may lead to code that is buggy and hard to follow. For example:
one:
for (i = 0; i < number; ++i)
{
test += i;
goto two;
}
two:
if (test > 5) {
goto three;
}
... .. ...
Also, goto statement allows you to do bad stuff such as jump out of scope.
o That being said, goto statement can be useful sometimes. For example: to break from nested loops.
Thank You

More Related Content

What's hot

C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Neeru Mittal
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++Bishal Sharma
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 

What's hot (20)

Loops in c
Loops in cLoops in c
Loops in c
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 
Enums in c
Enums in cEnums in c
Enums in c
 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
C program
C programC program
C program
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
If else statement in c++
If else statement in c++If else statement in c++
If else statement in c++
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 

Similar to Control structure of c

C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02Suhail Akraam
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptxDEEPAK948083
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solutionKuntal Bhowmick
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxAnkitaVerma776806
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops Hemantha Kulathilake
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 

Similar to Control structure of c (20)

C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
What is c
What is cWhat is c
What is c
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
the refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptxthe refernce of programming C notes ppt.pptx
the refernce of programming C notes ppt.pptx
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Lec 10
Lec 10Lec 10
Lec 10
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 

More from Komal Kotak

Internet Of things
Internet Of things Internet Of things
Internet Of things Komal Kotak
 
Mesh analysis and Nodal Analysis
Mesh analysis and Nodal AnalysisMesh analysis and Nodal Analysis
Mesh analysis and Nodal AnalysisKomal Kotak
 
Decision Tree and Bayesian Classification
Decision Tree and Bayesian ClassificationDecision Tree and Bayesian Classification
Decision Tree and Bayesian ClassificationKomal Kotak
 
4 stroke petrol engine and 4 stroke diesel engine
4 stroke petrol engine and 4 stroke diesel engine 4 stroke petrol engine and 4 stroke diesel engine
4 stroke petrol engine and 4 stroke diesel engine Komal Kotak
 
Traits of good listener
Traits of good listenerTraits of good listener
Traits of good listenerKomal Kotak
 

More from Komal Kotak (6)

Internet Of things
Internet Of things Internet Of things
Internet Of things
 
Mesh analysis and Nodal Analysis
Mesh analysis and Nodal AnalysisMesh analysis and Nodal Analysis
Mesh analysis and Nodal Analysis
 
Decision Tree and Bayesian Classification
Decision Tree and Bayesian ClassificationDecision Tree and Bayesian Classification
Decision Tree and Bayesian Classification
 
4 stroke petrol engine and 4 stroke diesel engine
4 stroke petrol engine and 4 stroke diesel engine 4 stroke petrol engine and 4 stroke diesel engine
4 stroke petrol engine and 4 stroke diesel engine
 
Corruption
CorruptionCorruption
Corruption
 
Traits of good listener
Traits of good listenerTraits of good listener
Traits of good listener
 

Recently uploaded

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptxMECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptxAnupkumar Sharma
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
The State of AI in Insights and Research 2024: Results and Findings
The State of AI in Insights and Research 2024: Results and FindingsThe State of AI in Insights and Research 2024: Results and Findings
The State of AI in Insights and Research 2024: Results and FindingsRay Poynter
 
CONCEPT OF MUTATION AND ITS CLASSIFICATION .pptx
CONCEPT OF MUTATION AND ITS CLASSIFICATION .pptxCONCEPT OF MUTATION AND ITS CLASSIFICATION .pptx
CONCEPT OF MUTATION AND ITS CLASSIFICATION .pptxAnupkumar Sharma
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 

Recently uploaded (20)

HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptxMECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
MECHANISMS OF DIFFERENT TYPES OF HYPERSENITIVITY REACTIONS.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
The State of AI in Insights and Research 2024: Results and Findings
The State of AI in Insights and Research 2024: Results and FindingsThe State of AI in Insights and Research 2024: Results and Findings
The State of AI in Insights and Research 2024: Results and Findings
 
CONCEPT OF MUTATION AND ITS CLASSIFICATION .pptx
CONCEPT OF MUTATION AND ITS CLASSIFICATION .pptxCONCEPT OF MUTATION AND ITS CLASSIFICATION .pptx
CONCEPT OF MUTATION AND ITS CLASSIFICATION .pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 

Control structure of c

  • 1. Control Structure in C Made By: Komal Kotak C.K.Pithawala College of Engineering and Technology
  • 2. C Programming Definition C is a high-level and general- purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System (OS) in the early 1970s
  • 3. Conditions in C Programming o C executes program sequentially. o Sometimes a program requires checking of certain conditions in program execution. o C provides various key condition statements to check conditions and execute statements according conditional criteria. o This statements are called decision making statements and control statements.
  • 4. Control Structure in C Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Show here is the general form of a typical decision making structure found in most of the programming languages −
  • 5. if-else statement (1) The if-else statement can exist in two forms: with or without the else. The two forms are: if(expression) statement or if(expression) statement1 else statement2 int a, b; // ... if(a < b) a = 0; else b = 0;
  • 6. Example: Odd Or Even No. Program #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d",&number); if( number%2 == 0 ) printf("%d is an even integer.",number); else printf("%d is an odd integer.",number); return 0; } if-else statement (2) Output Enter an integer: 7 7 is an odd integer.
  • 7. Nested if-else statement (1) o The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. o The nested if-else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
  • 8. Nested if-else statement (2) Example: Program to relate two integers using =, > or < #include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); if(number1 == number2) { printf("Result: %d = %d",number1,number2); } else if (number1 > number2) { printf("Result: %d > %d", number1, number2) } else { printf("Result: %d < %d",number1, number2); } return 0; } Output Enter two integers: 12 23 Result: 12 < 23
  • 9. C Programming for Loop Loops are used in programming to repeat a specific block until some end condition is met.There are three loops in C programming: 1. for loop 2. while loop 3. do...while loop
  • 10. For Loop (1) o The initialization statement is executed only once. o Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and the update expression is updated. o This process repeats until the test expression is false. o The for loop is commonly used when the number of iterations is known.
  • 11. For Loop (2) Example: Program to calculate the sum of first n natural numbers #include <stdio.h> int main() { int num, i, sum = 0; printf("Enter a positive integer: "); scanf("%d", &num); for(i = 1; i <= num; i++) { sum += i; } printf("Sum = %d", sum); return 0; } Output Enter a positive integer: 10 Sum = 55
  • 12. while Loop (1) o The while loop evaluates the test expression. o If the test expression is true (nonzero), codes inside the body of while loop is evaluated. The test expression is evaluated again.The process goes on until the test expression is false. o When the test expression is false, the while loop is terminated. o The syntax of a while loop is: while (testExpression) { //codes } o where, testExpression checks the condition is true or false before each loop.
  • 13. while Loop (2) Example: Program to find factorial of a number #include <stdio.h> int main() { int number; int factorial; printf("Enter an integer: "); scanf("%d",&number); factorial = 1; while (number > 0) { factorial *= number; --number; } printf("Factorial= %d", factorial); return 0; } Output Enter an integer: 5 Factorial = 120
  • 14. do-while Loop (1) o The do..while loop is similar to the while loop with one important difference.The body of do...while loop is executed once, before checking the test expression. Hence, the do...while loop is executed at least once. o do...while loop Syntax do { // codes } while (testExpression); o The code block (loop body) inside the braces is executed once. o Then, the test expression is evaluated. If the test expression is true, the loop body is executed again.This process goes on until the test expression is evaluated to 0 (false). o When the test expression is false (nonzero), the do...while loop is terminated.
  • 15. do-while Loop (2) Example: Program to add numbers until user enters zero #include <stdio.h> int main() { float number, sum = 0; do { printf("Enter a number: "); scanf("%f", &number); sum += number; } while(number != 0.0); printf("Sum = %f",sum); return 0; } Output Enter a number: 1.5 Enter a number: 2.4 Enter a number: -3.4 Enter a number: 4.2 Enter a number: 0 Sum = 4.70
  • 16. break Statement (1) o It is sometimes desirable to skip some statements inside the loop or terminate the loop immediately without checking the test expression. o In such cases, break and continue statements are used. o The break statement terminates the loop (for, while and do...while loop) immediately when it is encountered.The break statement is used with decision making statement such as if...else. o Syntax of break statement break;
  • 18. continue Statement o The continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else. o Syntax of continue Statement continue;
  • 19. Switch case Statement (1) o The switch case statement evaluates the value of an expression and a block of code is selected on the basis of that evaluated expression. o Each case refers back to the original expression. o The data type that can be used in switch expression is integer type only. o Each case has a break statement. o The switch case takes decision on the basis of equality.
  • 20. Switch case Statement (2) int main() { char op; int num1,num2; printf("Enter an operator (+,-,*,/):"); scanf("%c", &op); printf("Enter two operands: "); scanf("%d %d",&num1, &num2); switch(op) { case '+': printf("%d + %d = %d",num1, num2, num1+num2); break; case '-': printf("%d - %d = %d",num1, num2, num1-num2); break; case '*': printf("%d * %d = %d",num1, num2, num1*num2); break; case '/': printf("%d / %d = %d",num1, num2, num1/num2); break; default: printf("Error! op is not correct"); } return 0; }
  • 21. Switch case Statement (3) Output Enter an operator (+, -, *,): - Enter two operands: 32 12 32 - 12 = 20
  • 22. goto Statement (1) o The goto statement is used to alter the normal sequence of a C program. o Syntax of goto statement goto label; ... .. ... ... .. ... ... .. ... label: statement; o The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the code.
  • 23. goto Statement (2) Example: To check whether you can vote or not int main() { int age; Vote: printf("you are eligible for voting"); NoVote: printf("you are not eligible to vote"); printf("Enter your age:"); scanf("%d", &age); if(age>=18) goto Vote; else goto NoVote; return 0; } Output Enter your age:15 You are not eligible to vote
  • 24. Reason to avoid goto Statement o The use of goto statement may lead to code that is buggy and hard to follow. For example: one: for (i = 0; i < number; ++i) { test += i; goto two; } two: if (test > 5) { goto three; } ... .. ... Also, goto statement allows you to do bad stuff such as jump out of scope. o That being said, goto statement can be useful sometimes. For example: to break from nested loops.