In this 3rd meeting in Algorithm & Programming I learn about selection.
What is selection?
- In an algorithm implementation, an instruction or block of instructions may be executed (or not) with certain predetermined condition
- Syntax:
–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.
–IF-ELSE
Syntax :
if (boolean expression) statement1;
else statement2;
or
if (boolean expression){
statement1;
statement2; Block statement1
……
}
else {
statement3;
statement4; Block statement2
…
}
If boolean expression resulting in TRUE, then statement1 or block statement1 will be executed, if FALSE then statement2 or block statement2 be executed.
–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;
}
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