diff options
| author | Christian Kolset <christian.kolset@gmail.com> | 2025-09-10 14:21:30 -0600 |
|---|---|---|
| committer | Christian Kolset <christian.kolset@gmail.com> | 2025-09-10 14:21:30 -0600 |
| commit | e6244e2c4f1c8b241f822cdf25b36fb3fa6cc33e (patch) | |
| tree | f47373c6e0d3d1d92feb14f91c04b66603ab5068 /tutorials/module_1 | |
| parent | 9842533cf12a9878535b3539291a527c1511b45c (diff) | |
Added newton-raphson material and inclused recursive vs iterative approaches
Diffstat (limited to 'tutorials/module_1')
| -rw-r--r-- | tutorials/module_1/control_structures.md | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/tutorials/module_1/control_structures.md b/tutorials/module_1/control_structures.md index 0a0abc6..68db0c5 100644 --- a/tutorials/module_1/control_structures.md +++ b/tutorials/module_1/control_structures.md @@ -4,10 +4,13 @@ Control structures allow us to control the flow of execution in a Python program. The two main types are **conditional statements (`if` statements)** and **loops (`for` and `while` loops)**. +[Input complete flowchart here] ## Conditional Statements Conditional statements allow a program to execute different blocks of code depending on whether a given condition is `True` or `False`. These conditions are typically comparisons, such as checking if one number is greater than another. +[Image of a conditional statement] + ### The `if` Statement The simplest form of a conditional statement is the `if` statement. If the condition evaluates to `True`, the indented block of code runs. Otherwise, the program moves on without executing the statement. @@ -55,7 +58,7 @@ if 0 <= score <= 100: if retake_eligible == "yes": print("The student is eligible for a retest.") else: - print("The student has failed the course and must retake it next semester.") + print1("The student has failed the course and must retake it next semester.") ``` @@ -65,6 +68,8 @@ if 0 <= score <= 100: Loops allow a program to execute a block of code multiple times. This is especially useful for tasks such as processing lists of data, performing repetitive calculations, or automating tasks. +[Input image of flowchart loop here] + ### The `for` Loop A `for` loop iterates over a sequence, such as a list, tuple, string, or a range of numbers. Each iteration assigns the next value in the sequence to a loop variable, which can then be used inside the loop. @@ -104,7 +109,7 @@ while i < 6: i += 1 ``` -## Loop Control Statements +### Loop Control Statements Python provides special statements to control the behavior of loops. These can be used to break out of a loop, skip certain iterations, or simply include a placeholder for future code. @@ -125,3 +130,11 @@ For example, if we are iterating over numbers and want to skip processing number The `pass` statement is a placeholder that does nothing. It is useful when a block of code is syntactically required but no action needs to be performed yet. For example, in a loop where a condition has not yet been implemented, using `pass` ensures that the code remains valid while avoiding errors. + +## Recursion vs. Iteration +In computer logic it is possible for a function to call itself, this is called *recursion*. + +> The power of recursion evidently lies in the possibility of defining an infinite set of objects by a finite statement. In the same manner, an infinite number of computations can be described by a finite recursive program, even if this program contains no explicit repetitions. + — [Niklaus Wirth](https://en.wikipedia.org/wiki/Niklaus_Wirth "Niklaus Wirth"), _Algorithms + Data Structures = Programs_, 1976 + +Whilst they are similar, computationally they differ. |
