This page is a work in progress.You can help improve it. →

Missing Document Title

theme: Next, 1

[fit] Control Flow


Control Flow

Sequential lines of code are evaluated in a row.

To make decisions and take different paths we need control flow


If Statements

Simplest of the control flow statements.


Basic structure:

if (someBooleanCondition) {
// Code to run if the condition is true
}

If / Else statements

Can also handle the case when the condition is false

if (someBooleanCondition) {
// Code if the condition is true
}
else {
// Code if the condition is false
}

^ The part someBooleanCondition can be a boolean variable or a condition that evaluates to a boolean value.


Boolean Conditions

Examples of boolean conditions

KindExample
equalityname == "Paul"
inequalityname != "Paul"
greater thanscore > 90
less thanscore < 60
greater or equal toscore >= 90
less or equal toscore <= 60

Example

if (name == "Paul") {
Console.WriteLine("Greetings!");
} else {
Console.WriteLine("Wait, who are you?");
}

Multiple Booleans: Or, And

Combine two conditions.

Or is written as || while And is written as &&

[.column]

Condition that is true if the score is lower than 20 OR greater than 90:

if (score < 20 || score > 90) {
// Some code here
}

[.column]

Condition that is true if the person is named Paul AND the score is more than 85:

if (name == "Paul" && score > 85) {
// Some code here
}

Truth Table

[.column]

A && B

ABResult
TTT
TFF
FTF
FFF

[.column]

A || B

ABResult
TTT
TFT
FTT
FFF

^ Don't memorize these, but if you can remember them it will help you write better if statements


Chaining

A series of if/else statements can be chained.

if (name == "Paul") {
Console.WriteLine("Here");
} else if (name == "Dorothy") {
Console.WriteLine("Also here");
} else if (name == "Sam") {
Console.WriteLine("Here again");
} else {
Console.WriteLine("Didn't find anything");
}

[fit] We can do better


Introducing switch


switch (name)
{
case "Paul":
Console.WriteLine("Here");
break;
case "Dorothy":
Console.WriteLine("Also Here");
break;
case "Sam":
Console.WriteLine("Here Again");
break;
default:
Console.WriteLine("Didn't find anything");
break;
}

Other neat switch features

[.column]

We can handle multiple values by repeating the case statement:

In this code we will see the message Here for name if it is either Paul OR Peter OR Mary

[.column]

switch (name)
{
case "Paul":
case "Peter":
case "Mary":
Console.WriteLine("Here");
break;
case "Dorothy":
Console.WriteLine("Also Here");
break;
case "Sam":
Console.WriteLine("Here Again");
break;
default:
Console.WriteLine("Didn't find anything");
break;
}

Case conditionals

[.column]

Let's say we are working with an int variable named score and we wanted to print a grade associated to a score.

[.column]

var score = 95;
switch (score)
{
case < 65:
Console.WriteLine("F");
break;
case < 70:
Console.WriteLine("D");
break;
case < 80:
Console.WriteLine("C");
break;
case < 90:
Console.WriteLine("B");
break;
case >= 90:
Console.WriteLine("A");
break;
default:
Console.WriteLine("Hmmm, I don't recognize this score");
break;
}

[fit] What about repeating code?

What about repeating code?

What about repeating code?

What about repeating code?

🐢🐢🐢🐢🐢🐢🐢🐢


[fit] Loops


[fit] while


While

The while statement repeats the code inside the { } braces as long as the condition supplied remains true.


[.column]

Ask the user their name and greet them until the user enters the text quit. The code would look like this:

[.column]

Console.Write("What is your name? ");
var name = Console.ReadLine();
while (name != "quit") {
Console.WriteLine($"Hello {name}");
Console.Write("What is your name? ");
name = Console.ReadLine();
}
© 2017 - 2022; Built with ♥ in St. Petersburg, Florida.