Chapter 8
Chapter 8
1. Selection Statement
Selection statements, often referred to as conditional statements, allow programs to choose
between different actions depending on whether a condition is true or false. These structures are
essential for decision-making processes in programming.
This is the most basic selection structure. It enables the program to execute one of two code
blocks based on a condition
Syntax:
if condition then
statement1
else
statement2
end if
Condition: A boolean expression that determines which path the program takes.
statement1: Executed if the condition is true.
statement2: Executed if the condition is false
Example(Python)
x=5
if x > 0:
print("Positive")
else:
print("Negative or Zero")
Design Issues
Condition Evaluation: Should evaluation of the condition be short-circuit (i.e., if the first
condition fails, the second one isn’t evaluated)?
Complexity: Should you allow deeply nested if-else structures, or should you restrict their
use to avoid complexity?
The switch-case statement is used when there are multiple conditions to check. It provides a
cleaner alternative to multiple if-else chains when dealing with many choices
Syntax:
switch expression
case value1:
statement1
case value2:
statement2
...
default:
statement_default
end switch
Example(C)
int x = 2;
switch (x) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Other\n");
Design Issues
Fallthrough: Should the language allow the execution to fall through from one case to the
next unless explicitly interrupted by a break?
Clarity: Should a default case be mandatory to avoid unhandled cases?
c) Nested Selectors
Sometimes, the logic requires more than one level of selection. This happens when an if or
switch statement appears inside another selection statement.
Example:
if condition1 then
if condition2 then
statement1
else
statement2
end if
else
statement3
end if
Design Issues:
Readability: Deeply nested selectors can make code hard to read and maintain. Should
there be a limit on nesting depth or a style guideline against excessive nesting?
2. Iterative Statement:
Iterative statements (or loops) are used to repeatedly execute a block of code. Loops help in
reducing redundancy and can be controlled in two ways: through a counter (for a fixed
number of iterations) or logically (until a condition is met).
Syntax:
for i = 1 to N
statement
end for
Example(C)
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
Design Issues
Initialization: Should the loop counter be initialized in the loop, or should the
initialization be external to the loop for clarity?
Termination Condition: Should the language enforce that the counter will always move
towards the loop termination condition?
Syntax:
while condition do
statement
end while
condition: A boolean expression that determines whether the loop continues.
Example(Python)
x=0
while x < 5:
print(x)
x += 1
Design Issues
Termination: Should the language enforce the possibility of infinite loops if the
condition is always true?
Control Statements: Should the loop allow mechanisms like continue to skip one
iteration or break to terminate the loop prematurely
c) Nested Loops
Loops can be nested within other loops to iterate over multi-dimensional data structures (e.g., 2D
arrays).
Syntax:
for i = 1 to N
for j = 1 to M
statement
end for
end for
Example(C)
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("i: %d, j: %d\n", i, j);
}
}
Design Issues
Efficiency: Nested loops can lead to inefficient algorithms, especially with large datasets.
Should the language promote or restrict this?
Readability: Excessive nesting can make code less readable. Should languages enforce a
limit on the depth of nested loops
d) User-Located Loop Control Mechanisms
In many programming languages, users can control the flow of loops using statements like break
and continue:
Example(Python):
for i in range(10):
if i == 5:
break
print(i)
Design Issues
Control Flow: Should break and continue be universally available in all types of loops, or
should they be restricted to certain types of loops?
3. Unconditional Branching
a) Goto Statement
The goto statement allows the program to jump to a labeled section of code
Syntax:
goto label;
...
label:
statement;
Example (C):
int x = 10;
if (x > 5) {
goto label;
}
printf("This will be skipped\n");
label:
printf("Jumped to label\n");
Design Issues
Readability: Excessive use of goto can lead to unstructured and difficult-to-understand code
(spaghetti code). Many modern programming languages discourage or even prohibit goto.
Control Flow: Should languages promote structured control flow over unconditional
branching?
4.Guarded Commands
Guarded commands are an advanced control structure used in concurrent or parallel
programming. The key feature is that commands are only executed if a specific condition (guard)
is true.
Syntax:
if condition1 -> statement1
else if condition2 -> statement2
else -> statement3
end if
Example:
if x > 0 -> increment x
else -> decrement x
Design Issues:
Execution Guarantees: Should the language guarantee that at least one of the commands
will execute if multiple guards are true?
Concurrency: Guarded commands are useful in concurrent programming. Should the
language encourage or optimize for such usage
Conclusion:
Each of these control structures—selection, iteration, unconditional branching, and guarded
commands—plays a pivotal role in shaping how control flows through a program. The design
decisions made around their syntax, readability, efficiency, and use cases define how a
programming language enables developers to express complex logic while maintaining
readability and efficiency.
The careful use of these control structures can significantly impact the clarity, maintainability,
and performance of a program.