SlideShare a Scribd company logo
1 of 19
Download to read offline
 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

Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipKarl Donert
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEMISSRITIMABIOLOGYEXP
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenship
 
Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...Introduction to Research ,Need for research, Need for design of Experiments, ...
Introduction to Research ,Need for research, Need for design of Experiments, ...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 

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 !