0% found this document useful (0 votes)
9 views8 pages

Chapter 8

ti about software engineering

Uploaded by

hsyas918
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Chapter 8

ti about software engineering

Uploaded by

hsyas918
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Chapter no 8

Statement-Level Control Structures


Control Structure:
A control structure is a control statement and the statements whose execution it controls

Evolution of Control Statement


 FORTRAN I control statements were based directly on IBM 704 hardware
 Much research and argument in the 1960s about the issue
o One important result: It was proven that all algorithms represented by flowcharts can be
coded with only two-way selection and pretest logical loops

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.

a) Two-Way Selection (if-else)

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?

b) Multiple-Way Selection (switch-case)

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).

a) Counter Control Loops


A counter-controlled loop runs for a specific number of iterations, determined by a loop
counter. The most common loop structure is the for-loop.

Syntax:
for i = 1 to N
statement
end for

 i: The loop counter.


 N: The number of iterations

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?

b) Logically Controlled Loops


In logically-controlled loops, the loop runs until a specific condition is no longer true. The
condition is checked before each iteration, and if the condition is false, the loop terminates.

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:

 break: Exits the loop immediately.


 continue: Skips the current iteration and moves to the next one

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.

 Selection statements help in making decisions based on conditions.


 Iteration statements allow repetitive tasks to be performed based on counters or
conditions.
 Unconditional branching is generally avoided in modern languages due to its potential
to create hard-to-manage code.
 Guarded commands offer flexibility and safety, especially in concurrent systems.

The careful use of these control structures can significantly impact the clarity, maintainability,
and performance of a program.

You might also like