Understanding control statements
Control statements allow us to control the flow of execution of a program. They also allow us to execute a particular block of code based on a certain condition. C# defines three categories of control statements, as mentioned here:
- Selection statements:
ifandswitch - Iteration statements:
for,while,do-while, andforeach - Jump statements:
break,continue,goto,return, andyield
We will explore each of these statements in detail in the following sections.
Selection statements
Selection statements allow us to change the execution flow based on whether a condition is true or not. C# provides us with two types of selection statements: if and switch.
The if statement
The following snippet shows the syntax of an if statement:
if (condition1) statement1; else if(condition2) statement2; else statement3;
If condition1 evaluates to true, then statement1...