SlideShare a Scribd company logo
DATA STRUCTURES AND ALGORITHMSI (DSA010)
CONTROL STRUCTURES
CONTROL STRUCTURES
1.1 Structured Program Theorem It is possible to write any computer program by using only three
basic control structures:
 Executing one subprogram, and then another subprogram (sequence)
 Executing one of two subprograms according to the value of a boolean expression (selection)
 Executing a subprogram until a boolean expression is true (iteration or repetition)
Type of control structure
 sequence
 Selection
 (iteration or repetition)
SELECTION CONTROL STRUCTURE
 The selection structures are of very high importance, because they enable programs to branch
into different blocks of instructions depending on particular conditions. This way they provide a
kind of “intelligence” to programs.
 A control structure is a block of programming that analyzes variables and chooses a direction in
which to go based on given parameters.
• The term flow control details the direction the program takes (which way program control "flows").
Hence it is the basic decision-making process in computing; flow control determines how a
computer will respond when given certain conditions and parameters.
 Basic Terminologies Those initial conditions and parameters are called preconditions.
Preconditions are the state of variables before entering a control structure.
Based on those preconditions, the computer runs an algorithm (the control structure) to
determine what to do. The result is called a postcondition.
Postconditions are the state of variables after the algorithm is run.
An Example Let us analyze flow control by using traffic flow as a model.
 A vehicle is arriving at an intersection. Thus, the precondition is the vehicle is in motion.
Suppose the traffic light at the intersection is red. The control structure must determine
the proper course of action to assign to the vehicle.
- Precondition: The vehicle is in motion.
- Control Structure Is the traffic light green? If so, then the vehicle may stay in motion. Is
the traffic light red? If so, then the vehicle must stop. End of Control
Structure
Type Description Best Use Case
if Executes when condition is true Single decision
if-else Executes one of two blocks Two outcomes
if-elif-else Checks multiple conditions Multiple outcomes
Nested if if inside another if Dependent decisions
switch-case
Matches against multiple
constant values
Clean multiple-choice selection
(non-Python)
SELECTION CONTROL STRUCTURE:
1. Simple If Statement
Executes a block of code only when a specified condition is true.
2. If-Else Statement
Provides two paths: one block runs if the condition is true, the other runs if the condition is false.
3. Nested If Statement
Contains an if or if-else statement inside another if or else block, allowing multiple conditions to be checked in
a hierarchy.
4. If-Else If Ladder
Checks multiple conditions sequentially; the first true condition's block is executed.
5. Case (or Switch) Statement
Evaluates a single expression and executes one block of code among many alternatives, based on matching
values.
SELECTION CONTROL STRUCTURE:
1. Simple if Statement :
A simple if statement is a basic control structure used in programming to execute a
block of code only if a certain condition is true. If the condition is false, the code inside
the if block is skipped.
START
int number1
int number2
IF number1 > number2 THEN
PRINT "The largest number is", number1
ELSE
PRINT "The largest number is", number2
ENDIF
END
Example : Write an Algorithm to find the Largest of Two Numbers
if-else Statement :
Chooses between two blocks of code based on whether the
condition is true or false.
if-elif-else Statement
Checks multiple conditions in sequence and executes the block where the first
true condition is found.
Case Statement
A case statement (also known as a switch statement in many programming languages) is a control
structure that allows a program to execute one block of code from multiple options, based on the value of
a single variable or expression.
 It is used as an alternative to writing many if...else if statements, making the code more organized and
readable when handling multiple possible values of the same variable.
Example 1: Grade Evaluation
Write a CASE statement in pseudocode for
Grade Feedback Based on Letter Grade
INPUT grade
CASE grade OF
"A":
Display "Excellent!"
"B":
Display "Very Good!"
"C":
Display "Good"
"D":
Display "You passed"
"F":
Display "Failed"
OTHERWISE:
Display "Invalid grade"
ENDCASE
Write a CASE statement in pseudocode for Student Year
Classification. INPUT year Level
CASE yearLevel OF
1:
Display "You are a First-Year student"
2:
Display "You are a Second-Year student"
3:
Display "You are a Third-Year student"
4:
Display "You are a Final-Year student"
OTHERWISE:
Display "Invalid year level"
ENDCASE
• General Example:
Convert the following linear if statement into nested if statement.
linear if statement
IF a > 5 AND b < 7 THEN
statement(s)_A
ELSE IF c > 6 THEN
statement(s)_B
ELSE
statement(s)_C
ENDIF
nested if statement
IF a > 5 THEN
IF b < 7 THEN
statement(s)_A
ELSE IF c > 6 THEN
statement(s)_B
ELSE
statement(s)_C
ENDIF
ELSE IF c > 6 THEN
statement(s)_B
ELSE
statement(s)_C
ENDIF
Symbol Name Meaning
=
Assignment / Equal (varies by
context)
Often used to assign a value (e.g., x =
5)
== Equal to Checks if two values are equal
!= Not equal to Checks if two values are not equal
< Less than Example: x < 10
> Greater than Example: x > 5
<= Less than or equal to Example: x <= 10
>= Greater than or equal to Example: x >= 5
Comparison (Relational) Operators (used for comparing values)
SET i = 1
WHILE i <= 3 DO // Comparison Operator: <=
SET j = 1
WHILE j <= 3 DO // Comparison Operator: <=
DISPLAY i + " x " + j + " = " + (i * j) // Arithmetic Operator: *
SET j TO j + 1 // Arithmetic Operator: +
END WHILE
SET i TO i + 1 // Arithmetic Operator: +
END WHILE
Pseudocode with Operators
Operator Type Description
<= Comparison Operator Checks if the left value is less or equal
+ Arithmetic Operator Adds numbers or strings (concatenation)
* Arithmetic Operator Multiplies numbers
= Assignment Operator Assigns value to a variable
Operators Used Pseudocode
SET number = 7
IF number % 2 == 0 THEN // % is Modulus Operator
(remainder)
DISPLAY "Even Number"
ELSE
DISPLAY "Odd Number"
END IF
Check if a number is even or odd
Operators Used:
•% → Arithmetic Operator (modulus)
•== → Comparison Operator (equal to)
•= → Assignment Operator
SET a = 12
SET b = 8
SET c = 15
IF a > b AND a > c THEN // Logical Operator: AND
DISPLAY "a is the largest"
ELSE IF b > a AND b > c THEN
DISPLAY "b is the largest"
ELSE
DISPLAY "c is the largest"
END IF
Find the largest of three numbers

More Related Content

PPT
03a control structures
PPTX
Lewis_Cocking_AP_Decision_Making_For_Coding
PPT
Ap Power Point Chpt3
PDF
selection structures
PPT
slides03.ppt
PPT
The Three Basic Selection Structures in C++ Programming Concepts
PDF
CS305PC_C++_UNIT 2.pdf jntuh third semester
03a control structures
Lewis_Cocking_AP_Decision_Making_For_Coding
Ap Power Point Chpt3
selection structures
slides03.ppt
The Three Basic Selection Structures in C++ Programming Concepts
CS305PC_C++_UNIT 2.pdf jntuh third semester

Similar to DATA STRUCTURES AND ALGORITHMSI (DSA010).pdf (20)

PPT
Chapter 4 flow control structures and arrays
PPTX
Loops presentationnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
PPT
Control structures i
PPT
Control statements
PPT
Lecture 3
PDF
Chapter 2 - Flow of Control Part I.pdf
PPTX
GUI Programming in JAVA (Using Netbeans) - A Review
PPTX
MODULE_2_Operators.pptx
PPT
Ap Power Point Chpt3 B
PPT
C statements.ppt presentation in c language
PPTX
Control statements in c
PDF
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
PPT
Ppt lesson 08
PPTX
CPP programming Program Control Structures.pptx
PPTX
Review Python
PPTX
LOOPS AND DECISIONS
PPTX
BSc. III Unit iii VB.NET
PPT
cprogrammingprogramcontrolsHJKHJHJHJ.ppt
PPTX
Introduction-to-Conditional-Statements-in-Python.pptx
Chapter 4 flow control structures and arrays
Loops presentationnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
Control structures i
Control statements
Lecture 3
Chapter 2 - Flow of Control Part I.pdf
GUI Programming in JAVA (Using Netbeans) - A Review
MODULE_2_Operators.pptx
Ap Power Point Chpt3 B
C statements.ppt presentation in c language
Control statements in c
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Ppt lesson 08
CPP programming Program Control Structures.pptx
Review Python
LOOPS AND DECISIONS
BSc. III Unit iii VB.NET
cprogrammingprogramcontrolsHJKHJHJHJ.ppt
Introduction-to-Conditional-Statements-in-Python.pptx
Ad

More from blasiuspinias (9)

PDF
DATA STRUCTURES AND ALGORITHMSI (DSA010)_LESSON 1.pdf
PPTX
wCIHc9AaI fgn gfgjhgf bxn ffbb fv .pptx
PPTX
05fghsdgsgrg dxgs sdgfs sdgfd sdgs.pptx
PPTX
Data Structures Unit 1 bnmxc xgfh (1).pptx
PPTX
Programming II - Introductio bncvfh dgdgn.pptx
PPTX
Week 1fah xzvfsgds sdfasdfsfg dsaewag.pptx
PDF
DATAND ALGORITHMSI (DSA010)_LESSON 1.pdf
PDF
DPG Class Reps Information 2gffd5 v1.pdf
PDF
Repetition Control Structure_lsson 3.pdf
DATA STRUCTURES AND ALGORITHMSI (DSA010)_LESSON 1.pdf
wCIHc9AaI fgn gfgjhgf bxn ffbb fv .pptx
05fghsdgsgrg dxgs sdgfs sdgfd sdgs.pptx
Data Structures Unit 1 bnmxc xgfh (1).pptx
Programming II - Introductio bncvfh dgdgn.pptx
Week 1fah xzvfsgds sdfasdfsfg dsaewag.pptx
DATAND ALGORITHMSI (DSA010)_LESSON 1.pdf
DPG Class Reps Information 2gffd5 v1.pdf
Repetition Control Structure_lsson 3.pdf
Ad

Recently uploaded (20)

PDF
Solaris Resources Presentation - Corporate August 2025.pdf
PDF
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
PDF
NewBase 12 August 2025 Energy News issue - 1812 by Khaled Al Awadi_compresse...
PDF
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
PPTX
Board-Reporting-Package-by-Umbrex-5-23-23.pptx
PDF
NISM Series V-A MFD Workbook v December 2024.khhhjtgvwevoypdnew one must use ...
PDF
Booking.com The Global AI Sentiment Report 2025
PPTX
operations management : demand supply ch
PDF
1911 Gold Corporate Presentation Aug 2025.pdf
PDF
Comments on Crystal Cloud and Energy Star.pdf
PDF
Deliverable file - Regulatory guideline analysis.pdf
PDF
How to Get Funding for Your Trucking Business
PPTX
Sales & Distribution Management , LOGISTICS, Distribution, Sales Managers
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
PPTX
2025 Product Deck V1.0.pptxCATALOGTCLCIA
PDF
Technical Architecture - Chainsys dataZap
PDF
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
PDF
Introduction to Generative Engine Optimization (GEO)
PDF
Keppel_Proposed Divestment of M1 Limited
Solaris Resources Presentation - Corporate August 2025.pdf
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
NewBase 12 August 2025 Energy News issue - 1812 by Khaled Al Awadi_compresse...
ANALYZING THE OPPORTUNITIES OF DIGITAL MARKETING IN BANGLADESH TO PROVIDE AN ...
Board-Reporting-Package-by-Umbrex-5-23-23.pptx
NISM Series V-A MFD Workbook v December 2024.khhhjtgvwevoypdnew one must use ...
Booking.com The Global AI Sentiment Report 2025
operations management : demand supply ch
1911 Gold Corporate Presentation Aug 2025.pdf
Comments on Crystal Cloud and Energy Star.pdf
Deliverable file - Regulatory guideline analysis.pdf
How to Get Funding for Your Trucking Business
Sales & Distribution Management , LOGISTICS, Distribution, Sales Managers
Ôn tập tiếng anh trong kinh doanh nâng cao
2025 Product Deck V1.0.pptxCATALOGTCLCIA
Technical Architecture - Chainsys dataZap
Outsourced Audit & Assurance in USA Why Globus Finanza is Your Trusted Choice
Introduction to Generative Engine Optimization (GEO)
Keppel_Proposed Divestment of M1 Limited

DATA STRUCTURES AND ALGORITHMSI (DSA010).pdf

  • 1. DATA STRUCTURES AND ALGORITHMSI (DSA010) CONTROL STRUCTURES
  • 2. CONTROL STRUCTURES 1.1 Structured Program Theorem It is possible to write any computer program by using only three basic control structures:  Executing one subprogram, and then another subprogram (sequence)  Executing one of two subprograms according to the value of a boolean expression (selection)  Executing a subprogram until a boolean expression is true (iteration or repetition) Type of control structure  sequence  Selection  (iteration or repetition)
  • 3. SELECTION CONTROL STRUCTURE  The selection structures are of very high importance, because they enable programs to branch into different blocks of instructions depending on particular conditions. This way they provide a kind of “intelligence” to programs.  A control structure is a block of programming that analyzes variables and chooses a direction in which to go based on given parameters. • The term flow control details the direction the program takes (which way program control "flows"). Hence it is the basic decision-making process in computing; flow control determines how a computer will respond when given certain conditions and parameters.
  • 4.  Basic Terminologies Those initial conditions and parameters are called preconditions. Preconditions are the state of variables before entering a control structure. Based on those preconditions, the computer runs an algorithm (the control structure) to determine what to do. The result is called a postcondition. Postconditions are the state of variables after the algorithm is run. An Example Let us analyze flow control by using traffic flow as a model.
  • 5.  A vehicle is arriving at an intersection. Thus, the precondition is the vehicle is in motion. Suppose the traffic light at the intersection is red. The control structure must determine the proper course of action to assign to the vehicle. - Precondition: The vehicle is in motion. - Control Structure Is the traffic light green? If so, then the vehicle may stay in motion. Is the traffic light red? If so, then the vehicle must stop. End of Control Structure
  • 6. Type Description Best Use Case if Executes when condition is true Single decision if-else Executes one of two blocks Two outcomes if-elif-else Checks multiple conditions Multiple outcomes Nested if if inside another if Dependent decisions switch-case Matches against multiple constant values Clean multiple-choice selection (non-Python) SELECTION CONTROL STRUCTURE:
  • 7. 1. Simple If Statement Executes a block of code only when a specified condition is true. 2. If-Else Statement Provides two paths: one block runs if the condition is true, the other runs if the condition is false. 3. Nested If Statement Contains an if or if-else statement inside another if or else block, allowing multiple conditions to be checked in a hierarchy. 4. If-Else If Ladder Checks multiple conditions sequentially; the first true condition's block is executed. 5. Case (or Switch) Statement Evaluates a single expression and executes one block of code among many alternatives, based on matching values. SELECTION CONTROL STRUCTURE:
  • 8. 1. Simple if Statement : A simple if statement is a basic control structure used in programming to execute a block of code only if a certain condition is true. If the condition is false, the code inside the if block is skipped.
  • 9. START int number1 int number2 IF number1 > number2 THEN PRINT "The largest number is", number1 ELSE PRINT "The largest number is", number2 ENDIF END Example : Write an Algorithm to find the Largest of Two Numbers
  • 10. if-else Statement : Chooses between two blocks of code based on whether the condition is true or false.
  • 11. if-elif-else Statement Checks multiple conditions in sequence and executes the block where the first true condition is found.
  • 12. Case Statement A case statement (also known as a switch statement in many programming languages) is a control structure that allows a program to execute one block of code from multiple options, based on the value of a single variable or expression.  It is used as an alternative to writing many if...else if statements, making the code more organized and readable when handling multiple possible values of the same variable. Example 1: Grade Evaluation
  • 13. Write a CASE statement in pseudocode for Grade Feedback Based on Letter Grade INPUT grade CASE grade OF "A": Display "Excellent!" "B": Display "Very Good!" "C": Display "Good" "D": Display "You passed" "F": Display "Failed" OTHERWISE: Display "Invalid grade" ENDCASE Write a CASE statement in pseudocode for Student Year Classification. INPUT year Level CASE yearLevel OF 1: Display "You are a First-Year student" 2: Display "You are a Second-Year student" 3: Display "You are a Third-Year student" 4: Display "You are a Final-Year student" OTHERWISE: Display "Invalid year level" ENDCASE
  • 14. • General Example: Convert the following linear if statement into nested if statement. linear if statement IF a > 5 AND b < 7 THEN statement(s)_A ELSE IF c > 6 THEN statement(s)_B ELSE statement(s)_C ENDIF nested if statement IF a > 5 THEN IF b < 7 THEN statement(s)_A ELSE IF c > 6 THEN statement(s)_B ELSE statement(s)_C ENDIF ELSE IF c > 6 THEN statement(s)_B ELSE statement(s)_C ENDIF
  • 15. Symbol Name Meaning = Assignment / Equal (varies by context) Often used to assign a value (e.g., x = 5) == Equal to Checks if two values are equal != Not equal to Checks if two values are not equal < Less than Example: x < 10 > Greater than Example: x > 5 <= Less than or equal to Example: x <= 10 >= Greater than or equal to Example: x >= 5 Comparison (Relational) Operators (used for comparing values)
  • 16. SET i = 1 WHILE i <= 3 DO // Comparison Operator: <= SET j = 1 WHILE j <= 3 DO // Comparison Operator: <= DISPLAY i + " x " + j + " = " + (i * j) // Arithmetic Operator: * SET j TO j + 1 // Arithmetic Operator: + END WHILE SET i TO i + 1 // Arithmetic Operator: + END WHILE Pseudocode with Operators
  • 17. Operator Type Description <= Comparison Operator Checks if the left value is less or equal + Arithmetic Operator Adds numbers or strings (concatenation) * Arithmetic Operator Multiplies numbers = Assignment Operator Assigns value to a variable Operators Used Pseudocode
  • 18. SET number = 7 IF number % 2 == 0 THEN // % is Modulus Operator (remainder) DISPLAY "Even Number" ELSE DISPLAY "Odd Number" END IF Check if a number is even or odd Operators Used: •% → Arithmetic Operator (modulus) •== → Comparison Operator (equal to) •= → Assignment Operator
  • 19. SET a = 12 SET b = 8 SET c = 15 IF a > b AND a > c THEN // Logical Operator: AND DISPLAY "a is the largest" ELSE IF b > a AND b > c THEN DISPLAY "b is the largest" ELSE DISPLAY "c is the largest" END IF Find the largest of three numbers