Latest web development tutorials

C # if ... else statement

C # judge C # judge

After aif statement with an optional else statement,else statement executes the Boolean expression is false.

grammar

The syntax in C #if ... else statement:

if (boolean_expression)
{
   / * If the Boolean expression is true will execute statement * /
}
else
{
  / * If the Boolean expression is false statements that will be executed * /
}

If the Boolean expression istrue, then the code ifthe execution block. If the Boolean expression isfalse, then the code within the elseblock executed.

flow chart

In C # if ... else statement

Examples

using System;

namespace DecisionMaking
{
    
    class Program
    {
        static void Main (string [] args)
        {

            / * Local variable definitions * /
            int a = 100;

            / * * Check boolean condition /
            if (a <20)
            {
                / * If the condition is true, the output following statement * /
                Console.WriteLine ( "a less than 20");
            }
            else
            {
                / * If the condition is false, the output of the following statements * /
                Console.WriteLine ( "a greater than 20");
            }
            Console.WriteLine ( "a value is {0}", a);
            Console.ReadLine ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

a greater than 20
a value of 100

if ... else if ... else statement

After aif statement with an optional else if ... elsestatement, which can be used to test a variety of conditions.

When using if ... else if ... else statement, the following points should be noted:

  • If a post can be followed by zero or one else, it must be after any one else if.
  • If the latter can be followed by zero or more else if, they must precede else.
  • Once a successful match else if, else if or else the other will not be tested.

grammar

In C #if ... else if ... else statement syntax:

if (boolean_expression 1)
{
   / * When a Boolean expression is true execution * /
}
else if (boolean_expression 2)
{
   / * When the Boolean expression 2 is true execution * /
}
else if (boolean_expression 3)
{
   / * 3 When the Boolean expression is true execution * /
}
else 
{
   / * When the above conditions are not true, are executed * /
}

Examples

using System;

namespace DecisionMaking
{
    
    class Program
    {
        static void Main (string [] args)
        {

            / * Local variable definitions * /
            int a = 100;

            / * * Check boolean condition /
            if (a == 10)
            {
                / * If the if condition is true, then the output of the following statements * /
                Console.WriteLine ( "a value of 10");
            }
            else if (a == 20)
            {
                / * If the else if condition is true, then the output of the following statements * /
                Console.WriteLine ( "a value of 20");
            }
            else if (a == 30)
            {
                / * If the else if condition is true, then the output of the following statements * /
                Console.WriteLine ( "a value of 30");
            }
            else
            {
                / * If the above conditions are not true, then output the following statement * /
                Console.WriteLine ( "no matching value");
            }
            Console.WriteLine ( "a precise value is {0}", a);
            Console.ReadLine ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Exact value does not match the value of a is 100

C # judge C # judge