Conditional statements in C# programming



Accepting and displaying user inputs

You use Console.WriteLine() method to display and Console.ReadLine method to accept or to read user inputs.

The following code demonstrates this:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace UserInputs
    {
        class Program
        {
        static void Main(string[] args)
            {
            Console.WriteLine("Please type something and press Enter:");
            string userValue;
            userValue = Console.ReadLine();
            Console.WriteLine(myFirstName + myLastName);
            Console.WriteLine("You typed: " + uservalue);
            }
        }
    }

    Output:
    You Typed: Infocodify - The Newest Web Developers and Programmers Site

Making decisions using the if selection statement

You use if statements to test the truth of a statement. If the statement is true, the block of code associated with the if statement is executed, if the statement is false, control passes over the block.

The following code shows how to use an if statement:

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ifCondition
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter a Value: ");
                // You need to typecast the input
                int userValue = Convert.ToInt32(Console.ReadLine());
                string message = "";   
                if (userValue > 70)
                {
                    message = "You pass the EXAM";
                }
                Console.WriteLine(message);
                Console.ReadLine();
            }
        }
    }

    Output:
    Enter a Value : 80
    You pass the Exam

Using the if else statement

The if statement can have associated else clauses. The else block executes when the if statement is false.

The following code example shows how to use an if else statement

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ifElseCondition
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter a Value: ");
                // You need to typecast the input
                int userValue = Convert.ToInt32(Console.ReadLine());
                string message = "";   
                if (userValue > 70)
                {
                    message = "You pass the EXAM";
                }
                else
                {
                    message = "You don't pass the EXAM";
                }
                Console.WriteLine(message);
                Console.ReadLine();
            }
        }
    }

    Output:
    Enter a Value : 69
    You do not pass the Exam

Using the else if statement

The if statement can also have associated else if clauses. The clauses are tested in the order that they appear in the code after the if statement.

If any of the clauses returns true, the block of code associated with that statement is executed and control leaves the block of code associated with the entire if construct.

The following code example shows how to use an if statement with an else if clause:

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace elseIfCondition
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter a number 1, 2, or 3?");
                string userValue = Console.ReadLine();
                string message = "";
                if (userValue == "1")
                {
                    message = "You won a new car!";
                }
                else if (userValue == "2")
                    message = "You won a new boat!";
                else if (userValue == "3")
                    message = "You won a new cat!";
                else
                    message = "Sorry, we didn't understand.  You lose!";
                Console.WriteLine(message);
                Console.ReadLine();
            }
        }
    }

    Output:
    Enter a Number : 3
    You won a new cat!

Using the null-coalescing operator

The (??) null-coalescing operator returns the left-hand operand if the operand is not null, otherwise it returns the right hand operand.

The following code demonstrates the null-coalescing operator:

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace nullCoalescing
    {
        class Program
        {
            static void Main(string[] args)
            {
            int? a = 69;
            // Set y to the value of a if a is 69; otherwise,
            // if a = 69, set y to 70 .
            int y = a ?? 70;
            Console.WriteLine("Please type a number: ");
            Console.ReadLine();
            Console.WriteLine("If the value is greater than : " + a + " You pass the Exam");
            Console.ReadLine();
            }
        }
    }

    Output:
    Please type a number: 69
    If the value is greater than : 69 You pass the Exam!

Using the ternary operator

The conditional operator ?: evaluates a condition and returns one of two values depending on the result. Because this operator has three operands, it’s also called the ternary operator.

The following code demonstrates the ternary operator:

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ternaryOperator
    {
        class Program
        {
            static void Main(string[] args)
            {
            Console.WriteLine("Please type a number :");
            int userValue;
            userValue = Convert.ToInt32(Console.ReadLine());
            string message = (userValue > 70) ? "pass the EXAM" : "don't pass the Exam";
            Console.WriteLine("With" + userValue +"{0}", message);
            Console.ReadLine();
            }
        }
    }

    Output:
    Please type a number: 71
    With 71 pass the Exam!

Ads Right