Monday, October 29, 2018

Selection



What is selection?
In an algorithmic implementation, an instruction or block of instructions may be executed (or
not) with certain predetermined conditions.


Selection Syntaxes:
if

if (boolean expression) statement;
or
if (boolean expression) {
    statement1;
    statement2;           Block of statements
……
}

If boolean expression resulting is true, then the statement after the if will be executed.




if-else
if (boolean expression) statement1;
else statement2;
or

if boolean expression is true, then it will execute statement1,  otherwise, it will execute 
statement2.


nested-if
The if ... else statement executes two different codes depending on whether the test expression is true
or false. Sometimes a choice must be made among more than 2 possibilities.
The if ... else nested statement allows you to search for multiple test expressions and execute different
codes for multiple conditions.

if (boolean expression) statement1;
if (boolean expression) statement2;
if (boolean expression) statement3;

I
switch-case

This statement is used in exchange for IF-ELSE, when the nested number of levels is huge and hard
to read.
switch (expression) {
case constant1 : statements1; break; 
.
.
case constant2 : statements2; break;
default : statements;
}

The Switch statement evaluates an expression by looking for each case constant value. If an 
expression value is a constant case value, the related statement / s is executed. If nothing matches, the
default statement is executed.






?: Operator

The ?: operator is similar to If,  the difference is that ?: returns a value..



Example:



if(a > b)

max_value = a;

else

max_value = b;

can be written as

max_value = (a > b) ? a : b;









Go To and Label

There is still "go to" for C, but it is recommended to avoid using "go to" as it will confuse the users.



Syntax: 

goto label

 ……

label : 

 ……




There are 4 types of error that we usually found
Compilation error
caused by a syntax error

Link error
causes link error caused by no object code at the time of the link

Runtime error
error while running,caused by numeric operations such as: overflow, floating point overflow, division by zero, and so on.

Logical error
caused by incorrect logic







By :Steven

2201852132

Computer Science and Statistics





No comments:

Post a Comment