Constructs (Copy)
1. Introduction to Programming Constructs
- Programming constructs are fundamental structures that dictate how a program executes.
- Three main types of programming constructs:
- Sequence: Execution of statements in order.
- Selection: Decision-making using conditional statements.
- Iteration: Repeating a block of code based on conditions.
- Efficient use of programming constructs improves readability, maintainability, and efficiency of code.
2. Selection Constructs (IF and CASE Statements)
2.1 IF Statements
- Used to execute specific code based on a condition.
- Conditions typically involve relational operators (e.g.,
==,>,<,<=,>=,!=). - Syntax:
- Single IF Statement:
IF condition THEN Execute statement(s) ENDIF - IF-ELSE Statement:
IF condition THEN Execute statement(s) ELSE Execute alternative statement(s) ENDIF - Nested IF Statements:
- One IF condition inside another IF statement.
- Example:
IF condition1 THEN IF condition2 THEN Execute statement(s) ENDIF ENDIF
- Single IF Statement:
- Example: Checking a password’s length and characters.
IF LENGTH(inputPassword) = LENGTH(storedPassword) THEN IF LEFT(inputPassword, 1) = LEFT(storedPassword, 1) AND RIGHT(inputPassword, 1) = RIGHT(storedPassword, 1) THEN OUTPUT "Password entered has correct first and last letters" ELSE OUTPUT "Password entered is incorrect" ENDIF ELSE OUTPUT "Password entered is incorrect" ENDIF
2.2 CASE Statements
- Alternative to multiple IF-ELSE statements.
- More readable and structured when handling multiple conditions.
- Used when a variable can take multiple discrete values.
- Syntax:
CASE variable OF Value1 : Execute statement(s) Value2 : Execute statement(s) Value3 : Execute statement(s) OTHERWISE: Execute default statement(s) ENDCASE - Example: Menu selection program
OUTPUT "Please enter your choice" INPUT choice CASE choice OF 1 : OUTPUT "Routine 1" 2 : OUTPUT "Routine 2" 3 : OUTPUT "Routine 3" 4, 5, 6 : OUTPUT "Routine not written" 10 : Exit OTHERWISE: OUTPUT "Incorrect choice" ENDCASE - Language-Specific Differences:
- Python does not support
CASEstatements, alternatives likeif-elif-elseare used. - Java and VB use
switch-caseconstructs.
- Python does not support
3. Iteration Constructs (Loops)
- Loops are used for repeating code based on a condition.
- Three types of loops:
- Count-Controlled Loops (FOR Loops)
- Post-Condition Loops (REPEAT-UNTIL Loops)
- Pre-Condition Loops (WHILE Loops)
3.1 FOR Loops (Count-Controlled Loops)
- Executes a block of code a specific number of times.
- Syntax:
FOR counter = start TO end DO Execute statement(s) NEXT counter - Example: Summing numbers from 1 to 10
Total = 0 FOR Counter = 1 TO 10 INPUT Number Total = Total + Number NEXT Counter OUTPUT "The total is", Total - Some languages allow step increments:
FOR i = 1 TO 10 STEP 2 OUTPUT i NEXT i
3.2 REPEAT-UNTIL Loops (Post-Condition Loops)
- Executes code at least once, then checks the condition.
- Syntax:
REPEAT Execute statement(s) UNTIL condition - Example: Ensuring valid input
REPEAT OUTPUT "Enter value" INPUT value UNTIL value >= 0 AND value <= 10
3.3 WHILE Loops (Pre-Condition Loops)
- Checks the condition before executing the loop.
- May not execute if the condition is false at the start.
- Syntax:
WHILE condition DO Execute statement(s) ENDWHILE - Example: Asking for negative input
Number = 0 WHILE Number >= 0 DO OUTPUT "Enter a negative number" INPUT Number ENDWHILE
4. Comparison of Loop Types
| Loop Type | Execution | Condition Checked | Guaranteed Execution? |
|---|---|---|---|
| FOR | Fixed number of iterations | Before loop starts | Yes |
| WHILE | Runs while condition is true | Before each iteration | No |
| REPEAT-UNTIL | Runs until condition is met | After each iteration | Yes |
5. Conclusion
- Selection constructs allow conditional execution of code.
- Iteration constructs allow repeating code efficiently.
- Choosing the correct construct improves program efficiency and readability.
- Understanding these constructs is crucial for structured programming and problem-solving.
