SlideShare una empresa de Scribd logo
1 de 36
MohammedRazi K
razikallayi@gmail.com
www.facebook.com/razikallayi
twitter.com/razikallayi
in.linkedin.com/in/razikallayi
9746730324
Control Structures in C
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
Flow of control
• Unless specified, the order of statement
execution is linear: one statement after another
in sequence
• A control structure is any mechanism that
departs from the default straight-line
execution.
2
1
3
Selection statements
• Used to choose which statement to be executed next
• Therefore they are sometimes called conditional statements
• Conditional statements give us the power to make basic
decisions
• The C conditional statements are the:
– if statement
– if-else statement
– switch statement
The if Statement
The if statement has the following syntax:
if ( condition )
statement;
• if is a C reserved keyword.
• The condition must be a boolean expression. It is evaluated to
either true or false.
• If the condition is true, the statement is executed.
• If it is false, the statement is skipped.
The if-else Statement
• If the condition is true, statement1 is executed; if
the condition is false, statement2 is execute
if ( condition )
statement1;
else
statement2;
Consider the following example,
int grade=76;
if ( grade >= 60 )
printf( "Passedn");
else
printf( "Failedn");
The above code will produce an
output:
Passed
Since the true statement only executes
• An if statement which is within another
if or else is called nested if.
• Braces can be used to specify the if
statement to which an else clause
belongs
Nested if statements
Syntax of nested if:
if(condition1)
{
if(condition2)
do this;
else //this else with condition2
{
do this;
and this;
}
}
else //this else with condition1
do this;
Note that in C an else statement always refers to the
nearest if that is within the same block of else.
An example for nested if:
if(avg>=90)
printf("Student %s gets an A grade",name);
else if(avg>=80 &&avg<90)
printf("Student %s gets a B grade",name);
else if(avg>=70 &&avg<0)
printf("Student %s gets a C grade",name);
else if(avg>=60 &&avg<70)
printf("Student %s gets a D grade",name);
else if(avg<60)
printf("Student %s failed");
Nested if (contd…)
The Switch statement
• The switch statement provides another way to
decide which statement to execute next
• The switch statement evaluates an expression, then
attempts to match the result to one of several
possible cases
• Each case contains a value and a list of statements
• The flow of control transfers to statement associated
with the first case value that matches
• Often a break statement is used as the last statement
in each case's statement list
• A break statement causes control to transfer to the
end of the switch statement
• If a break statement is not used, the flow of control
will continue into the next case
• Sometimes this may be appropriate, but often we want
to execute only the statements associated with one
case
The Switch statement(Contd…)
• A switch statement can have an optional
default case
• If the default case is present, control will
transfer to it if no other case value matches
• If there is no default case, and no other
value matches, control falls through to the
statement after the switch
The Switch statement(Contd…)
• The expression of a switch statement must result in
an integral type, meaning an integer (byte, short,
int,) or a char
• It cannot be a floating point value (float or double)
• The implicit test condition in a switch statement is
equality
• You cannot perform relational checks with a switch
statement
The Switch statement(Contd…)
The general syntax of a switch statement is:
switch ( expression )
{
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
default:
statement
}
switch
and
case
are
reserved
words
If expression
matches value2,
control jumps
to here
The Switch statement(Contd…)
switch (option)
{
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
default:
otherCount++;
break;
}
The Switch statement(Contd…)
An example for switch statement:
Iteration Statements
• Also known as loops
• It allow a set of instructions to be executed
until a certain condition is met.
• The loop structures available in C are:
• For loop
• While loop
• Do while loop
The for Loop
• The syntax of for statement in C:
for (initialization expression;loop repetition condition;update expression)
{
statement
}
• The initialization expression set the initial value of the loop
control variable.
• The loop repetition condition test the value of the loop control
variable.
• The update expression update the loop control variable.
Nested Loops
• Nested loops consist of an outer loop with one or more
inner loops.
• e.g.,
for (i=1;i<=100;i++){
for(j=1;j<=50;j++){
…
}
}
• The above loop will run for 100*50 iterations.
5-22
Inner loop
Outer loop
For loop: An example
/*For loop to print even numbers below 100*/
for(i=2;i<100;i=i+2)
{
printf("%d,",i);
}
The While loop
• The syntax of do-while statement in C:
do
statement
while (loop repetition condition);
• The statement is first executed.
• If the loop repetition condition is true, the
statement is repeated.
• Otherwise, the loop is exited.
An Example of the do-while Loop
/* Find even number input */
do{
printf(“Enter a value: ”);
scanf(“%d”, &num);
}while (num % 2 !=0)
5-25
This loop will repeat if the user
inputs odd number.
Jump Statements
• Also called unconditional branching.
• It means transfer of control to a specified
statement.
• C has four statements that perform
unconditional branch
– Call/return
– Goto
– Break
– Continue
The return statement
• Used to return fro a function
• It returns to the point at which the function is
called.
• It can have a value with it.
• General form: return (expression);
• The expression is optional
• Function can have more than one return
statement, but will return at the first return
statement
The goto statement
• Allows to transfer control to any other
statement in the function.
• General form
label:statement
goto label;
The break statement
• It can be used to terminate a case in switch
statement.
• Can used to force immediate loop termination
by checking a condition
• Here, loop will terminate when i=10
for(i=0;i<=100;i++)
{
if(i==10)
{
break;
}
printf(“%d”,i);
}
The Continue Statement
• It allows to skip the current iteration and
hence wont run the remaining statements in
the loop.
for(i=0;i<=100;i++)
{
if(i==10)
{
break;
}
printf(“%d”,i);
}
• Here, loop will skip the printf statement when i=10 and incement i to 11
thank you
Doubts…?
Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
Contact Us

Más contenido relacionado

La actualidad más candente

Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++Neeru Mittal
 
Break and continue in C
Break and continue in C Break and continue in C
Break and continue in C vishnupriyapm4
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programmingAppili Vamsi Krishna
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programmingCHANDAN KUMAR
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statementRaj Parekh
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
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
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statementsrajshreemuthiah
 
10. switch case
10. switch case10. switch case
10. switch caseWay2itech
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c languageRai University
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C LanguageShaina Arora
 

La actualidad más candente (20)

Loops c++
Loops c++Loops c++
Loops c++
 
Increment and Decrement operators in C++
Increment and Decrement operators in C++Increment and Decrement operators in C++
Increment and Decrement operators in C++
 
C# Loops
C# LoopsC# Loops
C# Loops
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Break and continue in C
Break and continue in C Break and continue in C
Break and continue in C
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
SWITCH CASE STATEMENT IN C
SWITCH CASE STATEMENT IN CSWITCH CASE STATEMENT IN C
SWITCH CASE STATEMENT IN C
 
Control structure in c
Control structure in cControl structure in c
Control structure in c
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
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
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
C if else
C if elseC if else
C if else
 
10. switch case
10. switch case10. switch case
10. switch case
 
Strings
StringsStrings
Strings
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 

Similar a Control structures in C

Control statements anil
Control statements anilControl statements anil
Control statements anilAnil Dutt
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptxssuserfb3c3e
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxLikhil181
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C ProgrammingKamal Acharya
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxSKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptxLECO9
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsRai University
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statementsRai University
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statementsRai University
 

Similar a Control structures in C (20)

Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Control structure
Control structureControl structure
Control structure
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Condition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptxCondition Stmt n Looping stmt.pptx
Condition Stmt n Looping stmt.pptx
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Control structers in c
Control structers in cControl structers in c
Control structers in c
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
 

Más de baabtra.com - No. 1 supplier of quality freshers

Más de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Último

Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 

Último (20)

Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 

Control structures in C

  • 1.
  • 3. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 4. Flow of control • Unless specified, the order of statement execution is linear: one statement after another in sequence • A control structure is any mechanism that departs from the default straight-line execution.
  • 6. Selection statements • Used to choose which statement to be executed next • Therefore they are sometimes called conditional statements • Conditional statements give us the power to make basic decisions • The C conditional statements are the: – if statement – if-else statement – switch statement
  • 7. The if Statement The if statement has the following syntax: if ( condition ) statement; • if is a C reserved keyword. • The condition must be a boolean expression. It is evaluated to either true or false. • If the condition is true, the statement is executed. • If it is false, the statement is skipped.
  • 8. The if-else Statement • If the condition is true, statement1 is executed; if the condition is false, statement2 is execute if ( condition ) statement1; else statement2;
  • 9. Consider the following example, int grade=76; if ( grade >= 60 ) printf( "Passedn"); else printf( "Failedn"); The above code will produce an output: Passed Since the true statement only executes
  • 10. • An if statement which is within another if or else is called nested if. • Braces can be used to specify the if statement to which an else clause belongs Nested if statements
  • 11. Syntax of nested if: if(condition1) { if(condition2) do this; else //this else with condition2 { do this; and this; } } else //this else with condition1 do this; Note that in C an else statement always refers to the nearest if that is within the same block of else.
  • 12. An example for nested if: if(avg>=90) printf("Student %s gets an A grade",name); else if(avg>=80 &&avg<90) printf("Student %s gets a B grade",name); else if(avg>=70 &&avg<0) printf("Student %s gets a C grade",name); else if(avg>=60 &&avg<70) printf("Student %s gets a D grade",name); else if(avg<60) printf("Student %s failed"); Nested if (contd…)
  • 13. The Switch statement • The switch statement provides another way to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • Each case contains a value and a list of statements • The flow of control transfers to statement associated with the first case value that matches
  • 14. • Often a break statement is used as the last statement in each case's statement list • A break statement causes control to transfer to the end of the switch statement • If a break statement is not used, the flow of control will continue into the next case • Sometimes this may be appropriate, but often we want to execute only the statements associated with one case The Switch statement(Contd…)
  • 15. • A switch statement can have an optional default case • If the default case is present, control will transfer to it if no other case value matches • If there is no default case, and no other value matches, control falls through to the statement after the switch The Switch statement(Contd…)
  • 16. • The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int,) or a char • It cannot be a floating point value (float or double) • The implicit test condition in a switch statement is equality • You cannot perform relational checks with a switch statement The Switch statement(Contd…)
  • 17. The general syntax of a switch statement is: switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 default: statement } switch and case are reserved words If expression matches value2, control jumps to here The Switch statement(Contd…)
  • 18. switch (option) { case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: otherCount++; break; } The Switch statement(Contd…) An example for switch statement:
  • 19. Iteration Statements • Also known as loops • It allow a set of instructions to be executed until a certain condition is met. • The loop structures available in C are: • For loop • While loop • Do while loop
  • 20. The for Loop • The syntax of for statement in C: for (initialization expression;loop repetition condition;update expression) { statement } • The initialization expression set the initial value of the loop control variable. • The loop repetition condition test the value of the loop control variable. • The update expression update the loop control variable.
  • 21. Nested Loops • Nested loops consist of an outer loop with one or more inner loops. • e.g., for (i=1;i<=100;i++){ for(j=1;j<=50;j++){ … } } • The above loop will run for 100*50 iterations. 5-22 Inner loop Outer loop
  • 22. For loop: An example /*For loop to print even numbers below 100*/ for(i=2;i<100;i=i+2) { printf("%d,",i); }
  • 23. The While loop • The syntax of do-while statement in C: do statement while (loop repetition condition); • The statement is first executed. • If the loop repetition condition is true, the statement is repeated. • Otherwise, the loop is exited.
  • 24. An Example of the do-while Loop /* Find even number input */ do{ printf(“Enter a value: ”); scanf(“%d”, &num); }while (num % 2 !=0) 5-25 This loop will repeat if the user inputs odd number.
  • 25. Jump Statements • Also called unconditional branching. • It means transfer of control to a specified statement. • C has four statements that perform unconditional branch – Call/return – Goto – Break – Continue
  • 26. The return statement • Used to return fro a function • It returns to the point at which the function is called. • It can have a value with it. • General form: return (expression); • The expression is optional • Function can have more than one return statement, but will return at the first return statement
  • 27. The goto statement • Allows to transfer control to any other statement in the function. • General form label:statement goto label;
  • 28. The break statement • It can be used to terminate a case in switch statement. • Can used to force immediate loop termination by checking a condition
  • 29. • Here, loop will terminate when i=10 for(i=0;i<=100;i++) { if(i==10) { break; } printf(“%d”,i); }
  • 30. The Continue Statement • It allows to skip the current iteration and hence wont run the remaining statements in the loop.
  • 31. for(i=0;i<=100;i++) { if(i==10) { break; } printf(“%d”,i); } • Here, loop will skip the printf statement when i=10 and incement i to 11
  • 34. Want to learn more about programming or Looking to become a good programmer? Are you wasting time on searching so many contents online? Do you want to learn things quickly? Tired of spending huge amount of money to become a Software professional? Do an online course @ baabtra.com We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.
  • 35. Follow us @ twitter.com/baabtra Like us @ facebook.com/baabtra Subscribe to us @ youtube.com/baabtra Become a follower @ slideshare.net/BaabtraMentoringPartner Connect to us @ in.linkedin.com/in/baabtra Give a feedback @ massbaab.com/baabtra Thanks in advance www.baabtra.com | www.massbaab.com |www.baabte.com
  • 36. Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com Contact Us

Notas del editor

  1. There are 3 types of control structures in C. 1) Decision or conditional statements: if, if else, nested if, switch case 2) Looping statements: while, do while, for, nested for 3) Jumping statements: -> Conditional jumping: break, continue. -> Unconditional jumping: return, goto