PSPD Using C C Control Structures Topic Structure

















































- Slides: 49
PSPD Using C ‘C’ Control Structures
Topic & Structure of the lesson C Control Structures In this chapter you will learn about: • Types of Control Structures • Selection Structure • if-else statement • Condition Operator(? : ) • switch statement Sli de 2 of 51
Learning Outcomes C Control Structures If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams: Selection structure: Enables the selection of an option from several options. Repetitive structure: Enables a program to be repeated several times. Slide 3 of 51
Types of Control Structures C Control Structures § A control structure controls the flow of execution. § A combination of individual instructions into a with a single logical unit with one entry point and one exit point. § Three types: sequential, selection and repetition. Sli de 4 of 51
Types of Control Structures C Control Structures § Sequential flow is specified by a compound statement, consisting of group of statements in a block. (block is delimited by braces{}). { statement 1; statement 2; . . . statement; } Sli de 5 of 51
Flow chart -Sequence Structure C Control Structures Statement -1 Statement -2 Statement -n Sli de 6 of 51
Types of Control Structures C Control Structures § Selection structure allows choice for among alternative statements. § Repetition structure allows for an action to be repeated while some condition remains true. Sli de 7 of 51
Selection Structure C Control Structures § The selection structure allows a choice in what statement will be executed next. § C provides three types of selection structures. ü if statement ü if-else statement ü switch statement Sli de 8 of 51
if statement C Control Structures § The if selection structure either performs an action if a condition is true or skips the action if the condition is false. § The general form of the if statement is, if (expression) { statements } § For Example: - if (grade >= 60) printf(“Passed”); Sli de 9 of 51
if - else statement C Control Structures § The if-else selection structure either performs an action if a condition is true and performs a different action if the condition is false. § The general form of the if-else statement is, if (expression) { then-statements } else { else-statements } Sli de 10 of 51
if - else statement C Control Structures § For Example : if (grade >= 60) printf (“Passed”); else printf(“Failed”); Sli de 11 of 51
if - else statement C Control Structures § The Flow Chart representation is, false Grade>=60 Print “Failed” true Print “Passed” Sli de 12 of 51
if - else statement C Control Structures § if statements with compound statements if (ctri <= MAX_SAFE_CTRI) { printf(”Car %d: safen”, auto_id); safe++; } else { printf(”Car %d: unsafe”, auto_id); unsafe++; } Sli de 13 of 51
if - else statement C Control Structures Style: Where to put the braces? if (condition) { statements } or else { statements } if (condition) { statements } else { statements } Sli de 14 of 51
if - else statement § Removing common statements if (a < 0) { neg++; count++; scanf (”%d”, &c); } else { pos++; count++; scanf (”%d”, &c); } C Control Structures if (a < 0) neg++; else pos++; count++; scanf(”%d”, &c); Sli de 15 of 51
Nested if - else statement C Control Structures if (x > 0) pos++; else if (x < 0) neg++; else zero++; Sli de 16 of 51
Switch statement C Control Structures § The switch selection structure performs one of many different actions depending on the value of an expression. § Other wise called as multiway selection statement. § The general form of the switch statement is, switch (expression) { case val_1: case_1_statements; break; Sli de 17 of 51
Switch statement C Control Structures case val_2: case_2_statements; break; ……. case val_n: case_n_statements; break; default: default_statements; break; } Sli de 18 of 51
Switch statement C Control Structures §May only be used to test a constant integral expression. i. e. the value of the expression type may be of int or char only. §breaks must be used to terminate a branch. Sli de 19 of 51
Flow Chart C Control Structures Case a F Case b T T Case a action break Case b action break Case z action break F Case z T F Default action Sli de 20 of 51
Example - 1 C Control Structures /* To find odd or even*/ main() { int n=5; switch(n/2){ case 0 : printf(“Even Number”); break; case 1: printf(“Odd Number”); break; }} Sli de 21 of 51
Example-2 C Control Structures main() { char class; scanf(“%c”, &class); switch (class) { case ’B’: case ’b’: printf (”Battleshipn”); break; case ’C’: case ’c’: printf (”Cruisern”); break; Sli de 22 of 51
Example-2 C Control Structures case ’D’: case ’d’: printf (”Destroyern”); break; case ’F’: case ’f’: printf (”Frigaten”); break; default : printf (”Unknown ship class %cn”, class); }} Sli de 23 of 51
Repetition Structures C Control Structures §A loop is a group of instructions which is repeatedly executed while some condition remains true. § An infinite loops forever- must be avoided. § Three loops constructs: while, for and do-while § Two kinds of control: counter-controlled repetition and sentinel-controlled repetition. Sli de 24 of 51
while statement C Control Structures §The while statement is a pre-test condition controlled loop construct. § It has the form, while (expression) { statements } Sli de 25 of 51
Flow chart C Control Structures condition T action F Sli de 26 of 51
Example-1 C Control Structures count_star = 0; while (count_star < N){ printf(”*”); count_star++; } (or) count_star = 0; while (count_star++ < N) printf(”*”); Sli de 27 of 51
Example-2 C Control Structures int num = 1; /*declaration and*/ int total = 0; /*initialization*/ while (num <= 100) { total += num; num++; } Sli de 28 of 51
Counter controlled vs. Sentinel controlled C Control Structures § Use Counter-control repetition when the number of iterations are known. §Sentinel control repetition is more general. Sli de 29 of 51
Counter controlled repetition C Control Structures Example: #define N 10. . . total = 0; count = 1; while (count <= N) { printf (”Enter score: ”); scanf (”%d”, &score); total += score; } Sli de 30 of 51
Counter controlled repetition C Control Structures count++; } avg = (float) total / N; printf(”Average is %. 2 fn”, avg); Sli de 31 of 51
Sentinel controlled repetition C Control Structures #define SENTINEL -1. . . total = 0; count = 0; printf(”Enter score, -1 to end: ”); scanf (”%d”, &score); while (score != SENTINEL) { total += score; count++; Sli de 32 of 51
Sentinel controlled repetition C Control Structures printf(”Enter score, -1 to end: ”); scanf (”%d”, &score); } if (count) { avg = (float) total/count; printf(”Average is %. 2 fn”, avg); }else printf(”No scores were enteredn”); Sli de 33 of 51
for statement C Control Structures § The for statement is another pre-test condition-control loop structure. § It provides a more compact form for countercontrolled loops. § The general form is, for (initialization expression; loop condition; update expression) { statements; } Sli de 34 of 51
for statement C Control Structures Example: for (count_star = 0; /* init */ count_star < N; /* condition */ count_star++) /* update */ printf (”*”); Sli de 35 of 51
Flow Chart- for statement C Control Structures Initial value condition T statements Increment F Sli de 36 of 51
do-while statement C Control Structures §The do-while statement is a post-test condition control loop structure. §The loop body is executed at least once. §The general form of the do-while is, do { statements } while(loop condition); Sli de 37 of 51
do-while Examples §Example 1 : - C Control Structures count = 1; do { printf (”%d ”, count); } while (++count <= 10); §Example 2: do { printf(“Enter a letter A thru E”); scanf(“%c”, &letter); } while(letter< ‘A’ || letter >’E’); Sli de 38 of 51
Flow Chart--do-while statement C Control Structures action condition T F Sli de 39 of 51
break and continue statements C Control Structures § The break and continue statements are used to alter the flow of a control. § The break statement in a while, for, do-while or switch structure causes immediate exit from the structure. Sli de 40 of 51
break and continue statements C Control Structures /*Using the break in the for stt. */ main() { int x; for(x=1; x<=10; x++){ if (x == 5) OUTPUT: 1234 Broke out of loop at 5 break; printf(%d”, x); } printf(“n. Broke out of loop at x= %d”, x); } Sli de 41 of 51
break and continue statements C Control Structures §The continue statement in a while, for and do-while structure skips the remaining statements in the body, to the next iteration of the loop. §Example: /*Using continue in a for structure*/ main() { int x; Sli de 42 of 51
break and continue statements C Control Structures for(x=1; x<=10; x++){ if ( x == 5) OUTPUT: 1 2 3 4 6 7 8 9 10 continue; Used continue to skip printing printf(“%d”, x); the value 5 } printf(“n Used continue to”); printf(“skip printing the value 5”); } Sli de 43 of 51
SUMMARY C Control Structures § Specifying the order in which statements are to be executed in a computer programs is called program control. § A selection structure is used to choose among alternative courses of action. § The if selection structure executes an indicated action only when the condition is true. Sli de 44 of 51
SUMMARY C Control Structures § The if/else selection structure specifies separate actions to be executed when the condition is true and when the condition is false. § A repetition structure specifies than an action is to be repeated while some condition remains true. Sli de 45 of 51
SUMMARY C Control Structures § The Repetition statements are for, while and do/while. § The switch handles a series of decisions in which a particular variable/expression is tested. § The break statement, when executed in one of the repetition structures (for, while, do/while), causes immediate exit from the structure. Sli de 46 of 51
SUMMARY C Control Structures § The Continue statement, skips any remaining statements in the body of the structure, and proceeds with the next iteration of the loop. § Counter-controlled looping uses a variable as a counter to determine when a loop should terminate. § Sentinel values are generally used to control repetition when the precise number of repetitions is not known in advance and the loop includes statements that obtain data each time the loop is performed. Sli de 47 of 51
Quick Review Question C Control Structures • Using code explain the difference between sentinel control and counter control structures. Sli de 48 of 51
Next Session C Control Structures ‘C’ Arrays Sli de 49 of 51