SlideShare a Scribd company logo
1 of 19
 Decisions – if then else
 More decisions – switch
 Loops- while, do while, for
 Keyword break
 Keyword continue
If I’m bigger than him and I’m
hungry….
then it’s mealtime
else, if he’s bigger than me…
hope he doesn’t look hungry
else, if we’re the same else size
Wait to grow bigger
 Parentheses surround the best
 One statement becomes the “then part’
 If more are required, braces must be read
scanf(“%i”, &i);
If(i > 0)
printf(“a positive number was enteredn”);
If(i < 0) {
printf(“a negative number was enteredn”);
i = -1;
}
 A semicolon after the condition, form as “do
nothing’ statement
printf(“input an integer: “);
scanf(“%i”, &j);
If(j > 0);
printf(“a positive number was enteredn”);
input an integer: -6
a positive number was entered
 An optional else may be added
 One statement by default, if more are required,
braces must be used
if(i > 0)
printf(“i is positiven”);
else
printf(“i is negativen”);
if(i > 0)
printf(“i is positiven”);
else {
printf(“i is negativen”);
i = -1;
}
 Else associated with the nearest if
int i = 100;
if(i > 0)
if (i > 1000)
printf(“i is bign”);
else
printf(“i is reasonablen”);
int i = 100;
if(i > 0) {
if (i > 1000)
printf(“i is bign”);
} else
printf(“i is negativen”);
i is
reasonable
i is negative
 C supports a switch for multi-way decision
making
switch (c) {
case ’a’ ; case ‘A’:
printf(“area = %.2fn”, r * r * r pi);
break;
case ‘c’: case ‘C’:
printf(“circumference = %.2fn” , 2 * r * pi);
case ‘q’:
printf(“quit option chosenn”);
default:
printf(“unknown option chosenn”);
break;
}
 Only integral constants may be tested
 If no condition matches, the default is executed
 If no default, nothing is done (not an error)
 The break is important
float f;
switch (f) {
case 2:
…..
switch (i) {
case 2 * j:
…..
i = 3;
switch (i) {
case 3: printf(“i = 3n”);
case 2: printf(“i = 2n”);
case 1: printf(“i= 1n”);
}
i = 3
i = 2
i = 1
printf (“on the “) ;
switch (i) {
case 1: printf(“1st”); break;
case 2: printf(“2nd”); break;
case 3: printf(“3rd”); break;
default: printf(“%ith”,i); break;
}
printf (“day of Christmas my true love sent to me”);
switch (i) {
case 12: printf(“twelve lords a leaping,”);
case 11: printf(“eleven ladies dancing,”);
case 10: printf(“ten pipers piping,”);
case 9: printf(“nine drummers drumming,”);
case 8: printf(“eight maids a milking,”);
case 7: printf(“seven swams a swimming,”);
case 6: printf(“six geese a laying,”);
case 5: printf(“five gold rings,”);
case 4: printf(“four calling birds,”);
case 3: printf(“three french hens,”);
case 2: printf(“two turtle doves and ”);
case 1: printf(“a patridge in a pear treen”);
}
• The simplest C loop is the while
• Parentheses must surround the condition
• One statement forms the body of the loop
• Braces must be added if more statements
are to be executed
int j = 5;
while (j > 0)
printf (“j = %in”, j--);
while (j > 0) {
printf(“j = %in”, j);
j --;
}
j =
5
j =
4
j =
3
j =
2
j =
1
 A semicolon placed after the condition forms a
body that does nothing
 Sometimes an empty loop body is required
int j = 5;
while (j > 0 );
printf (“j = %in”, j --);
program disappears into an
infinite loop
int c, j;
while (scanf( “%i”, &j) ! = 1
while ( (c = getchar() )! = ‘n’)
;
placing semicolon on the
line below makes the
intention obvious
 Remember to get the condition the right way
around!
int j = 5;
printf (‘startn”);
while (j == 0)
printf (“j = %in”, j--);
printf (“endn”);
start
end
user probably intends
“until j is equal to zero”,
however this is NOT the
way to write it
 do while guarantees execution at least once
int j = 5 ;
printf (“startn”);
do
printf (“j = %in”, j--);
while (j > 0);
printf (“stopn”);
start
j = 5
j = 4
j = 3
j = 2
j = 1
Stop
int j = -10 ;
printf (“startn”);
do {
printf (“j = %in”, j);
j--;
} while (j > 0);
printf (“stopn”);
start
j = -10
stop
 for encapsulates the essential elements of a
loop into one statement
for (initial – part; while- condition; update-
part)
body;
int j;
for (j = 5; j > 0; j--)
printf(“j = %in”, j);
j = 5 odd
j = 4 even
j = 3 odd
j = 2 even
j = 1 odd
for (j = 5; j > 0; j--) {
printf(“j = %i”, j);
printf(“%sn”, ((j%2)==0) ? “even” : “odd”);
}
j = 5
j = 4
j = 3
j = 2
j = 1
 Remember to get the for condition the right way
around (it is really a while condition)
int j;
printf (“startn”);
for (j = 5; j == 0; j--)
printf(“j = %in”, j);
printf (“endn”);
start
end
user probably
intends “until j is equal
to zero”,
however this is NOT the
way to write it
either!
 Unlike some languages, the for loop is not
restricted to stepping up or down by 1
#include <math.h.
int main(void)
{
double angle;
for(angle – 0.0; angle < 3.14159; angle += 0.2)
printf(“sine of %.11f is %.21fn”, angle, sin(angle));
return 0;
}
 The initial and update parts may contain
multiple comma separated statements
 the initial, condition and update parts may
contain no statements at all!
int i, j, k;
for (i = 0, j = 5, k = -1; i < 10; i++, j++,
k--)
for (; i < 10; i++, j++, k--)
for (; i < 10;)
for (; ;)
use of a while loop
would be clearer here!
creates an infinite loop
 The break keyword forces immediate exit from
the nearest enclosing loop
 use in moderation!
for (; ;) {
printf (“type an int: “);
if (scanf (“%i”, &j) ==1)
break;
while ( (c = getchar() ) != ‘n’)
;
}
printf (“j = %n”, j);
type an int: an int
type an int: no
type an int: 16
j = 16
if scanf returns 1,
jump out of the loop
 The continue keyword forces the next iteration
of the nearest enclosing loop
 use in moderation!
for (j = 1; j <=10; j++) {
if (j % 3 ==0 )
continue;
printf (“ j = %in”, j);
}
j= 1
j= 2
j =4
j= 5
j= 7
j= 8
j=
10
if j is exactly divisible
by 3, skip
 If(then) else – watch the semicolons
 switch can test integer values
 while, do while, for – watch the semicolons
again
 break
 continue else…….
I want to be a tomato
!

More Related Content

What's hot

Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Abou Bakr Ashraf
 
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’a
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’aTen Typ tak ma - O systemach typów na przykładzie TypeScript’a
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’aArtur Skowroński
 
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...Artur Skowroński
 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2alish sha
 
C++,logical statements and assighnment statement
C++,logical statements and assighnment statementC++,logical statements and assighnment statement
C++,logical statements and assighnment statementAzeem Mumtaz
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5Rumman Ansari
 
cafeteria info management system
cafeteria info management systemcafeteria info management system
cafeteria info management systemGaurav Subham
 
Introduction to Programming @ NTHUEEECamp 2015
Introduction to Programming @ NTHUEEECamp 2015Introduction to Programming @ NTHUEEECamp 2015
Introduction to Programming @ NTHUEEECamp 2015淳佑 楊
 
Ejercicios Scilab Completo
Ejercicios Scilab CompletoEjercicios Scilab Completo
Ejercicios Scilab CompletoRicardo Grandas
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional StatementDeepak Singh
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programmingPrabhu Govind
 

What's hot (19)

Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
 
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’a
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’aTen Typ tak ma - O systemach typów na przykładzie TypeScript’a
Ten Typ tak ma - O systemach typów na przykładzie TypeScript’a
 
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...
Artur Skowroński – Ten Typ tak ma - O systemach typów na przykładzie TypeScri...
 
05 Jo P May 07
05 Jo P May 0705 Jo P May 07
05 Jo P May 07
 
Vcs8
Vcs8Vcs8
Vcs8
 
Answer unit4.4.1
Answer unit4.4.1Answer unit4.4.1
Answer unit4.4.1
 
Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2Dti2143 chap 4 control statement part 2
Dti2143 chap 4 control statement part 2
 
บทที่ 3
บทที่ 3บทที่ 3
บทที่ 3
 
C++,logical statements and assighnment statement
C++,logical statements and assighnment statementC++,logical statements and assighnment statement
C++,logical statements and assighnment statement
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
 
cafeteria info management system
cafeteria info management systemcafeteria info management system
cafeteria info management system
 
Trabajo Scilab
Trabajo ScilabTrabajo Scilab
Trabajo Scilab
 
Taller De Scilab
Taller De ScilabTaller De Scilab
Taller De Scilab
 
Introduction to Programming @ NTHUEEECamp 2015
Introduction to Programming @ NTHUEEECamp 2015Introduction to Programming @ NTHUEEECamp 2015
Introduction to Programming @ NTHUEEECamp 2015
 
Ejercicios Scilab Completo
Ejercicios Scilab CompletoEjercicios Scilab Completo
Ejercicios Scilab Completo
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
Menu vb
Menu vbMenu vb
Menu vb
 
Intro to c programming
Intro to c programmingIntro to c programming
Intro to c programming
 

Viewers also liked

The different rhinoplasty procedures
The different rhinoplasty proceduresThe different rhinoplasty procedures
The different rhinoplasty proceduresHealth First
 
Tension fabric-printing-inspiration-guide
Tension fabric-printing-inspiration-guideTension fabric-printing-inspiration-guide
Tension fabric-printing-inspiration-guideexhibithire
 
Bespoke glasses | Cool Eyewear
Bespoke glasses | Cool EyewearBespoke glasses | Cool Eyewear
Bespoke glasses | Cool EyewearMono Qool
 
VGIPM_Conference_CTA_2013_Abstracts[1]
VGIPM_Conference_CTA_2013_Abstracts[1]VGIPM_Conference_CTA_2013_Abstracts[1]
VGIPM_Conference_CTA_2013_Abstracts[1]Carlos Castilho
 
The many hidden causes of hip pain
The many hidden causes of hip painThe many hidden causes of hip pain
The many hidden causes of hip painHealth First
 
The 8 different sub disciplines of urology
The 8 different sub disciplines of urologyThe 8 different sub disciplines of urology
The 8 different sub disciplines of urologyHealth First
 
exhibitions,event planning,designers,events management,companies,contractors
exhibitions,event planning,designers,events management,companies,contractorsexhibitions,event planning,designers,events management,companies,contractors
exhibitions,event planning,designers,events management,companies,contractorsexhibithire
 

Viewers also liked (12)

Computer Programming- Lecture 11
Computer Programming- Lecture 11Computer Programming- Lecture 11
Computer Programming- Lecture 11
 
The different rhinoplasty procedures
The different rhinoplasty proceduresThe different rhinoplasty procedures
The different rhinoplasty procedures
 
Tension fabric-printing-inspiration-guide
Tension fabric-printing-inspiration-guideTension fabric-printing-inspiration-guide
Tension fabric-printing-inspiration-guide
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
 
Bespoke glasses | Cool Eyewear
Bespoke glasses | Cool EyewearBespoke glasses | Cool Eyewear
Bespoke glasses | Cool Eyewear
 
VGIPM_Conference_CTA_2013_Abstracts[1]
VGIPM_Conference_CTA_2013_Abstracts[1]VGIPM_Conference_CTA_2013_Abstracts[1]
VGIPM_Conference_CTA_2013_Abstracts[1]
 
C function
C functionC function
C function
 
The many hidden causes of hip pain
The many hidden causes of hip painThe many hidden causes of hip pain
The many hidden causes of hip pain
 
Coper in C
Coper in CCoper in C
Coper in C
 
The 8 different sub disciplines of urology
The 8 different sub disciplines of urologyThe 8 different sub disciplines of urology
The 8 different sub disciplines of urology
 
exhibitions,event planning,designers,events management,companies,contractors
exhibitions,event planning,designers,events management,companies,contractorsexhibitions,event planning,designers,events management,companies,contractors
exhibitions,event planning,designers,events management,companies,contractors
 
SGI. PROFILE
SGI. PROFILESGI. PROFILE
SGI. PROFILE
 

Similar to Control flow in c

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For LoopSukrit Gupta
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.comGreen Ecosystem
 
LET US C (5th EDITION) CHAPTER 4 ANSWERS
LET US C (5th EDITION) CHAPTER 4 ANSWERSLET US C (5th EDITION) CHAPTER 4 ANSWERS
LET US C (5th EDITION) CHAPTER 4 ANSWERSKavyaSharma65
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptSanjjaayyy
 
Chap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptxChap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptxFakhriyArif
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxSmitaAparadh
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C LanguageRAJWANT KAUR
 
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
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptxishaparte4
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin cVikash Dhal
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 

Similar to Control flow in c (20)

control statement
control statement control statement
control statement
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
3 flow
3 flow3 flow
3 flow
 
L3 control
L3 controlL3 control
L3 control
 
Learning C programming - from lynxbee.com
Learning C programming - from lynxbee.comLearning C programming - from lynxbee.com
Learning C programming - from lynxbee.com
 
LET US C (5th EDITION) CHAPTER 4 ANSWERS
LET US C (5th EDITION) CHAPTER 4 ANSWERSLET US C (5th EDITION) CHAPTER 4 ANSWERS
LET US C (5th EDITION) CHAPTER 4 ANSWERS
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
Chap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptxChap7_b Control Statement Looping (3).pptx
Chap7_b Control Statement Looping (3).pptx
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Chapter06.PPT
Chapter06.PPTChapter06.PPT
Chapter06.PPT
 
Practical File of C Language
Practical File of C LanguagePractical File of C Language
Practical File of C Language
 
Looping
LoopingLooping
Looping
 
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
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Presentation1
Presentation1Presentation1
Presentation1
 

More from thirumalaikumar3

More from thirumalaikumar3 (7)

Data type in c
Data type in cData type in c
Data type in c
 
C basics
C   basicsC   basics
C basics
 
Structure c
Structure cStructure c
Structure c
 
String c
String cString c
String c
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
 
Data type2 c
Data type2 cData type2 c
Data type2 c
 

Recently uploaded

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 

Recently uploaded (20)

Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 

Control flow in c

  • 1.  Decisions – if then else  More decisions – switch  Loops- while, do while, for  Keyword break  Keyword continue If I’m bigger than him and I’m hungry…. then it’s mealtime else, if he’s bigger than me… hope he doesn’t look hungry else, if we’re the same else size Wait to grow bigger
  • 2.  Parentheses surround the best  One statement becomes the “then part’  If more are required, braces must be read scanf(“%i”, &i); If(i > 0) printf(“a positive number was enteredn”); If(i < 0) { printf(“a negative number was enteredn”); i = -1; }
  • 3.  A semicolon after the condition, form as “do nothing’ statement printf(“input an integer: “); scanf(“%i”, &j); If(j > 0); printf(“a positive number was enteredn”); input an integer: -6 a positive number was entered
  • 4.  An optional else may be added  One statement by default, if more are required, braces must be used if(i > 0) printf(“i is positiven”); else printf(“i is negativen”); if(i > 0) printf(“i is positiven”); else { printf(“i is negativen”); i = -1; }
  • 5.  Else associated with the nearest if int i = 100; if(i > 0) if (i > 1000) printf(“i is bign”); else printf(“i is reasonablen”); int i = 100; if(i > 0) { if (i > 1000) printf(“i is bign”); } else printf(“i is negativen”); i is reasonable i is negative
  • 6.  C supports a switch for multi-way decision making switch (c) { case ’a’ ; case ‘A’: printf(“area = %.2fn”, r * r * r pi); break; case ‘c’: case ‘C’: printf(“circumference = %.2fn” , 2 * r * pi); case ‘q’: printf(“quit option chosenn”); default: printf(“unknown option chosenn”); break; }
  • 7.  Only integral constants may be tested  If no condition matches, the default is executed  If no default, nothing is done (not an error)  The break is important float f; switch (f) { case 2: ….. switch (i) { case 2 * j: ….. i = 3; switch (i) { case 3: printf(“i = 3n”); case 2: printf(“i = 2n”); case 1: printf(“i= 1n”); } i = 3 i = 2 i = 1
  • 8. printf (“on the “) ; switch (i) { case 1: printf(“1st”); break; case 2: printf(“2nd”); break; case 3: printf(“3rd”); break; default: printf(“%ith”,i); break; } printf (“day of Christmas my true love sent to me”); switch (i) { case 12: printf(“twelve lords a leaping,”); case 11: printf(“eleven ladies dancing,”); case 10: printf(“ten pipers piping,”); case 9: printf(“nine drummers drumming,”); case 8: printf(“eight maids a milking,”); case 7: printf(“seven swams a swimming,”); case 6: printf(“six geese a laying,”); case 5: printf(“five gold rings,”); case 4: printf(“four calling birds,”); case 3: printf(“three french hens,”); case 2: printf(“two turtle doves and ”); case 1: printf(“a patridge in a pear treen”); }
  • 9. • The simplest C loop is the while • Parentheses must surround the condition • One statement forms the body of the loop • Braces must be added if more statements are to be executed int j = 5; while (j > 0) printf (“j = %in”, j--); while (j > 0) { printf(“j = %in”, j); j --; } j = 5 j = 4 j = 3 j = 2 j = 1
  • 10.  A semicolon placed after the condition forms a body that does nothing  Sometimes an empty loop body is required int j = 5; while (j > 0 ); printf (“j = %in”, j --); program disappears into an infinite loop int c, j; while (scanf( “%i”, &j) ! = 1 while ( (c = getchar() )! = ‘n’) ; placing semicolon on the line below makes the intention obvious
  • 11.  Remember to get the condition the right way around! int j = 5; printf (‘startn”); while (j == 0) printf (“j = %in”, j--); printf (“endn”); start end user probably intends “until j is equal to zero”, however this is NOT the way to write it
  • 12.  do while guarantees execution at least once int j = 5 ; printf (“startn”); do printf (“j = %in”, j--); while (j > 0); printf (“stopn”); start j = 5 j = 4 j = 3 j = 2 j = 1 Stop int j = -10 ; printf (“startn”); do { printf (“j = %in”, j); j--; } while (j > 0); printf (“stopn”); start j = -10 stop
  • 13.  for encapsulates the essential elements of a loop into one statement for (initial – part; while- condition; update- part) body; int j; for (j = 5; j > 0; j--) printf(“j = %in”, j); j = 5 odd j = 4 even j = 3 odd j = 2 even j = 1 odd for (j = 5; j > 0; j--) { printf(“j = %i”, j); printf(“%sn”, ((j%2)==0) ? “even” : “odd”); } j = 5 j = 4 j = 3 j = 2 j = 1
  • 14.  Remember to get the for condition the right way around (it is really a while condition) int j; printf (“startn”); for (j = 5; j == 0; j--) printf(“j = %in”, j); printf (“endn”); start end user probably intends “until j is equal to zero”, however this is NOT the way to write it either!
  • 15.  Unlike some languages, the for loop is not restricted to stepping up or down by 1 #include <math.h. int main(void) { double angle; for(angle – 0.0; angle < 3.14159; angle += 0.2) printf(“sine of %.11f is %.21fn”, angle, sin(angle)); return 0; }
  • 16.  The initial and update parts may contain multiple comma separated statements  the initial, condition and update parts may contain no statements at all! int i, j, k; for (i = 0, j = 5, k = -1; i < 10; i++, j++, k--) for (; i < 10; i++, j++, k--) for (; i < 10;) for (; ;) use of a while loop would be clearer here! creates an infinite loop
  • 17.  The break keyword forces immediate exit from the nearest enclosing loop  use in moderation! for (; ;) { printf (“type an int: “); if (scanf (“%i”, &j) ==1) break; while ( (c = getchar() ) != ‘n’) ; } printf (“j = %n”, j); type an int: an int type an int: no type an int: 16 j = 16 if scanf returns 1, jump out of the loop
  • 18.  The continue keyword forces the next iteration of the nearest enclosing loop  use in moderation! for (j = 1; j <=10; j++) { if (j % 3 ==0 ) continue; printf (“ j = %in”, j); } j= 1 j= 2 j =4 j= 5 j= 7 j= 8 j= 10 if j is exactly divisible by 3, skip
  • 19.  If(then) else – watch the semicolons  switch can test integer values  while, do while, for – watch the semicolons again  break  continue else……. I want to be a tomato !