C# Control Statements
The flow of program control is specified by control statements in C#. It includes the following −
1. if statement
An if statement consists of a Boolean expression followed by one or more statements.
The following is the syntax −
if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ }
2. if-else statement
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
The following is the syntax −
if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ }
3. for loop
It executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
The following is the syntax −
for ( init; condition; increment ) { statement(s); }
4. while loop
It repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body.
The following is the syntax −
while(condition) { statement(s); }
5. do…while loop
It is similar to a while statement, except that it tests the condition at the end of the loop body.
The following is the syntax −
do { statement(s); } while( condition );
Comments
Post a Comment