0% found this document useful (0 votes)
16 views4 pages

Control Structures in IT Algorithms

Uploaded by

Devanand Manaram
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)
16 views4 pages

Control Structures in IT Algorithms

Uploaded by

Devanand Manaram
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

Information Technology

Form 4 Notes for Class 1 and Class 2


Wednesday, October 17, 2023
Class 1 - 8:00am to 9:00 am Class 2 - 9:00am to 10:30 am

Control Structures Statements

The steps that are identified for preparing algorithms can be written using three basic control
structures or program constructs to indicate how instructions will be carried out or how the flow of
control from one statement to another will take place.

The three basic control structure statements are:


1. Sequential control structure
2. Selection control structure
3. Iteration/Repetition/Loop structure

The Sequential Control Structure


The sequential control structure is used when you have instructions to be carried out a particular
order such as:

1. Input statements for example: (a) read price, tax


(b) Get num1, num2
(c) Accept guess

2. Output statements example: (a) Print total_cost


(b) Display average
(c) Produce ”Enter an integer value"

3. Involving arithmetic operators, such as:


(a) Sum  num1 +num2
(b) Average  sum/2
(c) Total_cost  Total_cost +Price

4. Statements involving the assignment of values variables through initialization, such as:
(a) Count  1
(b) Maximum  20
(c) Num1  0

1
The Selection Control Structure
The selection control structure is used in problems with instructions to be carried out if a certain
condition is met. The choice of options will be dependent on whether the condition it's true or false.
The control structure statements commonly used in algorithms are normally written as:

If (condition) then

< Instructions to be performed if the condition is true>

Else

< Instructions to be performed if the condition is false>

Endif

Example 2
Example 1
If (Age>=50) then
If (A>B) then Print “Old”
Display A Else
Else Print “Young”
Display B Endif
Endif

If (condition) then

< Instructions to be performed if the condition is true>

Endif
Example

If (A>B) then
Display A
Endif

2
The Nested IF statement
The If/Elseif statement allows you to create a chain of if statements. The If statements are evaluated
in order until one of the expressions is true or the end of the If/Elseif chain is reached. If the end of
the If/Elseif chain is reached without a true expression, no code blocks are executed. The Nested
control structure statements commonly used in algorithms are normally written as:

If (condition 1) then
< Instructions to be performed if the condition 1 is true>
Elseif (condition 2) then
< Instructions to be performed if the condition 2 is true>
Elseif (condition 3) then
< Instructions to be performed if the condition 3 is true>
Else
< Instructions to be performed if the condition is false>
Endif

Example
Print “Please enter today’s temperature”
Get Temp
If (Temp >45) then
Print “Wear light weight rain coat”
Elseif (Temp>20) AND (Temp<45) then
Print “Wear soft shell jacket”
Elseif (Temp>0) AND (Temp<20) then
Print “Wear down jacket”
Else
Print “Wear base layers and down jacket”
Endif

Iteration/Repetition/Loop Control Structure


Iteration/Repetition/Loop Control Structure allows statements to be repeated a fixed number of times
or until some condition evaluates to false while some condition is true. Iteration forms a repetitive
stage of the processing steps. There are two types of Iteration or Loop control structures:

1. Indefinite Loop/Unbounded Iteration


In this type of loop, you do not know in advance how many times the statements within the loop are to
be repeated.

3
Example of the While-do loop and Repeat-Until loop

While-do loop Repeat-Until Loop.


The general form of the While-do loop is The general form of the Repeat-Until loop
as follows: is as follows:

While (condition is true) do Repeat


<statements> <statements>
Endwhile Until (Condition is True)

Example
Display “Please enter Price of an item” Example
Read Price Repeat
While (price <> 0) do Display “Please enter Price of an item”
Total_Cost  Total_Cost + price Read Price
Display “Please enter Price of an item” Total_Cost  Total_Cost + price
Read Price Until (Price=0)
Endwhile Print Total_cost
Print Total_cost

2. Definite Loop/Bounded Iteration


In this type of loop, you know in advance the number of times the statement within the loop will be
repeated.
E.g. For-do Loop

The general form of the For-do loop is as follows:


:
For (<counter variable> = <Start Value> to <Terminating Value>) do

<statements>

Endfor

Example 1
For (Count = 1 to 5) do
Display “Please enter Price of an item”
Read Price
Total_Cost  Total_Cost + price
Endfor
Print Total_cost

Common questions

Powered by AI

The document defines three basic control structures: sequential, selection, and iteration. Sequential control structures involve executing instructions in a specific order, such as reading inputs or performing arithmetic operations . Selection control structures decide between different paths based on conditions, using statements like "if", "else", and "elseif" to direct flow . Iteration control structures, including indefinite (e.g., "while-do") and definite loops (e.g., "for-do"), repeat a set of instructions multiple times until a condition is met .

Sequential control structures manage data processing by executing each step in a predetermined order, ensuring each action occurs once, such as accepting inputs and performing calculations sequentially . Iterative control structures, however, handle repetitive tasks by looping through a set of instructions multiple times until a condition is met, optimizing processes that require repeated evaluations or accumulations, such as repeatedly summing item prices until a certain condition holds . These structures cater to different algorithmic needs: sequential structures are optimal for straightforward, linear processes, while iterative ones are essential for handling repeated operations efficiently.

Nested IF statements allow for multiple conditions to be evaluated sequentially, enabling complex decision-making within algorithms. Unlike single IF-ELSE statements, which handle only one binary condition, nested IF statements perform a chain of evaluations until a true condition is found, allowing for more nuanced and hierarchical decision paths . This is significant in practical applications requiring multiple decision criteria to be addressed, such as determining appropriate clothing based on temperature ranges .

'Repeat-Until' loops are beneficial in user-input-driven applications because they ensure the loop body is executed at least once before condition evaluation, which is essential for scenarios requiring initial user interaction, like collecting input until a certain termination condition is met . This behavior is useful in applications where the first execution's result is necessary, such as gathering data entries from users until a sentinel value indicates completion, promoting better user experience and data flow management.

An indefinite loop, such as a while-do loop, is preferred when the number of iterations is not known in advance and depends on dynamic conditions evaluated at runtime, like waiting for user input until a sentinel value is entered . In contrast, a definite loop, such as a for-do loop, is used when the number of iterations is predetermined, like iterating a fixed number of times to process a known set of data .

A nested IF statement enhances complexity by allowing multiple conditions to be evaluated in sequence, enabling a broader range of decision-making pathways within an algorithm compared to a simple IF-ELSE structure that evaluates only a single condition. Nested IF statements can handle more elaborate logic, facilitating detailed control over scenario-specific outputs, as illustrated by varying responses to changing temperature inputs for appropriate clothing recommendations . This allows algorithms to model complex real-world decision scenarios more effectively.

In a while-do loop, the condition is evaluated at the beginning of each iteration, so the loop may not execute at all if the condition is initially false . It is used when continuous evaluation before executing the loop body is necessary, such as when summing item prices until a zero price is entered . In contrast, a repeat-until loop evaluates the condition after the loop body, ensuring it executes at least once. This is useful when initial execution is required before condition checking, such as prompting for item price input until zero is entered .

Value initialization in sequential control structures is crucial as it sets initial conditions and states for variables that algorithms use for correct execution. For example, initializing a counter or an accumulator variable ensures that subsequent operations start with the intended initial value, preventing erroneous computations . Proper initialization is fundamental for maintaining data integrity and ensuring algorithm logic flows correctly from the start of computation tasks.

An "if" statement without an "else" clause is sufficient when specific actions need to be performed only if a given condition is true, and no actions are required if the condition is false. For instance, displaying a value only when a certain comparison is true, like "If (A>B) then Display A" . This simplifies scenarios where no alternative actions are needed if conditions aren't met.

The 'For-do' loop is advantageous in circumstances where the number of iterations is known beforehand. This control structure is efficient for processing collections with a defined size, such as iterating through a list or performing actions a predetermined number of times, ensuring each iteration is controlled precisely by a counter variable . This predictability is key for tasks requiring a specific number of repetitions, such as iterating over a fixed dataset to compute sums or averages.

You might also like