0% found this document useful (0 votes)
20 views7 pages

ASD1 Chapter 3!24!25

Chapter 3 discusses control structures in programming, focusing on conditional statements, including 'If-Then', 'If-Else', and 'Case' statements. These structures allow developers to manage the flow of execution based on Boolean conditions and make decisions in their code. The chapter provides examples and algorithms to illustrate how these control structures can be implemented effectively.

Uploaded by

leaderkoud
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)
20 views7 pages

ASD1 Chapter 3!24!25

Chapter 3 discusses control structures in programming, focusing on conditional statements, including 'If-Then', 'If-Else', and 'Case' statements. These structures allow developers to manage the flow of execution based on Boolean conditions and make decisions in their code. The chapter provides examples and algorithms to illustrate how these control structures can be implemented effectively.

Uploaded by

leaderkoud
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/ 7

Chapter 3 – Control Structures

1.Introduction

Control structures are fundamental to programming, allowing developers to create complex logic and
manage the flow of execution in their code. Understanding these structures is essential for writing
effective and efficient programs. In this section, we see some of these statements.

2.Conditional statement

In a conditional statement we make a test. The result of the test is a Boolean - either True or False. If
the result of the test is True we take a certain course of action and if the result of the test is False we
take another course of action.

2.1. Truncated instruction If ----- Then-----


If a particular condition is met (TRUE), a set of instructions is performed. Otherwise, this set is ignored.
Syntax

The general form of this statement is:

IF <condition> Then

<Sequence> ; // Code to execute when the condition is TRUE


End If;
Example: Maximum of two integers A,B.
Algorithm Maximum1;
Var A, B: Integer;
BEGIN
Write('Please enter two numbers A and B: ');
Read(A,B);
If (A >=B) Then
Max  A ;
End If;
If (A <B) Alors
Chapter 3 : Control Structures

Max B ;
End If;
Write (‘ The maximum of ‘,A, ‘and’,B ‘ is :’ ,Max) ;
END.

2.2. Alternative instruction

2.2.1. If ----- Else statement

The `Else` statement is used in conjunction with an `If` statement to specify a block of code to perform
when the condition is TRUE.
Syntax

If <condition> Then

<instruction 11> ;
<instruction 12> ;
// Code to perform when the condition is TRUE

<instruction 1N> ;
Else
<instruction 21> ;
<instruction 22> ;
// Code to perform when the condition is FALSE

<instruction 2M> ;
End If;

The [If then else] block will perform a particular sequence of instruction (instruction11, …,
instruction1N) if the condition is met (TRUE), and another set of instructions (instruction21, …,
instruction2M) if the condition is NOT met (FALSE), thereby 'branching' the project flow. Only one
branch in the [If then else] will be executed.

Example1 : Maximum of two integers A,B.


Algotrithm Maximum2 ;
Var A, B : Integer ;
BEGIN
Write (‘Enter A and B’) ;
Read (A, B) ;

2
Chapter 3 : Control Structures

If (A >=B) Then
MaxA ;
Else
MaxB ;
End If;
Write (‘ The maximum of ‘,A, ‘and’,B ‘ is :’ ,Max);
FIN.

Example 2 : : Write an algorithm that displays three integer numbers in ascending order.
Algorithm Ascending_Order1;
Var A, B, C, Min: integer;
BEGIN
Write ('Enter three integers A, B, and C');
Read (A, B, C);
IF (A <= B) THEN
IF (B <= C) THEN
Write('The order is: ', A, B, C);
ELSE
IF (A <= C) THEN
Write('The order is: ', A, C, B);
ELSE
Write('The order is: ', C, A, B);
END IF;
END IF;
ELSE
IF (A <= C) THEN
Write('The order is: ', B, A, C);
ELSE
IF (B <= C) THEN
Write('The order is: ', B, C, A);
ELSE
Write('The order is: ', C, B, A);
END IF;
END IF;
END IF;
END.

2.2.1. If ----- Else If statement

The `else-if` statement allows us to check multiple conditions in sequence and perform the first block
of pseudocode whose condition is TRUE.

3
Chapter 3 : Control Structures

- Example:

If condition1 Then

<Sequence1> ; // Code to perform when condition1 is true

ElseIf condition2 Then

<Sequence2> ; // Code to perfom when condition2 is true

Else

<Sequence3> ; // Code to perform when no conditions are true

End If;
Exercise 3: The same algorithm, that displays three integer numbers in ascending order, can be written by using
this statement.

Algorithm Ascending_Order2;
Var A, B, C, Min: integer;
BEGIN
Write ('Enter three integers A, B, and C');
Read (A, B, C);
IF (A <= B) THEN
IF (B <= C) THEN
Write('The order is: ', A, B, C);
ELSEIF (A <= C) THEN
Write('The order is: ', A, C, B);
ELSE
Write('The order is: ', C, A, B);
END IF;
END IF;
ELSE
IF (A <= C) THEN
Write('The order is: ', B, A, C);
ELSEIF (B <= C) THEN
Write('The order is: ', B, C, A);
ELSE
Write('The order is: ', C, B, A);
END IF;
END IF;
END IF;

4
Chapter 3 : Control Structures

END.

2.3. Case Statement


The Case statement allows us to make a choice among several possibilities based on the value of an
expression, which must be of ordinal type (integer, character, or boolean). It allows us to control the
flow of our algorithm and make decisions based on the evaluation of conditions.

Syntax 1

Case <expression> Is

Domain 1 : <block of actions 1 > ; // Domain is a list of constants.

Domain 2 : <block of actions 2 > ;



Domain N : <block of actions N > ;
End Case ;
In this instruction, the expression is evaluated as follows:

• If the result is equal to an element in Domain 1, then the actions in Block 1 are executed.
• If the result is equal to an element in Domain N, then the actions in Block N are executed.
• If the expression doesn't match any element in any domain, then the program proceeds to
the next part without executing any of the domain blocks.

Syntax 1

Case <expression> Is

Domain 1 : <block of actions 1 > ; // Domain is a list of constants.

Domain 2 : <block of actions 2 > ;



Domain N : <block of actions N > ;
End Case ;

In this case, if the expression doesn't match any element in any domain, then the actions in block p will be
executed.

The more general form of the case statement is :

5
Chapter 3 : Control Structures

Case <expression> Is

Domain 1 : < block of actions 1 > ;


Domain 2 : < block of actions 2 > ;

Domain N : < block of actions N > ;
Default
< block of actions p> ;
End Case;

Default (or Else) represents the code to be performed if none of the cases match the expression.

Example 1: Write a code that reads an integer i and performs different actions based on its value.
Depending on the value of i, it either adds a and b, sets s to 0, subtracts a from b (if i is 4, 5, or 6), or
multiplies a and b. The final result is then displayed as "The result is" followed by the value of s.Finding

Algorithm Action_Case;
Var i, a, b, s: integer;
BEGIN
Read (i);
CASE i Is
1,2 : s  a + b;
3: s  0;
4 to 6: s  a - b;
Default
s  a * b;
End Case;
Write ('The result is: ', s);
END.

Example 2: Write an algorithm that acts as a pocket calculator. It requires entering two values
separated by one of the operation signs +, -, *, and /. The algorithm performs the corresponding
operation.

Algorithm Pocket_Calculator;
Var Val1, Val2, S: real;
Op: character;
BEGIN

6
Chapter 3 : Control Structures

Read (Val1, Op, Val2);


case Op Is
'+': S  Val1 + Val2;
'-': S  Val1 - Val2;
'*': S  Val1 * Val2;
‘/', '÷':
If Val2 <> 0 Then
S  Val1 / Val2;
Else
Write ('Zero division');
End If;
Default
Write ('Error');
End Case;
Write ('The result is: ', S);
END.

You might also like