Branching

If Statements

An If Statement is a branching structure which contains a sequence of scopes with associated conditions. The only scope entered is the first of the sequence whose condition is met.

  • The statements or blocks of code that follow only execute if condition is true.

  • If condition is false, skip.

//if Statement syntax
if (/*condition*/)
{
	//statement block
}

//everything after will execute normally

RULES of If Statements

  • The condition must evaluate to bool (true/false)

  • NO semicolon after the parentheses

  • Curly braces are not needed if the condition is a single statement

//Example

Console.WriteLine("Enter your age");
int age = int.Parse(Console.ReadLine());

if (age >= 18){
	Console.WriteLine("You are an adult");
}

If-Else Statements

This is statement offers another option for the program to execute if the condition evaluates to false.

  • If and else are mutually exclusive

  • There is no condition for the else statement. It is given that else has a condition that covers every possible condition other than what is in the if.

It is VERY important to note that when a condition is met, this means that the boolean expression evaluates to true.

decimal price;

Console.WriteLine("Please enter your age");
int age = int.Pare(Console.ReadLine());

if(age < 18){
	price = 5;
else
	price = 8;

//price = (age<18)?5:(age<=65)?6:8;


Console.WriteLine($"Your cost is {price : C}.");

Combining Conditions in Branching Statements

  • In C#, logical operators can be used to combine conditions.

//Example
bool isSprinkling = true;
bool hasRainjacket = true;

if (isSprinkling && !hasRainjacket){
	Console.WriteLine("Stay Inside")
} else if (isSprinkling && hasRainjacket){
	Console.WriteLine("Take your jacket with you!");
}

Best Practices

  • To write clear and maintainable code:

    • Always use braces {}, even for single-line if-statements

    • Indent your code consistently

    • Order conditions from most specific to most general

    • Use meaningful variable names that make conditions readable

    • Test edge cases (like 0, negative numbers, or boundary conditions)

Lecture Examples

/*Lecture Example 1

Let the user pick their first creature. Depending on which of the 3 is selected, pick the creature that rivals it */

string selection, rival;
Console.WriteLine("Please Select a Creature (charizard, water, Canada): ");
selection = Console.ReadLine();

if (selection == "charizard"){
	rival = "water";
} else if (selection == "water"){
	rival == "Canada";
} else if (selection == "Canada"){
	rival == "charizard";
} else{
	Console.WriteLine("We don't have that option");
}

Console.WriteLine("You picked " + selection + " so your rival chose " + rival + ".");

/*Lecture Example 1.5

Modify the code from the previous example to ask the user if they overslept. If they did oversleep, then force them to choose a cute mascot character. The rival will then take the Fox Dog.*/

string selection, rival;
bool overslept = false;

Console.WriteLine("Did you oversleep?");
overslept = bool.Parse(Console.ReadLine());

if(!overslept){
	Console.WriteLine("Please Select a Creature (charizard, water, 			
	Canada): ");
	selection = Console.ReadLine();

	if (selection == "charizard"){
		rival = "water";
	} else if (selection == "water"){
		rival == "Canada";
	} else if (selection == "Canada"){
		rival == "charizard";
	} else{
	Console.WriteLine("We don't have that option");
	}
} else{
	selection = "Panda";
	rival = "Fox Dog";
}

Console.WriteLine("You picked " + selection + " so your rival picked " + rival + ".");
//Lecture Example 2

static void Main(){
	Console.WriteLine("What's the weather");
	Console.WriteLine($"{SuggestActivity(Console.ReadLine())}");
}

public string SuggestActivity(string weather){
	String activity = "?";
	if(weather == "rainy"){
	activity = "watch movie";
	} else if (weather == "sunny") {
		activity = "walk";
	} else if (weather == "cloudy"){
		activity = "homework";
	} else{
		activity = "sleep";
	}
	
	return activity;
}