04-lecture
Secure Coding Lecture 04 - Iterations
Course Overview
Course Name:
Secure Coding Lecture 04 - Iterations
Institution:
De Montfort University Leicester
Focus:
This course emphasizes collaboration with industry partners in developing the Cyber-Jargon Iteration (CJi) framework, which focuses on secure coding practices through iterative processes.
Quiz Questions
True/False Question: An "if" statement must always have an "else" statement.
Condition Checking: How to check if a variable x is greater than 10 and less than 20?
Options:
x > 10 || x < 20
x > 10 && x < 20
x > 10 && x > 20
x < 10 && x < 20
Looping Basics
Overview of Loop Types:
While loops: Used when the number of iterations is not known beforehand and depends on dynamic conditions.
Do-while loops: Similar to while loops but guarantees at least one execution of the loop body.
For loops: Best for situations where the number of iterations is predetermined.
Understanding Loops
Definition:
A loop is a control structure that allows the execution of a statement or block repeatedly until a certain condition is met, facilitating efficient coding practices and minimizing redundancy.
Purpose of Loops:
Algorithms often require repeating the same action multiple times, such as:
Summing numbers until reaching a specified total.
Collecting user input until a termination command, such as 'exit', is issued.
Reading and processing lines from a file until the end-of-file (EOF) is reached.
Types of Loops in C++:
While: Executes as long as the specified boolean condition is true. Ideal for scenarios where the termination condition may change during the loop execution.
Do-While: Executes the code block once before checking the boolean condition, ensuring that the loop body runs at least one time.
For: Typically structured for scenarios involving known iteration counters, such as iterating through arrays or lists.
Loop Phases
Basic Phases of a Loop:
Initialization: Establish necessary variables or data structures before the loop begins to ensure proper control.
Loop Entry: Represents the first execution of the loop, where conditions are evaluated.
Iteration: Each pass through the loop; conditions are re-evaluated to decide if further iterations should occur.
Termination: The condition fails when the requirements for looping are no longer satisfied, leading to an exit.
Loop Exit: Returns control to the main program, allowing for further operations.
While Loops
Functionality:
Operates based on a boolean condition, executing the loop body as long as that condition evaluates to true.
Flow Diagram:
Previous statement
While (condition)
Next statement Important Notes:
The statement within must be capable of altering the boolean condition during execution, ensuring the loop does not become infinite.
Example Scenario:
Collecting windfall apples until a predefined limit is reached, such as filling a bucket.
Syntax:
while (condition) {
// Execution block
}Do While Loops
Functionality:
Similar to a while loop, but checks the condition after the loop body has executed at least once, ensuring at least one iteration.
Flow Diagram:
Previous statement
Statement
Next statement
Do While (condition) Example Use Case:
Collecting user input until a specific termination character, such as 'x', is entered.
Syntax:
do {
// Execution block
} while (condition); Differences Between While and Do While:
While Loop: Condition is evaluated before any execution.
Do While Loop: Guarantees execution of the block at least once before condition checking.
For Loops
Purpose:
Primarily used for counting iterations and executing a block for a set number of times.
Syntax:
for (initialization; condition; alteration) {
// Execution block
} Example: Counting to 10:
for (int iCounter = 1; iCounter < 11; iCounter++) {
cout << iCounter << endl;
}Nested Loops
Concept:
Involves placing one loop inside another, allowing for more complex iteration structures and handling multidimensional data, such as matrices.
Choosing Loop Types
Primary choices in looping:
Counters: Often utilized in for loops where the number of iterations is predetermined.
Events: Related specifically to user input or other dynamic conditions monitored during execution.
Flags: Boolean variables indicating a specific occurrence that affects loop execution or termination.
Flag Driven Loops
Definition:
A flag usually represents a boolean variable that indicates whether a certain condition is satisfied, determining if the loop should continue or exit.
Example:
Prompting a user to enter a password, where the loop continues until the correct password is entered or a limited number of attempts have been made.
Key Takeaways on Iteration
Repetition: Central to loop functionality is the ability to repeat actions efficiently, minimizing the need for redundant code, whether counting iterations with a For loop or waiting for an event with While or Do-While loops.