IPRG
Understanding Programming Structures
Scene and Initial Selection
The scene is a programming construct that includes either a selection or a loop.
In this context, the next structure is identified as a loop.
Key indicators for identifying a loop include:
An arrow pointing to the top of the loop symbol, suggesting repetition.
Loop Structure Details
The loop executes based on certain criteria—if conditions are met, the loop continues.
If conditions are not met:
Fold paper at the end.
Tape the paper in place.
Rotate the gift around to check the state.
Identifying Programming Structures
Subversion Structure
The structure discussed appears to be subversion or a nested structure.
Within a program, a sequence structure can be represented as a square, indicating a linear flow.
Nested Structures
The sequence structure is nested inside a loop, indicating a complex relationship between the operations.
After exiting the loop, a selection structure follows.
Selection Structure Classifications
A selection structure can be defined as single alternative or multiple alternatives.
To identify a selection structure:
Look for a two-step decision process based on yes/no conditions (e.g., placing a bow or returning to null case).
The presence of an arrow returning to the question indicates repetition.
Pseudo Code Structure
Indentation and Structure Hierarchy
Good pseudo code practices include:
Proper indentation to signify which tasks belong to each structure (loop or selection).
Loop conditions need to have their end aligned with the execution line to maintain clarity.
Example of Loop in Pseudo Code
E.g.,
WHILE <condition>
followed by indented tasks:Tasks executed during the loop.
Ending with
END WHILE
aligned on the same margin asWHILE
.
Selection Structure in Pseudo Code
Similar indentation rules apply for selection:
IF <condition>
followed by tasks and terminating withEND IF
on the same line.
Modular Division
Purpose of Modular Division
Modular division is crucial for obtaining the remainder after division.
Example:
2 MOD 4
results in a remainder of2
.
Applied in programming tasks:
Defining variables to store user inputs and calculating remainders.
User Interaction
Users are prompted for input, saved in a variable:
Example:
Prompt: "Enter a number."
Variable:
number
stores user input.
Analysis of whether the number is even or odd is conducted using:
number % 2
(modulo operator).Check if remainder is
0
for even; else it is odd.
Example Program Logic
If
remainder == 0
:Output: "The number is even."
Else:
Output: "The number is odd."
Finalize by terminating the
IF
statement correctly.