C# Examples

C# Example Code


C# Decision Making

In C#, various types of decision making statements are available such as if..else, if..else..if, switch etc. Each statement is used to evaluate the specific test. If tests are determined to be true, specific statement will be execued for example : if(a > b) statement1 else statement2. Sometimes when develpoing program that requires to take the decision to execute specific part of program, decision making statement helps to do so.

If Statement

Sometimes we reqiure to execute certain statement when specified condition true.

Syntax

If Statement Example

If..Else Statement

When specified condition fail the else statement will get execute.

Syntax

If..Else Statement Example

If..Else..If Statement

When there are situations when we need to test several conditions then we will use else if condition.

Syntax

If..Else..If Statement Example

Switch Case Statement

In C# switch acts like a multiple if / else if / else chain. Checks a value against a list of cases, and executes the first case that is true. If no matching case found, it executes the default case. The break(optional) statements with case indicate to the interpreter to end the particular case.

Syntax

Switch Case Statement Example 1

If forget a break which is optional than code will run from the case where condition is met, and will run the case after that regardless if condition was met.

Switch Case Statement With Break Example 2