0% found this document useful (0 votes)
16 views

Decision Structures Problem Solving Power ICT Presentation

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Decision Structures Problem Solving Power ICT Presentation

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Decision

Structures:
Problem-Solving
Power
Decision structures are fundamental building blocks of code.
They enable programs to respond to different inputs and
make choices based on specific conditions.
Problem-solving:
"process of identifying a problem, or devising possible
solutions and implements the best solutions".
Decision structure:
"Decision structure is a fundamental programming construct that allows a program to execute different
actions based on whether the condition is true or false".

"key components" conditions Branching


• Conditions {expressions} A logical expression that The path that the program
• Decision {Branching} evaluates to either true or can take based on the
false. outcome of the conditions.
Types of Decision
Structures

1 if Statement 2 if-else Statement

Executes code if a Executes one block of


condition is true. code if a condition is
true, and another if it's
false.

3 Nested Statement 4 switch Statement

Multiple if statements Evaluates an


within each other, expression and
creating complex executes the
decision trees. corresponding case
block.
Conclusion and Key Takeaways
Understanding

Essential for building complex and responsive programs.

Choosing

The right decision structure is crucial for efficiency.

Flexibility

Decision structures add adaptability to code.


Flowcharts: Start

Input
IF statement:

Execute only one block of


code if the condition is
False Condition
True
true.

Block of if-statement

End
Start
If-else statement:

Input
To provide two outcomes based on

the condition, if it true or false .


Condi
False
tion

True
If block Else block

End
Being switch

Switch statement: True


Case 1 Staement 1

It is used when select one statement False


out of many statements. True
Case 2 Statement 2

False
True
Case 3 Staement 3

False

Default statement End switch


Start

Nested statement: condi


tion
1 False

True
• To check multiple conditions by placing
One if-statement inside another. Cond
ition False
• Inner If-statement execute only if outer 2

conditions are true.


True

Block Block Block


statement 3 statement 2 statement 1

output

Stop
What is Pseudocode?
Pseudocode is a way to describe algorithms and problem-solving logic in simple, plain language. It helps you plan out what your code
should do without worrying about the syntax of a specific programming language.

Decision Structures in Pseudocode:

1- If Statement:

Purpose: Executes a block of code if a specific condition is true. • Example: Check if a number is positive. Pseudocode: Begin

Input number

If number > 0 Then

Print "The number is positive".

End If

End
Explanation:

• The program starts by taking input.

• It checks if the number is greater than 0.

• If true, it prints "The number is positive.”

• Ends the If-statement and the process.


2- If-Else Statement:
Purpose: Executes one block of code if a condition is true, and another block if it is false. Example: Check
if a number is positive or negative.Begin

Input number

If number > 0 Then

Print "The number is positive."

Else

Print "The number is negative."

End If

End
Explanation:
· The program starts by taking input.

· It checks if the number is greater than 0.

· If true, it prints "The number is positive."

· If false, it prints "The number is negative."

· Ends the If-Else statement and the process


4. Switch Statement:
Purpose: Chooses a block of code to execute based on the value of a variable.

Example: Determine the day of the week.

Pseudocode:

Begin

Input day

Switch day

Case "Monday":

Print "Start of the work week."

Case "Wednesday":

Print "Midweek."

Case "Friday":

Print "Almost weekend!"

Default:

Print "Regular day."

End Switch

End
Explanation:
· The program starts by taking input.

· It checks the value of "day."

· Depending on the value, it prints a corresponding message.

· If the value doesn't match any case, it prints "Regular day."

· Ends the Switch statement and the process.


5. Nested Statement
Purpose: An If-Else structure inside another If-Else structure for more complex decision making.

Example: Determine if a person can vote and if they are a senior citizen.

Pseudocode: Begin

Input age

If age >= 18 Then

If age >= 65 Then

Print "You can vote. You are a senior citizen."

Else

Print "You can vote."

End If

Else

Print "You cannot vote."

End If

End
Explanation:
· The program starts by taking input.

· It checks if the age is 18 or more.

· If true, it checks if the age is 65 or more.

· If both conditions are true, it prints "You can vote. You are a senior citizen."

· If only the first condition is true, it prints "You can vote."

· If the first condition is false, it prints "You cannot vote."

· Ends the Nested If statement and the process.


Why Use Pseudo-code?
Algorithm Design:

Communication:

Easy Translation:
 Problem-solving with Repetition structure:

• UNDERSTANDING LOOPS
• FLOWCHARTS
• PSEUDO-CODES
Looping through taska:

For Loop

1 Executes a block of code a predetermined number of times.

While Loop
2
Executes a block of code as long as a condition is true.

Do-while Loop
3 Executes a block of code at least once, then
checks the condition for further repetitions.
1. For loop
2. While loop
3. Do while loop

For loop:
A for loop is a control flow statement that
allows you to repeat a block of code a specific
number of times or over a sequence (like a
list, range, or other iterable). It's especially
useful when you know in advance how many
times you want to execute a piece of code.
The syntax of a for loop varies depending on
the programming language, but its structure
is generally the same.
Explanation of parts:
1- Initialization: Sets a starting point, often initializing a counter variable.

2- Condition: Checks if the loop should continue. If True, the loop runs; if False, it stops.

3- Update/Increment: Changes the counter variable to eventually make the condition False.

In this loop:

• The counter (i) starts at 0


• It increments by 1 each time.
• The loop stops when (i) reaches 10
WHILE LOOP:
A while loop is a control flow statement that allows
code to be executed repeatedly as long as a
specified condition is TRUE. Unlike a for loop, a
while loop does not require knowing the number
of iterations in advance_it continues to execute
until the condition becomes FALSE.
Explanation:
1- Initialization: int count = 0; -Sets up the starting value of count.

2- Condition: while (count < 5) - The loop will continue as long as count is less than 5.

3-Loop Body: System.out.println ("Count is: " + count); - Prints the current value of count.

4- Increment: count++; - Increases count by 1 on each iteration to eventually make the condition count
< 5 False and stop the loop.
Do While Loop:
A do-while loop is a variation of the while loop
that ensures the code inside the loop is executed
at least once, even if the condition is False from
the start. This is because, in a do-while loop, the
condition is checked after the loop body has been
executed.
Explanation:
1) The code inside the do block executes first.

2) After execution, the while condition is checked.

3) If the condition is true, the loop runs again; if false the loop stops.
Flowchart of Repetition structure:

while loop:

Explanation:

• Initialization:
• A variable (e.g., counter) is initialized with a starting value.
• Condition Check:
• The condition is evaluated. If it's true, the loop body is executed.
• If the condition is false, the loop terminates.
• Loop Body:
• The statements within the loop are executed. These statements typically involve processing data or
performing calculations.
• Do-While Loop:

Another type of repetition structure, the do-while loop, executes the loop body at least once before checking the c

• For Loop:

A for loop is often used when the number of iterations is known in advance.

• Nested Loop:

Loops can be nested within each other to create complex patterns and calculations. By understanding
these concepts and the flowchart representation, you can effectively use repetition structures in your
pseudocode and programming to automate repetitive tasks and solve problems efficiently.
Pseudo-Codes

For loop pseudo-code

For (initialization ; condition)


Increment; DO
Statement
End For

Example:
(i=1; i<=5; i=i+1) Do
Print I
End For
While Loop Pseudocode

WHILE (condition) DO
statements
END WHILE

Example:

x=1
WHILE (x <= 5) DO
PRINT x
x=x+1
END WHILE
while loop pseudo-code

While (condition) DO
Statements
End while

Example:
X=1
While (x=5) DO
Print x
X=x+1
End while
For Loop Pseudocode

FOR (initialization; condition; increment) DO


statements
END FOR

Example:

FOR (i = 1; i <= 5; i = i + 1) DO
PRINT i
END FOR
Do-While Loop Pseudocode

DO
statements
WHILE (condition)

Example:

x=1
DO
PRINT x
x=x+1
WHILE (x <= 5)
Repeat-until loop pseudo-code

Repeat statements
Until (condition)

Example:
X=1
Repeat
Print x
X=x+1
Until (x>5)

You might also like