2.4: if, else if, else

In this module we will go deeper into boolean logic and learn about the else if and else keywords.

File Location

  1. Right click on the solution you made in the first module

  2. Go to Add > New Project and select Console App (.NET Framework)

  3. Name it 0.02_Booleans_Conditionals

  4. Write your code for this module within Program.cs of that project

Description

A conditional statement is a statement that can be executed based on a condition. Two different types of conditionals are looping and branching. Some different branching statements are switch cases, if statements, and ternary statements. Here we will go into some fun ways to move data around.

If, Else If, and Else Statements

In the previous module on booleans, we got a brief introduction to some if statements. if statements check to see if a condition is true or false.

 class Program
    {
        static void Main(string[] args)
        {
        //  1   2     3     4
            int value = 100 / 2;
        //  5   6
            if (value == 50)
            {
        //      7
                Console.WriteLine(true);
            }
            Console.ReadLine();
        }
    }

Above, we checked to see if a number is equal to 50.

  1. DataType

  2. Name we associate to value we are declaring

  3. Equals operator

  4. We set value to 100 divided by 2. 50

  5. Declaring our if statement

  6. Boolean condition asking if value is equal to 50

  7. Code executed if statement is true.

Let's take a look at some more boolean logic with if, else if, and else.

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a number");
            int UserNumber = int.Parse(Console.ReadLine());

            if (UserNumber == 1)
            {
                Console.WriteLine("Your number is 1");
            }
            else if (UserNumber == 2)
            {
                Console.WriteLine("Your number is 2");
            }
            else if (UserNumber == 3)
            {
                Console.WriteLine("Your number is 3");
            }
            else
            {
                Console.WriteLine("Wrong Number");
            }
        }
    }

Discussion

  1. In the program above we have the user enter in a number.

  2. It will then check if the number is 1.

  3. If it is not one, it will check if it is 2.

  4. If it is not 2, it will check if it is 3.

  5. If it is none of the above, it will print Wrong Number.

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How are you feeling today from 1-5?");
            string feelingNumber = Console.ReadLine();
            if (feelingNumber == "5")
            {
                Console.WriteLine("That's great to hear!");
            }
            else if (feelingNumber == "4")
            {
                Console.WriteLine("Good stuff!");
            }
            else if (feelingNumber == "3")
            {
                Console.WriteLine("Hope things get better!");
            }
            else if (feelingNumber == "2")
            {
                Console.WriteLine("Oh. Sorry to hear that.");
            }
            else if (feelingNumber == "1")
            {
                Console.WriteLine("Dang. We hope your day gets better!");
            }
            else
            {
                Console.WriteLine("Sorry, we don't understand. Come back later.");
            }
                Console.ReadLine();
        }
    }

Discussion

Above is an example of a program that uses if, else if, and else statements with an 'is equal to' operator or == to display a result depending on what the user inputs. You can see the program checks whether the user's input is true or false compared to the different statements.

else if statements are run if the previous if statements are resulted to false. else statements are run if all previous statements are resulted to false.

Multiple if Statements

If you have multiple if statements within a branch your program will check all those if statements even if a previous one resulted in true. Not a bad thing, but handy to know especially when you get into the speed of your application or are only wanting to get one result back from the branch. Consider the following:

    class Program
    {
        static void Main(string[] args)
        {
            bool isOn = true;
            bool isHot = false;

            if (isOn)
            {
                Console.WriteLine("The light is on, it's bright.");
            }

            //2
            if (isOn == true)
            {
                Console.WriteLine("The light is on.");
            }

            //3
            if (isOn && isHot)
            {
                Console.WriteLine("lights on and it's hot");
            }

            //4
            if (isOn || isHot)
            {
                Console.WriteLine("lights on or it is hot");
            }

            //5
            if (!isHot)
            {
                Console.WriteLine("it is not hot");
            }
        }
    }

Another Example

At Eleven Fifty, we believe that in order to gain literacy with code, you have to read as much code as you write. For the following exercise, coopy the code into a new console app called Bank_Pro

class Program
    {
        static void Main(string[] args)
        {
            var bankAccount = 10000;
            var debt = 4200;
            var difference = bankAccount - debt;

            Console.WriteLine("I have $" + bankAccount + " in my bank account, and I am $" + debt + " in debt.");

            if (difference >= 700 && difference <= 1999)
            {
                Console.WriteLine("I have extra money. I should pay off my debt. I'll have $" + difference + " left.");
            }
            else if ((bankAccount - debt > 2000))
            {
                Console.WriteLine("I have a ton of extra cash right now. I think I could go to Europe!");
            }
            else
            {
                Console.WriteLine("It probably isn't a good time to pay off my debt.");
            }
            Console.Read();
        }
    }

Next: Switch Statements

Last updated