PPT4 - COMP6112 - Program Control Selection - Repetition - R0
PPT4 - COMP6112 - Program Control Selection - Repetition - R0
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
……
}
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
• 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
• Note:
Expression and constant type should be integer (including
char)
Selection: SWITCH-CASE
Repetition/looping operation:
– for
– while
– do-while
Repetition: FOR
• Syntax:
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
statements
condition
true
false
Repetition : WHILE
• Example :
statements
true
condition
false
Repetition : DO-WHILE
Flow Chart of DO-WHILE Statement
• 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:
Error cause:
Division by Zero
Error Type
• Example for Logical-Error
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