Date post: | 13-Dec-2014 |
Category: |
Education |
Upload: | prabhu-govind |
View: | 25 times |
Download: | 0 times |
CONTROL CONSTRUCTS
Control ConstructsControl constructs are of two types
1.DECISION MAKING STATEMENTS
2.LOOPING STATEMENTS
Control constructs areDecision making statements1.If statement2.If else statement3.Nested if else statement4.Switch caseLooping statements5.For loop6.While loop7.Do while loop
If statement:
if (condition) statement; orif (condition) { compound statement; }
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"); }
If else statement:
if (condition) statement1; Else statement2; Orif (condition) { statements1 ;} Else { statements2 ;}
If - else
? else
if
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!"); }
Nested if else statement:
if ((cond.) && (cond.)) { statement;} ORif (cond.) { if (cond.) { statement; } }
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"); } }
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: