Laboratory Discussion 2: Control Structures
Objectives:
TO program codes in blocks
TO implement basic conditional statements and the logical directive behind it.
TO do loops and other control structures
TWO MAJOR TYPES OF CONTROL STRUCTURES:
1. Selection
2. Iteration
I. Sequence/BLOCK STATEMENTS
~composed of multiple statements
~often characterised by an opening and closing curly bracket
EXAMPLE:
int main() {
printf(“HELLO WORLD”);
return 0;
}
comment counterpart for block statements!
/* bla bla bla
*/
II. CONDITIONAL STATEMENTS & SWITCH CASE
~ A Control Structure
controls the flow of the program
allows other codes to run and others not.
~Conditional statements (e.g. if, else if, else)
accepts logical values (0 or nonzero)
Runs the code if condition is met
~Remember: 0 is false and nonzero is true in C