Warm-up Exercises

  1. Explain Boolean type bool and the meaning of logical operations AND (&&), OR (||) and negation (!). Provide a small example.

    Solution

    The Boolean data type holds either of the two values true or false. The AND (&&) and OR (||) operators are used to evaluate multiple conditions. The AND (&&) returns true if all conditions are true, and the OR (||) operator returns true if at least one of them is true. The negation operator (!) changes a Boolean value into its opposite.

    Example: ``` bool b1 = true, b2 = false;

    Console.Write(b1 && !b2); // Displays true Console.Write(b1 || b2); // Displays true ```

  2. Write a statement that declares a variable of type int and sits its value to 3.​

    Solution

     int num = 3;

Questions

  1. Write a statement that initializes a variable named myHeightInMeters to your height in meters. What should be the datatype of myHeightInMeters, and why?

    Solution

    decimal myHeightInMeters = 1.74m; The datatype should be decimal because a person’s height in meters most likely needs the precision afforded by the decimal type.

  2. What is wrong with the following? Will the error(s) appear at compilation time, or at execution time?

    int age;
    Console.WriteLine("Please enter your age:");
    age = Console.ReadLine();

    Solution

    Console.ReadLine() returns a value of type string, which cannot be stored in an integer variable. This results in a compile time error.

  3. What is the difference, if any, between 3 and “3”?

    Solution

    3 is an integer value, and “3” is a string value.

Problems

  1. Declare and initialize 3 variables:

    1. Each variable should have a different data type
    2. Choose an appropriate name and value for each variable

    Then display the value of each variable on the screen.

    Solution

     int number = 5;
     string name = "Samuel";
     float weight = 120.65f;
     
     Console.WriteLine($"number: {number}");
     Console.WriteLine($"name: {name}");
     Console.WriteLine($"weight: {weight} kg");