C# Lesson 5

Run sections of your code only IF you want them to with the "if" statement in the 5th lesson from the series on learning C# for beginners.

The if statement is one of the most crucial commands. It allows you to control the state of your application so that sections of code only run when you want them to.
Afterwards you can continue to the next lesson!
Or go back and review the previous lesson.
We also have the source code on GitHub.


using System;

namespace Lesson5
{
    class IfStatement
    {
        static void Main(string[] args)
        {

            if (true)
            {
                Console.WriteLine("This will always be true.");
            }

            if (false)
            {
                Console.WriteLine("This will never execute");
            }

            //Console.Write("Enter x: ");
            //int x = Convert.ToInt32(Console.ReadLine());

            //Console.Write("Enter y: ");
            //int y = Convert.ToInt32(Console.ReadLine());

            //if (x > y)
            //{
            //    Console.WriteLine("x is bigger than y.");
            //}
            //else if (y > x)
            //{
            //    Console.WriteLine("y is bigger than x");
            //}
            //else
            //{
            //    Console.WriteLine("x is equal to y");
            //}


            //if (x > y)
            //{
            //    Console.WriteLine("x is bigger than y.");
            //}

            //if (y > x)
            //{
            //    Console.WriteLine("y is bigger than x");
            //}

            //if (x == y)
            //{
            //    Console.WriteLine("x is equal to y");
            //}


            Console.WriteLine("What planet are we on?");
            string planetAnswer = Console.ReadLine();

            if (planetAnswer == "Earth")
            {
                Console.WriteLine("Earth is correct!");
            }
            else
            {
                Console.WriteLine(planetAnswer + " is wrong");
            }


            Console.WriteLine("How many sides are on a cube?");
            int sidesAnswer = Convert.ToInt32(Console.ReadLine());

            if (sidesAnswer != 6)
            {
                Console.WriteLine(sidesAnswer + " is wrong");
            }
            else
            {
                Console.WriteLine("6 is correct!");
            }


            Console.WriteLine("Input a number greater than 9.0");
            double nineAnswer = Convert.ToDouble(Console.ReadLine());
            bool isCorrect = nineAnswer > 9.0;

            if (isCorrect)
            {
                Console.WriteLine(nineAnswer + " is correct!");
            }
            else
            {
                Console.WriteLine(nineAnswer + " is wrong");
            }

        }
    }
}


/*
* TASK:
*   Use console commands to ask the user a question, take their input,
*   then give feedback if the user is right or wrong.
*   
*   TIME TO THINK:
*     COUNT every CORRECT answer the user gives and tell them the number of correct answers at the end of the program.
*   
*   Make 6 questions. 
*   Make a question that uses one of the following comparison opperators to check for the correct answer.
*   Each comparison opperator must be used at least once.
*   
*   ==
*   !=
*   >
*   <
*   >=
*   <=
*   
*   If it is hard to think of a question. You can ask things like "What number is greater than 8" >
*   OR "What number is less than or equal to 8.3" <=
*   OR "Do not type Ball" !=
* 
* Example Output:
*   bla bla?
*   yes
*   bla bla?
*   no
*   bla bla?
*   3
*   bla bla?
*   ball
*   bla bla?
*   9.8
*   bla bla?
*   2
*   
*   You got 4 questions right!
*   
* BONUS POINTS:
*   Also display the number of questions they got wrong
*   EX:
*     You got 4 questions right and 2 wrong!
*  
* 
*/

using System;

namespace Lesson5Task
{
    class IfStatementTask
    {
        static void Main(string[] args)
        {
        }
    }
}

Recommended posts

We have similar articles. Keep reading!

C# Lesson 8

Characters, the data type that's just a little less useful than a string.

C# Lesson 9

Lesson 9 in our series about learning C# teaches you how to make Strings do your bidding.

C# Lesson 12

Lesson 12 in a our series about learning C# teaches you about working with methods.

C# Project 2

Test out your knowledge on basic C# programming with a challenging project making a hangman game. Covers lessons 7-12 from the beginner series.

Comments

Log in or sign up to leave a comment.