C # if statement


Release date:2023-08-24 Update date:2023-12-08 Editor:admin View counts:352

Label:

C # if statement

One if a statement consists of a Boolean expression followed by one or more statements.

Grammar

In C # if syntax of the statement:

if(boolean_expression)
{
   /* The statement to be executed if the Boolean expression is true */
}

If the Boolean expression is true, the code block within the if statement will be executed. If the Boolean expression is false, the first set of code (after closed parentheses) after the end of the if statement will be executed.

Flow chart

Image0

Example

using System;
namespace DecisionMaking
{

    class Program
    {
        static void Main(string[] args)
        {
            /* Definition of Local Variables */
            int a = 10;
            /* Check Boolean conditions using if statements */
            if (a < 20)
            {
                /* If the condition is true, output the following statement */
                Console.WriteLine("A less than 20");
            }
            Console.WriteLine("The value of a is {0}", a);
            Console.ReadLine();
        }
    }
}

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

A less than 20
The value of a is 10

Powered by TorCMS (https://github.com/bukun/TorCMS).