branching in c

10
CONTROL CONSTRUCTS

Upload: prabhu-govind

Post on 13-Dec-2014

26 views

Category:

Education


0 download

DESCRIPTION

Branching in C For S Teacher

TRANSCRIPT

Page 1: Branching in C

CONTROL CONSTRUCTS

Page 2: Branching in C

Control ConstructsControl constructs are of two types

1.DECISION MAKING STATEMENTS

2.LOOPING STATEMENTS

Page 3: Branching in C

Control constructs areDecision making statements1.If statement2.If else statement3.Nested if else statement4.Switch caseLooping statements5.For loop6.While loop7.Do while loop

Page 4: Branching in C

If statement:

if (condition) statement; orif (condition) { compound statement; }

Page 5: Branching in C

Example of if statement:int i; printf ("Type in an integer"); scanf ("%ld", &i); if (i == 0) { printf ("The number was zero"); } if (i > 0) { printf ("The number was positive"); } if (i < 0) { printf ("The number was negative"); }

Page 6: Branching in C

If else statement:

if (condition) statement1; Else statement2; Orif (condition) { statements1 ;} Else { statements2 ;}

If - else

? else

if

Page 7: Branching in C

Example of if-else statement:int i; scanf ("%d",i); if (i > 0) { printf ("That number was positive!"); } else { printf ("That number was negative or zero!"); }

Page 8: Branching in C

Nested if else statement:

if ((cond.) && (cond.)) { statement;} ORif (cond.) { if (cond.) { statement; } }

Page 9: Branching in C

Example of nested-if statement:if ((i > 2) && (i < 4)) { printf ("i is three"); } or:if (i > 2) { if (i < 4) { printf ("i is three"); } }

Page 10: Branching in C

SWITCH & BREAK

switch (integer value) {

case 1: statement1; break;

/* optional line */ case 2: statement2;

break; /* optional line */ .... default: default

statement break; /* optional line */ }

Switch case statement: