Basic Operations and Syntax

Your Progress in this Chapter

0%

0 / 12 units completed

Chapter Overview

Algorithm Flow Control

Section 1.5 of 612 min5 code examples

Algorithm Flow Control

Algorithms are rarely linear. In numerical analysis, we must make decisions based on error thresholds and repeat operations until a solution converges. Flow control allows us to dictate the path our code takes using conditional logic and iterative loops.

1. Conditional Logic (if-else and switch)

Decisions in C# are handled primarily by if and switch statements. While if is perfect for range-based checks (like error tolerances), switch is ideal for choosing between discrete "modes" or "states" of an algorithm.

Example 1C#

1
2
3
4
5
6
7
8
9
10
11
12
13
Code is ready to run

2. Iterative Loops

Loops allow us to repeat a block of code. For numerical methods, the for loop is king when we know the number of iterations, while the while loop is used when we wait for a specific condition to be met.

[Image comparison of for-loop, while-loop, and foreach-loop structures]

Loop TypeBest Use CaseKey Characteristic
forStepping through matrices with an index.Precise index control.
foreachIterating over all nodes in a mesh.Read-only and safe.
whileRunning a solver until convergence.Condition-based exit.
do-whileWhen the first iteration must occur.Post-condition check.

Examples

.. Admonition:: Example 1 : The If-Condition: Safety Checks

Before performing a division in a numerical formula, such as normalizing a vector, it is vital to ensure we aren't dividing by zero. An if statement acts as a "Guard Clause" to keep the solver stable.

Example 2C#

1
2
3
4
5
6
7
8
9
10
Code is ready to run
OutputFrom the book
Inversion successful: 100000000

.. Admonition:: Example 2 : The Switch-Condition: Solver Selection

In a multi-physics application, you might need to switch between different integration schemes. The switch statement makes this choice clear and organized compared to a long chain of if-else blocks.

Example 3C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Code is ready to run
OutputFrom the book
Executing Runge-Kutta 4th Order...

.. Admonition:: Example 3 : The While-Loop: Convergence Monitor

A while loop is the heartbeat of iterative methods. In this example, we simulate a cooling process where we keep calculating the temperature until it reaches the ambient environment temperature.

Example 4C#

1
2
3
4
5
6
7
8
9
10
11
Code is ready to run
OutputFrom the book
Cooled to 25.10°C in 63 seconds.

.. Admonition:: Example 4 : The Foreach Loop: Property Calculation

When you need to perform an operation on every element in a collection, like calculating the total mass of all elements in a structural model, foreach is the most readable choice because it eliminates index management.

Example 5C#

1
2
3
4
5
6
7
8
9
10
Code is ready to run
OutputFrom the book
Total System Mass: 19.1

Pro-Tip: Nested Loops

When working with 2D Matrices, you will often nest a for loop inside another. Remember that C# stores arrays in row-major order; iterating through rows in the outer loop and columns in the inner loop is significantly faster due to CPU cache optimization.