GST Billing Software

If Statement If Else If Statement: Nested If Statement: Switch Statements

header image

DECISION MAKING STATEMENTS

Erachana Line

An if statement consists of a boolean expression followed by one or more statements.

Syntax:

The syntax of an if statement in C# is −

if (Boolean expression)

{

/* statement(s) will execute if the Boolean expression is true */

}

If the boolean expression evaluates to true, then the block of code inside the if statement is executed. If boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing curly brace) is executed.

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

Syntax:

The syntax of an if...else statement in C# is −

if(boolean_expression1)

{

/* statement(s) will execute if the boolean expression is true */

}

else if(boolean_expression2)

{

/* statement(s) will execute if the boolean expression is false */

}

else

{

/* statement(s) will execute if the boolean expression is false */

}

If the boolean expression evaluates to true, then the if block of code is executed, otherwise else block of code is executed.

It is always legal in C# to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).

Syntax:

The syntax for a nested if statement is as follows −

if (Boolean expression 1)

{

/* Executes when the boolean expression 1 is true */

if(Boolean expression 2)

{

/* Executes when the boolean expression 2 is true */

}

}

You can nest else if...else in the similar way as you have nested if statement.

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

Syntax:

The syntax for a switch statement in C# is as follows −

switch(expression)

{

case constant-expression:

statement(s);

break; /* optional */

case constant-expression:

statement(s);

break; /* optional */

/* you can have any number of case statements */

default: /* Optional */

statement(s);

}