Flow Control with Control Statements
- C# supports several types control statements.
- These are almost all similar to C.
- Branching Control Statements
- if
- switch-case
- break
- continue
- goto
- while
- do-while
- for
Note: All the syntaxes are same as C/C++. Just for idea, we recollect the syntaxes now.
Implementation Syntax of Control Statements
Branching Control Statements
- if
- Simple if
if (condition)
{ ----; }
- if-else
if (condition)
{ ----; }
else
{ ----; }
- else-if
if (condition)
{ ----; }
else if (condition)
{ ----; }
else if (condition)
{ ----; }
else
{ ----; }
- Nested-if
if (condition)
{
if (condition)
{ ----;----; }
else
{ ----;----; }
}
else
{
if (condition)
{ ----;----; }
else
{ ----; ----; }
}
- switch-case
switch (variable)
{
case value1: ---------; break;
case value2: ---------; break;
case value3: ---------; break;
case value4: ---------; break;
default: ---------; break;
}
- break
for loop / while loop / do-while loop
{ ----------;----------;
break;
----------;
}
- continue
for loop / while loop / do-while loop
{ ----------;----------;
continue;
----------;
}
- goto
----------;
goto labelname;
----------;
labelname:
----------;
Looping Control Statements
- while
initialization;
while (condition)
{ ----------;
----------;
Increment / decrement;
}
- do-while
initialization;
do
{
----------;
Increment / decrement;
} while(condition)
- for
for (initialization; condition; increment/decrement)
{ ----------;
----------;
----------;
}
Note: You can also implement nested loops as you implemented in C/C++.
No comments:
Post a Comment