Control Structure in C
Transcript of Control Structure in C
![Page 1: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/1.jpg)
Made By:Neel Pradip Shah (13CP612)(130110107048)
1
![Page 2: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/2.jpg)
There are some situations where one may have to
Change the order of execution based on certain conditions
OR Repeat a group of statements until certain
conditions are satisfied.
2
![Page 3: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/3.jpg)
For this, C supports some statements which are capable of decision making, they are:
1. If statement2. Switch statement3. Conditional Operator4. Goto Statement5. While loop6. Do-While loop7. For loop
3
![Page 4: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/4.jpg)
Syntax
if(test expression){
statement Block;
} statement X;
Test expression ?
Statement - block
Statement - X
Next Statement
TRUE
FALSE
ENTRY
4
![Page 5: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/5.jpg)
Syntax
if(test expression){ statement
Block(true);}else{ statement
Block(false);} statement X;
Test expression ?
Statement - block
Statement - X
TRUE
ENTRY
Statement - block
FALSE
5
![Page 6: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/6.jpg)
Syntax
switch(expression){ case value-1: block-1; break;case value-2: block-2; break;……………..……………..default block; break;}Statement - X
6
![Page 7: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/7.jpg)
It is used for an unconditional jumpSyntax(forward jump) (backward jump)goto label; label:…………….. Statement;…………….. ………………Label: ………………Statement; goto label;
Programmers avoid using this As this statement makes logic complicated and
debugging is difficult.
7
![Page 8: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/8.jpg)
An Entry Controlled loop
Syntax while(test condition){
Body Of the Loop }
8
![Page 9: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/9.jpg)
An Exit Controlled loop
Syntaxdo{
set of statements}while(condition);
9
![Page 10: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/10.jpg)
An Entry Controlled Loop
Syntax
for(initialization ; test-condition ; increment) {
BODY OF THE LOOP }
10
![Page 11: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/11.jpg)
for(initialization ; test-condition ; increment)
{
BODY OF THE LOOP
}
11
![Page 12: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/12.jpg)
Entry
True
False
Entry
False
True
Test Condition
Test Condition
Body of The loop
Body of The loop
12
![Page 13: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/13.jpg)
a=10; for(a=10; a!=0; a--)a=10; for(a=10; a!=0; a--)while(a != 0)while(a != 0) { {{ printf(“%d”,a);{ printf(“%d”,a);
}}printf(“%d”,a);printf(“%d”,a);
a--;a--;} }
a=10;a=10;dodo{ OUTPUT : 10987654321{ OUTPUT : 10987654321 Printf(“%d”,a);Printf(“%d”,a); a--;a--;}}while(a!=0)while(a!=0)
13
![Page 14: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/14.jpg)
ANSI C By E.Balagurusamy Programming With C By Byron S Gottfried
14
![Page 15: Control Structure in C](https://reader030.fdocuments.us/reader030/viewer/2022032616/55a669a31a28abeb7e8b462e/html5/thumbnails/15.jpg)
15