chapter 5 program control
DESCRIPTION
CHAPTER 5, COMPUTER PROGRAMMING (DAE 20102)TRANSCRIPT
![Page 1: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/1.jpg)
COMPUTERPROGRAMMINGDAE 20102
CHAPTER 5PROGRAM CONTROL
![Page 2: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/2.jpg)
INTRODUCTION
• Control Structures=control the flow of execution in a program or function
• There are 3 control structures:-▫ Sequence structure▫ Selection structure▫ Repetition structure
![Page 3: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/3.jpg)
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
![Page 4: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/5.jpg)
CONDITION•Operator Precedence
Operator PrecedenceFunctions calls Highest
Lowest
! + - & (unary operators)
* / %
+ -
< <= >= >
== !=
&&
| |
=
![Page 6: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/6.jpg)
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”);
![Page 7: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/7.jpg)
The if Selection Structure•Flowchart of the preceding if statement
Grade >=55 printf (“passed”)True
False
End
Sel
ectio
n S
truc
ture
![Page 8: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/8.jpg)
The if Selection Structure
Notice that C uses == to test for equality, while it uses = to assign a value to a variable.
![Page 9: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/9.jpg)
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”
![Page 10: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/10.jpg)
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
![Page 11: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/11.jpg)
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”);
![Page 12: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/12.jpg)
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
![Page 13: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/13.jpg)
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”);
![Page 14: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/14.jpg)
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
![Page 15: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/15.jpg)
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
![Page 16: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/16.jpg)
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
![Page 17: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/17.jpg)
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
![Page 18: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/18.jpg)
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
![Page 19: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/19.jpg)
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
![Page 20: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/20.jpg)
Looping Statement•There are three types of looping statement
in C:-▫for statement▫do…while statement▫while statement
![Page 21: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/21.jpg)
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(“*”);
![Page 22: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/22.jpg)
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
![Page 23: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/23.jpg)
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
![Page 24: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/24.jpg)
do..while Statement•Syntax
dostatement
while (loop repetition condition);•Exampledostatus = scanf (“%d”,&num);
while (status!=0);
24
![Page 25: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/25.jpg)
while Statement•Syntax Format
while (loop repetition condition)statement
•Examplecount_star = 0;while (count_star < N) {printf (“*”);count_star = count_star+1;
}
25
![Page 26: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/26.jpg)
Programming C
26
![Page 27: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/27.jpg)
Programming C
27
![Page 28: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/28.jpg)
Programming C
28
![Page 29: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/29.jpg)
Programming C
29
![Page 30: Chapter 5 program control](https://reader033.vdocuments.us/reader033/viewer/2022061221/54bdae664a79595b5b8b4597/html5/thumbnails/30.jpg)
Programming C
30