Lesson Weekend

As we learned in Intro, we can use branching to execute different code depending on specific conditions. In this lesson, we'll look at how C# implements branching using booleans.

Booleans


As we covered in Intro, booleans can hold one of two values: true or false. We can use the C# equality operator == to see if one thing is equal to another. If it is true, C# returns a boolean with a true value. If it isn't, C# will return a false value.

In Intro, we used JavaScript's strict equality operator === to determine whether two things are completely identical, including their types. In C#, we only use two == to evaluate equality.

Comparison Operators and Booleans

Let's open the C# REPL (with the $ dotnet script command) and observe several operators that return booleans.

We can compare two strings like this:

> "1" == "1"
true

The same is true for int data types:

> 1 == 1
true

If we compare things that aren't the same, C# will return false:

>  "1" == "2"
false

bool Variables

In C#, we can declare boolean variables with the bool data type:

> bool mathIsWrong = 1 > 2;

> mathIsWrong
false

> bool mathIsRight = 1 < 2;

> mathIsRight
true

Booleans and Branching

Let's write a program that uses an if/else statement and booleans to determine whether someone is old enough to see an R-rated film in theaters.

Create a directory with a project file and a file called RatedR.cs. We'll start with the following logic:

RatedR.cs
using System;

class RatedR
{
  static void Main()
  {
    Console.WriteLine("How old are you?");
    string stringUserAge = Console.ReadLine();
    int intUserAge = int.Parse(stringUserAge);
  }
}

In the code above, we:

  • Add the necessary boilerplate code, including using System;, a class RatedR with a set of curly brackets that matches our file name, and a Main() method.
  • Within the Main() method, we add three lines of code.
    • The first prints the question "How old are you?" to the console.
    • The second uses the Console.ReadLine() method to retrieve user input, then saves the user in put in string variable called stringUserAge.
    • The third line converts the string stringUserAge into an int value.

Now that we have the user's age, we can use branching to return a message to the user based on whether the user is at least 17 years old:

RatedR.cs
using System;

class RatedR
{
  static void Main()
  {
    Console.WriteLine("How old are you?");
    string stringUserAge = Console.ReadLine();
    int intUserAge = int.Parse(stringUserAge);

    if (intUserAge >= 17)
    {
      Console.WriteLine("You can see the movie!");
    }
    else
    {
      Console.WriteLine("I'm sorry, you are too young to see the movie.");
    }
  }
}
  • We use the relational operator (>=) to check if intUserAge is greater than or equal to 17.
  • Then we use branching to print a message that lets the user know if they can see the movie.

We can compile and launch the program with the $ dotnet run command.

Methods Returning Booleans

There are also C# methods that return bools. Let's create a statement using one of these methods in a new program file called StartsWithZ.cs. (Note that you'll need to remove the earlier RatedR.cs file, or comment out Main() because you can't have two entry points in the same project.) We'll begin with the following code:

StartsWithZ.cs
using System;

class FirstLetterChecker
{
  static void Main()
  {
    Console.WriteLine("What is your name?");
    string userName = Console.ReadLine();
    if (userName.StartsWith("Z"))
    {
      Console.WriteLine("Your name starts with a Z!");
    }
    else
    {
      Console.WriteLine("Your name doesn't start with a Z :(");
    }
  }
}

After our if statement, we use the built-in C# method StartsWith to store userName.StartsWith("Z") in parentheses. This is called a conditional statement. This conditional statement will take the string argument passed to it and compare it to the first letter of userName. It will return true if it's equal to the argument and false if it isn't.

Other Operators


Here are the operators we can use in C#:

Equality

Operator Definition Example
== equal to 1 == 1 is true. 1 == 2 is false.
!= not equal to 1 != 2 is true. 1 != 1 is false.

Relational

Operator Definition Example
> greater than 2 > 1 is true. 1 > 2 is false.
>= greater than or equal to 2 >= 2 and 2 >= 1 are both true. 2 >= 3 is false.
< less than 1 < 2 is true. 2 < 1 is false.
<= less than or equal to 1 <= 1 and 1 <= 2 are both true. 2 <= 1 is false.

Conditional Example


if (intUserAge >= 17)
{
  Console.WriteLine("You can see the movie!");
}
else
{
  Console.WriteLine("I'm sorry, you are too young to see the movie.");
}

Equality Operators


Operator Definition Example
== equal to 1 == 1 is true. 1 == 2 is false.
!= not equal to 1 != 2 is true. 1 != 1 is false.

Relational Operators


Operator Definition Example
> greater than 2 > 1 is true. 1 > 2 is false.
>= greater than or equal to 2 >= 2 and 2 >= 1 are both true. 2 >= 3 is false.
< less than 1 < 2 is true. 2 < 1 is false.
<= less than or equal to 1 <= 1 and 1 <= 2 are both true. 2 <= 1 is false.

Lesson 1 of 9
Last updated more than 3 months ago.