If Conditional Statements

Overview

In this lesson we focus on if conditional statements, their syntax and why they are so powerful.

The Code

In this program you will write the following code:

class Program {

	static void Main(string[] args) {
		Console.WriteLine("How old are you?");
		
		int age = Convert.ToInt32( Console.ReadLine() );
		
		if (age < 18) {
			Console.WriteLine("Sorry, you are too young to view this content.");
		} else if (age == 18) {
			Console.WriteLine("Wooohooo, you barely made it!");
		} else {
			Console.WriteLine("Welcome to the site");
		}
		
		Console.Read();
	}
	
}
  • else if - Notice how we combine multiple if conditional statements together to make a large code block. This is very important because a single block of if conditional statements will exit the block after the first true check.

Checking for Understanding | Coding Errors

Some assignments lesson will have you repair broken code based on what you have learned. These lessons are shared via .NET Fiddle, which is an online IDE and complier for multiple coding languages. For these lessons you just need to find the errors, fix them, and copy and paste your code into the corresponding Google Form.

Deliverable

Using your knowledge and code from this lesson to create a simple site validation that only allows users 18 or older into the site, then asked only validated users (over 18) if they would like to sign up for a weekly newsletter. You must give the user feedback based on their response, like we did above.

Assignments in this module are handed in via Google Classroom. Make sure you are signed into your PLSD account and click the link below:

Mark as Complete
chevron_right