Secure Coding Lecture 04 - Iterations
De Montfort University Leicester
This course emphasizes collaboration with industry partners in developing the Cyber-Jargon Iteration (CJi) framework, which focuses on secure coding practices through iterative processes.
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
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.
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.
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.
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.
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.
Operates based on a boolean condition, executing the loop body as long as that condition evaluates to true.
Previous statement
While (condition)
Next statement
The statement within must be capable of altering the boolean condition during execution, ensuring the loop does not become infinite.
Collecting windfall apples until a predefined limit is reached, such as filling a bucket.
while (condition) {
// Execution block
}
Similar to a while loop, but checks the condition after the loop body has executed at least once, ensuring at least one iteration.
Previous statement
Statement
Next statement
Do While (condition)
Collecting user input until a specific termination character, such as 'x', is entered.
do {
// Execution block
} while (condition);
While Loop: Condition is evaluated before any execution.
Do While Loop: Guarantees execution of the block at least once before condition checking.
Primarily used for counting iterations and executing a block for a set number of times.
for (initialization; condition; alteration) {
// Execution block
}
for (int iCounter = 1; iCounter < 11; iCounter++) {
cout << iCounter << endl;
}
Involves placing one loop inside another, allowing for more complex iteration structures and handling multidimensional data, such as matrices.
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.
A flag usually represents a boolean variable that indicates whether a certain condition is satisfied, determining if the loop should continue or exit.
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.
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.