#StoneProfitsSystem


Decision Making Statements and Loops

If Statement : 

  • 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.

The syntax of an if statement in C# is:
if(boolean_expression)
 { /* statement(s) will execute if the boolean expression is true */ }

If..else Statement:

  • If the boolean expression evaluates to true, then the if block of code is executed, otherwise else block of code is executed.
The syntax of an if...else statement in C# is:

if(boolean_expression)
{
   /* statement(s) will execute if the boolean expression is true */
}
else
{
   /* statement(s) will execute if the boolean expression is false */
}

Example:


Nested if Statements:
  • An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
The syntax of an if...else if...else statement in C# is:
if( boolean_expression 1)
{
   /* Executes when the boolean expression 1 is true */
   if(boolean_expression 2)
   {
      /* Executes when the boolean expression 2 is true */
   }
}

Example:

Switch Statement:

  • 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.
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);
}

Example:


C# - Loops:

  • A loop statement allows us to execute a statement or a group of statements multiple times and following is the general from of a loop statement in most of the programming languages.

While Loop:

  • A while loop statement in C# repeatedly executes a target statement as long as a given condition is true.
The syntax of a while loop in C# is:

while(condition)

{
   statement(s);

}

Example:


For Loop:
  • for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
The syntax of a for loop in C# is:
for ( init; condition; increment )
{
   statement(s);
}

Example:


Do...While Loop:

  • do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
The syntax of a do...while loop in C# is:
do
{
   statement(s);


}while( condition );

Example:

Nested Loops:

  • C# allows to use one loop inside another loop. Following section shows few examples to illustrate the concept.
The syntax for a nested for loop statement in C# is as follows:
for ( init; condition; increment )
{
   for ( init; condition; increment )
   {
      statement(s);
   }
   statement(s);
}

Example:


No comments:

Post a Comment