(All this was done in notepad with little coffee, please point out any mistakes..)
Conditionals
Conditionals are used to control program flow.
Since we are teaching C#, I will show the common C# conditionals, but these
apply to all programming languages, at least in concept.
These are mostly self explanatory, but I will expand on them all.
IF
An if statement checks a condition. If (pun intended) that condition evaluates to true, then the next associated code block or statement will execute.
string name = "Kevin"; //Assign a name
if(name == "Kevin") //Check a condition
{//This is a code block, and it will execute if the above condition is true.
MessageBox.Show("Awesome");
}
Output
Awesome
string name = "Kevin"; //Assign a name
//Check a condition
if(name == "Kevin") MessageBox.Show("Awesome"); //This is also valid, but this executes the next statement only up to the semicolon.
//The next statement is not part of the "if" above.
Console.WriteLine(name + " is a smoking monkey.");
Output
Awesome
Kevin is a smoking monkey.
IF-ELSE
A little more complicated, but not by much.
An if-else statement checks a condition. If that condition evaluates to true, then the next associated code block or statement will execute, if it evaluates to false, then the associated code block or statement after the “else” will execute.
string name = "Kevin V"; //Assign a name
if(name == "Kevin") //Check a condition
{//This is a code block, and it will execute if the above condition is true.
MessageBox.Show("Awesome");
}
else
{//This is a code block, and it will execute if the first condition (the if condition) is false.
MessageBox.Show(name + " - The jury is still out.");
}
Output
Kevin V - The jury is still out.
Ladder Type IF-ELSE
Hmmm, how to explain this? Maybe just show you…
string name = "Mark"; //Assign a name
if(name == "Kevin") //Check a condition
{//This is a code block, and it will execute if the above condition is true.
MessageBox.Show(name + " - Awesome, but easily distracted.");
}
else if(name == "Jose") //Check another condition
{//This is a code block, and it will execute if the above condition is true.
MessageBox.Show(name + " - Awesome as well, has a tldr issue.");
}
else if(name == "Mark") //Check another condition
{//This is a code block, and it will execute if the above condition is true.
MessageBox.Show(name + " - Awesome, but a huge corndog.");
}
else //None of the above were true so... execute the following code block:
{
MessageBox.Show("Who am I kidding, you are all awesome.");
}
Output
Mark - Awesome, but a huge corndog.
SWITCH
Ahh, good old select-case ![]()
This is what you use when you need to test a lot of different values (“cases”) against a single condition / expression.
Now switch has a few rules to remember.
- The data types of what you are checking and the case must be the same.
- Case values must be a constant, can’t use a variable.
- No duplicates. (Including default)
- The “default” case is optional.
- “break” is used to terminate a “case”.
- You can use multiple case statements, and “fall through” to a code block or statement.
Here is an example:
string name = "Bryan"; //Assign a name
switch(name) //We will be checking the name variable..
{
case "Brandon": //"Brandon" is a constant string, and constantly cranky, but that's ok.
//This will execute if "name" is "Brandon".
Console.WriteLine(name + " - Wore thicker soled shoes so he could be taller than me");
break; //This is the end of this case statement.
case "Hannah":
//This will execute if "name" is "Hannah".
Console.WriteLine("Was missed at Insights 2024.");
break; //This is the end of this case statement.
case "Simon":
case "Bryan": //We'll enter here, and fall on through
case "Haso":
//This will execute if "name" is "Simon", "Bryan", or "Haso"
Console.WriteLine(name + " - I can't think of anything funny at the moment.");
break; //This is the end of this case statement.
default: //Optional catch all... This will execute if everything else fails
Console.WriteLine( "Sorry, I can't put EVERYONE in the examples, but I'll try ;)" );
break; //This is the end of this default statement.
}
Output
Bryan - I can't think of anything funny at the moment.
Also notice the colon beside the “case”… Yep, see that do ya? a case statement is a label..
Which means, you can alter the logic with “goto:”
Usually that is done by replacing the “break” statement with “goto”.
Most often used when you want to execute the “default” statement AND the “case” statement.
List<string> names = new List<string>(){"Mike", "Greg"}; //Make a list of names
foreach(string name in names)
{
switch(name) //We will be checking the name variable..
{
case "Mike":
Console.WriteLine(name + " - Cannot be tamed.");
goto default:
case "Evan":
Console.WriteLine(name + " - Has very long private messages..");
goto default:
case "Greg":
Console.WriteLine(name + " - Knows way more than me.");
goto case "Woot": //I jumped to a "case"
case "Woot":
Console.WriteLine(name + " - This will execute also if the name is Greg... or 'Woot' obviously.."); //Why, no idea, but you can..
goto default:
default: //In this case, this will always run..
Console.WriteLine( "I like to make silly examples.." );
break; //This is the end of this default statement.
}
}
Output
Mike - Cannot be tamed.
I like to make silly examples..
Greg - Knows way more than me.
Greg - This will execute also if the name is Greg... or 'Woot' obviously..
I like to make silly examples..
Nesting…
All of these can be nested, so you can have conditionals inside conditionals… ![]()
Simple Example, IF, with a NESTED IF-ELSE:
string name = "Chris"; //Assign a name
if(name.StartsWith("C"))
{
if(name == "Chris")
{
Console.WriteLine(name + " - Likes to make 'interesting' music...");
}
else Console.WriteLine("Insert poop joke?"); //Notice, just a statement, no code block... This is valid. Recommmended ? Ahh depends, subjective, just make sure it's clear.
}
Output
Chris - Likes to make 'interesting' music...
I’m not doing a NESTED SWITCH, they look ridiculous, but can be done ![]()
Thus concludeth todayeth metthage, it ith knowenth.


