This document discusses different types of control flow statements in C programming. It explains that control flow is required when program execution needs to change based on conditions. It then provides examples of common conditional control structures in C like if-then-else statements, ternary operators, switch-case statements, and while loops. For each structure, it gives the syntax and an example code snippet to illustrate how it works.
3. Control flow in software programs is required when program
execution should alter it’s behavior based on machine
conditions, user inputs, external triggers or internal triggers.
It’s easier to program in human readable computer software
programming languages when program complexity dictates high
level spoken or written language like constructs. C is one such
high level language.
C, as you already know, is a computer software programming
language that is more high-level (human readable) over low-
level (machine readable) i.e. low level machine language like
programming constructs are available at your disposal but high
level constructs are more in fashion.
Control statements in execution flow control of a software
program are required when there is logical decision making
(conditional execution) involved during working of the machine
program based on its decision variables (conditions) of
execution.
5. “If –then-else” conditional loop is used in those sections of the programs where branching
of logical flow of instructions is needed because of change in values of execution control
variables.
Syntax:
if (condition) *“condition” check could involve one or more relational or boolean logical
tests.
if_true;
else // program could have multiple else statements qualified by another “if “
check i.e. “else if”
if_false;
In the example above, code branches out based on whether test condition is true or
false.
Example Code:
int status;
pid_t pid = fork();
if (pid == -1) {
error("fatal fork error", true);
exit(FAILURE);
} else if (pid == 0) { // This is a child function.
child_function(args);
} else { // This is system call in parent context that puts the parent in
wait state for child.
(void)waitpid(pid, &status, 0);
}
exit(SUCCESS);
6. Ternary operator (?:) is another conditional
expression available at programmers disposal for
choice tests:
Syntax:
(condition) ? (if_true) : (if_false)
If "condition" is true, the second expression is
executed ("if_true") otherwise the third expression
("if_false") is executed.
Example code:
largest = ((a > b) ? a : b);
7. “switch – case” statements controls the path of a program
execution based on the value of selection variable in multiple
choice decision scenarios.
Syntax:
switch (variable) {
case ‘x’: statement; break;
.
.
case default: statement; break;
}
Example code:
switch (age) {
case 1: printf("You're one."); break;
case 2: printf("You're two."); break;
case 3: printf("You're three."); break;
case 4: printf("You're four."); break;
default: printf("You're neither!"); break;
}
8. While(True) loop
Execute a piece of code after test of truthfulness of a condition
based on the value of specified execution control variable.
Syntax:
while ( condition ) {
/* code */
}
Example Code:
int x = 0;
while ( x != 5 )
{
function_A();
x = x + 1;
}