COMPUTERPROGRAMMINGDAE 20102
CHAPTER 5PROGRAM CONTROL
INTRODUCTION
• Control Structures=control the flow of execution in a program or function
• There are 3 control structures:-▫ Sequence structure▫ Selection structure▫ Repetition structure
INTRODUCTIONTypes of selection structure:-i. if-selection (single-selection)=selects/perform an
action if a condition is true or skips the action if the condition is false
ii. If-else selection (double-selection)=performs an action if a condition is true and performs a different action if condition is false
iii. Switch selection (multiple-selection)=performs one of many different actions depending on the value of an expression
CONDITION
•Relational and Equality Operators
Operator Meaning Type
< Less than Relational
> Greater than Relational
<= Less than or equal to Relational
>= Greater than or equal to
Relational
== Equal to Equality
!= Not equal to Equality
CONDITION•Operator Precedence
Operator PrecedenceFunctions calls Highest
Lowest
! + - & (unary operators)
* / %
+ -
< <= >= >
== !=
&&
| |
=
The if Selection Structure•Used to choose among alternative courses
of action•If the algorithm/flow chart have a diamond
symbols, then it will used control structures•Example: The pseudo-code statement
If student’s grade is greater than or equal to 55print “passed”
if (grade>=55)printf (“Passed\n”);
The if Selection Structure•Flowchart of the preceding if statement
Grade >=55 printf (“passed”)True
False
End
Sel
ectio
n S
truc
ture
The if Selection Structure
Notice that C uses == to test for equality, while it uses = to assign a value to a variable.
The if-else Selection Structure•Performs an indicated action when the
condition is true; otherwise the action is skipped
•Pseudo-code statement:-
if student’s grade is greater than or equal to 55
print “passed”else
print “failed”
The if-else Selection Structure•Syntax for if-else selection:-
if (condition){
statement T;}else{
statement F;}
if (result>=55){
printf(“Passed\n”);printf(“Congratulations\n”);
}else{
printf(“Failed\n”);ptintf(“Good luck in the resits\n”);
}
Compound Statement
The if-else Selection Structure•Syntax for multi way decision based on
several conditions:-
if (condition 1)statement 1;
else if (condition 2)statement 2;
else if (condition n)statement n;
elsestatement a;
if (result>=80)printf(“Passed: Grade A\n”);
else if (result>=70)printf(“Passed: Grade B\n”);
else if (result>=55)printf(“Passed: Grade C\n”);
elseprintf(“Failed\n”);
The Nested if-else Selection Structure
• if-else statement may contain any single or compound statement including another if-else statement
if (hours > 40)if (hours >60)
printf(“double time”);else
printf(“time is half”); if (hours > 40)if (hours >60)
printf(“double time”);else
printf(“time is half”);
Which if statement should be paired with else statement
The Nested if-else Selection Structure
•Braces can be used to change the matching above
if (hours > 40) {if (hours >60)
printf(“double time”);}
else printf(“time is half”);
The Nested if-else Selection Structure
•Exampleif (road_status==0) if (temp>0) {
printf(“Wet roads ahead\n”);
printf(“Stopping time doubled\n”); } else {
printf(“Icy roads ahead\n”); printf(“Stopping time quadrupled\n”);
}else
printf(“Drive carefully!\n”);
road_status is ‘s’
Temp>0
Drive carefully message
Icy road message
True
False False
True
The switch statement• Alternative to the if-else chain• Compared the value of an integer expression to a
specific value in order to select a set of instructions to be executed from many possible alternatives
• Also another form of multi-way decision, but can only be used in certain cases where:-
▫ If only 1 variable is tested, all branches must depend on the value of that variable. Variable must be of an integral type (int ,long, short or char)
▫ Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally used to trap unspecified cases
The switch statement•General syntax of switch
statementswitch (expression){case value_1;
statement A;statement B;break;
case value_2;statement C;statement D;break;
case value_n;statement Y;statement Z;break;
default;statement W;statement X;
}
• Break statement is used to exit from a loop or a switch
• With loops, break can be used to force an early exit from the loop or to implement a loop with a test to exit in the middle of the loop body
The continue Statement•Similar to break but encountered less
frequently•Only works within loops where its effect is
to force an immediate jump to the loop control statement
The goto Statement•goto loop construct uses two elements; first
is a label and second is colon (:).•A label marks a location within a program to
which program execution can jump•Label must be identifiers•Normally the goto statement is executed if
some logical test is true•goto statement can be avoided by using
return, break or continue statement•goto statement generally considered bad
programming practices
The goto Statement•goto coding
example#include<stdio.h>int main (){
int count=1;mula:
if (count>10)goto akhir;
printf ("%d",count);++count;goto mula;
akhir:printf("Akhir/n");
return 0;}
Label
Label
Looping Statement•There are three types of looping statement
in C:-▫for statement▫do…while statement▫while statement
for Statement• There are three loop control components
▫ Initialization of the loop control variable▫ Test of the loop repetition condition, and▫ Change (update) of the loop control variable
• Syntax format for (initialization expression;
loop repetition condition; update expression) statement
• Examplefor (count_star=0;count_star <N;count_star+=1)
printf(“*”);
22
#include<stdio.h>#define CBEGIN 10#define CLIMIT -5#define CSTEP 5
int main (void){
int celcius;double fahrenheit;printf(“ Celcius Fahrenheit”\n);
for (celcius = CBEGIN;celcius >= CLIMIT;celcius -= CSTEP)
{fahrenheit = 1.8 * celcius + 32.0;printf(“%6c%3d%8c%7.2f\n”, ‘ ‘,celcius, ‘
‘,fahrenheit);}return (0);
}
for Statement
do..while Statement
•Both the for statement and the while statement evaluate a loop repetition condition before the first evaluation of the loop body
•do..while statement evaluate on the pretest desire then do the looping
23
do..while Statement•Syntax
dostatement
while (loop repetition condition);•Exampledostatus = scanf (“%d”,&num);
while (status!=0);
24
while Statement•Syntax Format
while (loop repetition condition)statement
•Examplecount_star = 0;while (count_star < N) {printf (“*”);count_star = count_star+1;
}
25
Programming C
26
Programming C
27
Programming C
28
Programming C
29
Programming C
30