0% found this document useful (0 votes)
75 views48 pages

PPT4 - COMP6112 - Program Control Selection - Repetition - R0

This document discusses program control structures in C including selection and repetition. For selection, it covers if, if-else, and switch-case statements. For repetition, it covers for, while, and do-while loops. It also discusses break and continue statements, goto and labels, and different types of errors like compile-time, link-time, run-time, and logical errors. Examples are provided for each control structure.

Uploaded by

Dimas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views48 pages

PPT4 - COMP6112 - Program Control Selection - Repetition - R0

This document discusses program control structures in C including selection and repetition. For selection, it covers if, if-else, and switch-case statements. For repetition, it covers for, while, and do-while loops. It also discusses break and continue statements, goto and labels, and different types of errors like compile-time, link-time, run-time, and logical errors. Examples are provided for each control structure.

Uploaded by

Dimas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

COMP6112 – Algorithm and

Programming
Week 4 - Program Control: Selection &
Repetition
Sub Topics
Program Control: Selection & Repetition
• Selection
• Repetition
• Break Vs. Continue
• Go to and Label
• Error Type
SELECTION
Selection Definition
 In an algorithm implementation, an instruction
or block of instructions may be executed (or
not) with certain predetermined condition

 Syntax:
– if
– if-else
– switch-case
Selection : if
Syntax :
if (boolean expression) statement;
or
if (boolean expression) {
statement1;
statement2; Block of statements
……
}

If boolean expression resulting in True, then a statement or


some statements will be executed.
Selection : if
• Flow Chart of IF Statement

false true

condition

statements
Selection : if-else
Syntax :
if (boolean expression) statement1;
else statement2;
or If boolean
if (boolean expression){ expression
statement1; resulting in TRUE,
Block
then statement1 or
statement2; statement 1
block statement1
…… will be executed, if
} FALSE then
else { statement2 or
statement3; Block block statement2
statement4; statement 2 be executed.

}
Selection : if-else
Flow Chart of IF-ELSE Statement

false true

condition

statements 2 statements 1
Selection : Nested-if
• Nested selection occurs when the word IF appears more than
once within IF statement.
Syntax :
if (boolean expression) statement1;
if (boolean expression) statement2;
if (boolean expression) statement3;
or
if (boolean expression) statement1;
else
if (boolean expression) statement2;
else
if (boolean expression) statement3;
Program Examples Using IF

• Example : (Grade Program)


Program Examples Using IF-
Else
• Example : (Calculator Program)
Selection: SWITCH-CASE

• Switch-Case Operation
This statement is used in exchange of IF-ELSE, when if-else
nested number of level is enormous and difficult to read

• Syntax:
switch (expression) {
case constant1 : statements1; break;
.
.
case constant2 : statements2; break;
default : statements;
}
Selection: SWITCH-CASE

• Switch statement evaluate an expression by looking up for


each case constant value. If an expression value matches
with a case constant value then related statement/s is
executed. If nothing match then default statement is
executed.

• Note:
Expression and constant type should be integer (including
char)
Selection: SWITCH-CASE

 Flow Chart of SWITCH-CASE Statement


Program Examples Using
SWITCH-CASE
• Example :
REPETITION
Repetition Definition
 One or more instruction repeated for certain amount of time

 Number of repetition can be predefined (hard-coded in


program) or defined later at run time

 Repetition/looping operation:
– for
– while
– do-while
Repetition: FOR
• Syntax:

for(exp1; exp2; exp3) statement;


or:
for(exp1; exp2; exp3){
statement1;
statement2;
…….
}

exp1 : initialization
exp2 : conditional
exp3 : increment or decrement
exp1, exp2 and exp3 are optional
Repetition: FOR
• exp1 and exp3 can consist of several expression separated with
comma

• Example:
void reverse(char ss[])
{
int c,i,j;
for(i=0, j=strlen(ss)-1; i<j; i++, j--){
c=ss[i];
ss[i]=ss[j];
ss[j]=c;
}
}
Repetition: FOR
• Flow Chart of FOR Statement

exp1

exp3

statements

true
exp2

false
Repetition: FOR
• Flow Chart of FOR Statement Example :
Program Examples Using for

• Example :
– Program to print out numbers from 1 to 10

– Program to print out numbers from 10 to 1


Repetition: WHILE
• Syntax :
while (exp) statements;
or:
while(exp){
statement1;
statement2;
…..
}
• while (exp) statements;
• exp is Boolean expression. It will result in true (not zero) or
false (equal to zero).
• Statement will be executed while the exp is not equal to zero.
• exp evaluation is done before the statements executed.
Repetition: WHILE
• Flow Chart of WHILE Statement

statements

condition
true

false
Repetition : WHILE
• Example :

In while operation, statement block of statements may


never be executed at all if exp value is false
Program Examples Using
while
Program Examples Using
while
• Example: (Question)
Repetition : DO-WHILE
• Syntax : Example :
do{ int counter=0;
do {
< statements >; printf( "%d ", counter );
} while(exp); ++counter;
} while (counter <= 10);

• Keep executing while exp is true


• exp evaluation done after executing the statement(s)
Repetition : DO-WHILE
 Flow Chart of DO-WHILE Statement

statements

true
condition

false
Repetition : DO-WHILE
 Flow Chart of DO-WHILE Statement

In do-while on the other hand statement block of


statements will be executed min once
Program Examples Using
do-while
• Example: (Sentinel)
As sentinel, used 0 for width or height
BREAK VS CONTINUE
Break vs Continue
• break:
– ending loop (for, while and do-while)
– end the switch operation

• continue:
skip all the rest of statements (subsequent to
the skip statement) inside a repetition, and
continue normally to the next loop
Program Examples Using :

break
• Example using break:
Program Examples Using :

continue
• Example using continue:
GO TO AND LABEL
Go To and Label
• C is still providing the old fashion goto statement
• Syntax:
goto label;
……
label :
……
• label is written using colon symbol
• Avoid using goto to improve code readability
Program Examples Using :
goto and labeling
• Example using labeling:
ERROR TYPE
Error Type
• Compile-Time Error
– caused by syntax error
• Link-Time Error
– success fully compiled, but cause link error
– caused by no object code at link time
• Run-Time Error
– successfully compiled, but error at runtime. Usually caused by
numerical operation such as: overflow, floating point
underflow, division by zero, etc.
• Logical Error
– wrong result caused by incorrect logical flow/algorithm
Error Type
• Among those Error Types the most difficult to debug is
Logical Error.
• Example of Compile-Time Error:

Dev-C compiler will give message: In function main missing


terminating ” character, syntax error before return
Error Type
• Some C compiler merge the compile and link
processes, causing in difficulty to distinguish between
Compile-Time Error with Link-Time Error
• Example of Link-Time Error (Visual C++)
Error Type
• Example for Run-Time Error

Error cause:
Division by Zero
Error Type
• Example for Logical-Error

Should be: x1 = 5.00 and x2 = 2.00


Can you find the logic error ??
SUMMARY
Summary
 In an algorithm implementation, an instruction or block of
instructions may be executed (or not) with certain predetermined
condition, that’s why we use selection

 3 types of selection in C:
– if
– if-else
– switch-case
 Repetition is a condition which is one or more instruction repeated
for certain amount of time

 3 types of repetition/looping in C:
– for
– while
– do-while
References
• Paul J. Dietel,Harvey M. Deitel,. 2010. C : how to program.
PEAPH. New Jersey. ISBN:978-0-13-705966-9 Chapter 3 & 4
• Choosing between Alternatives:
http://docs.roxen.com/pike/7.0/tutorial/statements/conditions.xml
• Getting Controls: http://aelinik.free.fr/c/ch10.htm
• Doing the Same Thing Over and Over:
http://aelinik.free.fr/c/ch07.htm
Thank You

You might also like