Write algorithms with decision-making (branching, looping).
Algorithms
An algorithm is a finite sequence of well-defined, ordered steps that, when executed, solve a specific problem or accomplish a particular task. Algorithms are fundamental to computer science and are the basis for creating computer programs.
Key characteristics of an algorithm:
Finite: It must terminate after a finite number of steps.
Input: It may have zero or more inputs.
Output: It must produce one or more outputs.
Definiteness: Each step must be unambiguous and clearly defined.
Effectiveness: Each step must be practically executable.
Decision Making (Branching)
Decision-making in algorithms involves using conditional statements to execute different blocks of code based on whether a certain condition is true or false. Common branching structures include:
if: Execute a block of code only if a condition is true.
else: Execute a block of code if the initial condition is false.
else if: Check multiple conditions sequentially.
switch (or case): Select one of several blocks of code to execute based on the value of an expression.
Looping
Looping allows a block of code to be executed repeatedly until a certain condition is met. Common looping structures include:
while: Execute a block of code repeatedly as long as a condition is true.
for: Execute a block of code a specified number of times, often using a counter variable.
do-while: Similar to 'while', but the code block is executed at least once before the condition is checked.
Example Algorithm: Finding the Maximum of Two Numbers
Here's an algorithm to find the maximum of two numbers:
Input: Get two numbers, 'num1' and 'num2'.
Compare: If 'num1' is greater than 'num2', go to step 3. Otherwise, go to step 4.
Output: The maximum is 'num1'.
Output: The maximum is 'num2'.
Flowcharts
A flowchart is a diagrammatic representation of an algorithm. It uses standardized symbols to illustrate the flow of control and the sequence of operations. Flowcharts are useful for visualizing algorithms and making them easier to understand.
Suggested diagram: A simple flowchart illustrating the maximum of two numbers algorithm using standard symbols (start, process, decision, end).
Flowchart Symbols
Symbol
Description
Represents the start and end of an algorithm.
Represents a processing step or operation.
Represents a decision point where the flow of control can branch.
Represents input or output operations.
Used to connect different parts of the flowchart.
Example Flowchart: Finding the Maximum of Two Numbers
Here's a flowchart representing the algorithm for finding the maximum of two numbers:
Suggested diagram: A flowchart illustrating the maximum of two numbers algorithm using standard symbols (start, process, decision, end). The flowchart should show the input of num1 and num2, a process to compare them, a decision diamond, and outputs for the maximum.
Example Algorithm: Calculating the Factorial of a Number
Here's an algorithm to calculate the factorial of a non-negative integer:
Input: Get a non-negative integer, 'n'.
Initialize: Set 'factorial' to 1.
Loop: While 'n' is greater than 0, do the following: