Control Structure in C

15
Made By: Neel Pradip Shah (13CP612) (130110107048) 1

Transcript of Control Structure in C

Page 1: Control Structure in C

Made By:Neel Pradip Shah (13CP612)(130110107048)

1

Page 2: Control Structure in C

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

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

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

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

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

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

An Entry Controlled loop

Syntax while(test condition){

Body Of the Loop }

8

Page 9: Control Structure in C

An Exit Controlled loop

Syntaxdo{

set of statements}while(condition);

9

Page 10: Control Structure in C

An Entry Controlled Loop

Syntax

for(initialization ; test-condition ; increment) {

BODY OF THE LOOP }

10

Page 11: Control Structure in C

for(initialization ; test-condition ; increment)

{

BODY OF THE LOOP

}

11

Page 12: Control Structure in C

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

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

ANSI C By E.Balagurusamy Programming With C By Byron S Gottfried

14

Page 15: Control Structure in C

15