If, Else, C#

Advertisements

Hello readers,

Michael here, and in today’s post, we’re going to explore the wonderful world of if-else statements (aka conditionals) in C#.

Now, you’ve seen me cover conditionals in Java and Python, but let’s see how we do it in C#.

A truth tables refresher

One thing that is important when writing conditionals or Boolean statements in programming is the concept of truth tables, like so:

The three most common logical operators you will likely use when writing Boolean statements and conditionals in programming are & (and), | (or) and ! (not).

Here are some other things to keep in mind with truth tables:

  • In C#, much like in other programming tools we’ve covered, statements are read from left to right for the most part, so the leftmost Boolean statements will be evaluated first.
  • One exception to what I just mentioned-if you have a statement in parentheses, that will be the first one read. For instance, if your statement goes A & B | (C & D), C & D would be read first as it is the statement in parentheses.
  • Following what I just said, if you have several statements inside several nested parentheses, the innermost statement will be read first. For instance, if your statement goes A | B | C & (D & (E | F)), E | F would be read first as it is the statement in the innermost pair of parentheses.

A basic if-else statement, C# style

Now, how do we write a basic if-else statement, C# style. Take a look at my example below:

Console.WriteLine("Pick a number between 0 and 100");
String userNumber = Console.ReadLine();
int userNumberInt = int.Parse(userNumber);
String grade = "";

if (userNumberInt >= 70 | userNumberInt <= 100)
{
    grade = "pass";
}
else
{
    grade = "fail";
}

Console.WriteLine("Your grade is a " + grade);

Pick a number between 0 and 100
72
Your grade is a pass

Using the American school grading system as an example, in this script I ask the user for a number from 0 to 100, which represents possible class grades. If the number is between 70 and 100, the grade is set to pass (think of it as needing a C- to pass). Otherwise, the grade is set to fail (think of it as a D+ or lower is failing).

  • You might be wondering about the int.Parse([insert string here]) method. This is one way to convert strings in C# into integers. It’s also known by the name typecasting, much like in Python when we converted strings into integers simply by typing int([string here]).

If, Else, Nested, C#

Now, as we saw in some of the other tools I’ve covered, it is possible to nest if-else statements inside other if-else statements. How can we do so? Take a look at the code below:

Console.WriteLine("Pick a number between 0 and 100");
String userNumber = Console.ReadLine();
int userNumberInt = int.Parse(userNumber);
String grade = "";

if (userNumberInt >= 90 & userNumberInt <= 100)
{
    if (userNumberInt >= 97 & userNumberInt <= 100)
    {
        grade = "A+";
    }
    else if (userNumberInt >= 93 & userNumberInt <= 96)
    {
        grade = "A";
    }
    else
    {
        grade = "A-";
    }
}

else if (userNumberInt >= 80 & userNumberInt <= 89)
{
    if (userNumberInt >= 87 & userNumberInt <= 89)
    {
        grade = "B+";
    }
    else if (userNumberInt >= 83 & userNumberInt <= 86)
    {
        grade = "B";
    }
    else
    {
        grade = "B-";
    }
}

else if (userNumberInt >= 70 & userNumberInt <= 79)
{
    if (userNumberInt >= 77 & userNumberInt <= 79)
    {
        grade = "C+";
    }
    else if (userNumberInt >= 73 & userNumberInt <= 76)
    {
        grade = "C";
    }
    else
    {
        grade = "C-";
    }
}

else if (userNumberInt >= 60 & userNumberInt <= 69)
{
    if (userNumberInt >= 67 & userNumberInt <= 69)
    {
        grade = "D+";
    }
    else if (userNumberInt >= 63 & userNumberInt <= 66)
    {
        grade = "D";
    }
    else
    {
        grade = "D-";
    }
}

else
{
    grade = "F";
}

Console.WriteLine("Your grade is a " + grade);

Pick a number between 0 and 100
88
Your grade is a B+

In this example, I added more dimensions to the grade variable by adding different scenarios for this script. In other words, in this example the grade won’t simply be set to pass or fail but rather to a letter value based on the inputted number. For instance, inputting a 71 will yield a C- but a 78 will yield a C+. For this example, I chose 88 and got a grade of B+.

  • For all my non-American readers, there is no such thing as an F+ or F-, hence why I don’t have any nested conditionals for the F scenario. Anything under 60 would be a F in the American grading system.

Michael’s Programming Bytes 2025 Feedback

Hello readers,

Michael here, and I know the link to my survey that I put out in the previous post didn’t work (my apologies for that), but I would certainly appreciate it if you could take some time (not even 10 minutes) to complete my quick, easy-peasy 2025 feedback survey-https://forms.office.com/Pages/ResponsePage.aspx?id=DQSIkWdsW0yxEjajBLZtrQAAAAAAAAAAAAZ__sEfXLlUMUxENjZIN0IxWDlNODJWN0RKOVdFTlRGRy4u.

Rest assured, I’ll only use this data to help improve my slate of content going forward. That’s it. It’s my way of getting your feedback on my content slate.

Also, here is my script on GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IfElse.cs. Both examples are on the same script, but I did note which code belongs to which example in the comments so if you wish to test out either example, feel free to comment out the code for the example your not testing.

Thanks for reading,

Michael

Leave a ReplyCancel reply