If Statements

https://csci-1301.github.io/about#authors

August 13, 2021 (08:00:10 AM)

This lab is aimed at having you practice simple conditional statements. It may seem repetitive, but you need to practice if statements a lot to understand their mechanics and to be able to write them properly.

Basic Conditional Statements

Testing and Improving Conditional Statements

Consider the following code:

using System;

class Program
{
    static void Main()
    {
        int yourAge;
        Console.WriteLine("Please, enter your age");
        yourAge = int.Parse(Console.ReadLine());
        if (yourAge < 0)
        {
            Console.WriteLine(
                "I believe you made a mistake, an age cannot be negative!");
        }
        else if (yourAge > 2000)
        {
            Console.WriteLine(
                "I believe you made a mistake, nobody can live that long!");
        }
        else
        {
            if (yourAge >= 18)
            {
                Console.WriteLine(
                    "In all States but Alabama, Nebraska, Mississippi and Puerto Rico, you have reached the age of majority.");
            }
            else if (yourAge >= 19)
            {
                Console.WriteLine(
                    "In all States but Mississippi and Puerto Rico, you have reached the age of majority.");
            }
            else if (yourAge >= 21)
            {
                Console.WriteLine(
                    "You have reached the age of majority in all US states.");
            }
        }
    }
}

The information about the age of majority comes from wikipedia.

  1. Without executing it, write down what you expect to be displayed if the user enters
    1. “That’s confidential”,
    2. 10
    3. 18
    4. 19
    5. 22
    6. 29339
  2. Copy-and-paste the code in the body of the main method and execute it, providing the values written below. Was your thinking correct? If not, revise it and make sure you understand the logic of the program.
  3. There is at least one issue with this code, as “You have reached the age of majority in all US states.” will never be displayed. Can you understand why?
  4. Fix the program so that all the messages can be displayed when relevant. Feel free to reorder statements or to use conjunction, disjunction, etc. to alter your conditions.

Writing Simple Conditional Statements

Read all the instructions in this part before starting to type code. Create a new project, and write portions of code that perform the following:

  1. Ask the user for an integer, and display on the screen “You were born after me” if the number is strictly greater than your year of birth.
  2. Ask the user for an integer, and display on the screen “Between  − 1 and 1” if the number is greater than or equal to  − 1 and less than or equal to 1.
  3. Ask the user for an integer, and display on the screen “Not between  − 1 and 1” if the number is greater than 1 or less than  − 1.
  4. Ask the user for an integer, and display on the screen “Odd” or “Even”, depending if the number is odd or even.
  5. Ask the user for an integer, and display on the screen “Negative” if the integer is negative, “Positive” if the integer in positive, and nothing if the integer is 0.
  6. Ask the user for an integer, and display on the screen “positive and odd” if the number is positive and odd, “positive and even” if the number is positive and even, “negative and odd” if the number is negative and odd, “negative and even” if the number is negative and even, and “You picked 0” if the number is 0.

For each of those questions, write on paper whenever you should use if, if-else, if-else-if or nested conditional structures, and what the condition(s) should be. Once you feel confident, write the code in your IDE, and then test it intensively: enter all kinds of values (positive and odd, negative and even, 0, and remember that 0 is even, etc.) and make sure that what is displayed on the screen is always correct.

Observation: How to Construct a Value Progressively

Please, read this part only once you have solved the last question of the previous exercise. You were asked the following:

Ask the user for an integer, and display on the screen “positive and odd” if the number is positive and odd, “positive and even” if the number is positive and even, “negative and odd” if the number is negative and odd, “negative and even” if the number is negative and even, and “You picked 0” if the number is 0.

A possible answer is:

int a;
Console.WriteLine("Enter an integer");
a = int.Parse(Console.ReadLine());
if (a >= 0)
{
    if (a % 2 == 0)
        Console.WriteLine("Positive and even");
    else // if (a % 2 != 0)
        Console.WriteLine("Positive and odd");
}
else
{
    if (a % 2 == 0)
        Console.WriteLine("Negative and even");
    else
        Console.WriteLine("Negative and odd");
}

That is a lot of repetition! And, as you know, programmers hate having to copy-and-paste the very same code, as it requires twice the editing every time you make an update!

We could actually “progressively” construct the message we will be displaying:

string msg;
if (a >= 0)
{
    msg = "Positive";
}
else
{
    msg = "Negative";
}
if (a % 2 == 0)
    msg += " and even";
else // if (a % 2 != 0)
    msg += " and odd";

Much better! Since the two conditions are actually independent, we can test them in two different if statements!

Pushing Further (Optional)

Conditional Operator

You were introduced to the conditional operator, which can be used to replace if-else statements in particular cases (assignment, call, increment, decrement, and new object expressions). Its structure is:

condition ? first_expression : second_expression;

You can read more about it in the documentation.

If you have time, practice using the conditional operator by adding these statements to your program:

  1. Write a statement that sets myVar to 0 if initial is an upper-case letter, and to 1 otherwise. You already wrote an if statement that accomplishes this in the previous exercise, so you just need to rewrite it using the conditional operator.
  2. Write a statement that sets initial to 'B' if myVar is greater than 500 and to 'S' if myVar is less than or equal to 500.
  3. Write a statement that doubles the value of myVar if day is "Sat." or "Sun." and adds 1 to the value of myVar otherwise.

Computing Entry Price

You are asked to write a simple program that computes the total price for a group of people to enter a park.

Your program should:

  • Ask the user how many adults and how many children want to enter the park,
  • If the group comprises 6 persons or more, offer to sell a group pass for $30 (that allows everyone in the group to enter the park),
  • Compute and display the total price on the screen, knowing that:
    • Adults pay $7,
    • Children pay $4,
    • If purchasing the group pass allowed the group to save money (which isn’t always the case!), you should display on the screen the amount saved.

Some tips:

  • When asking “yes” / “no” questions, treat “y” and “Y” as a “Yes”, and any other string as a “No”.
  • Note that we will sell the pass even if the user is not gaining money by doing so (for instance, if 6 children want to enter, $4 × 6 = $24 < $30, but we would still sell them the pass).