Showing posts with label Control Statements. Show all posts
Showing posts with label Control Statements. Show all posts

C# - Control Statements


Control flow and program logic are of the most important parts of a programming language’s dynamic behavior. In this section, I’ll cover control flow in C#. Most of the condition and looping statements in C# comes from c and C++. Those who are familiar with java will recognize most of them, as well.

The if . . .else Statement 
The if . . .else statement is inherited from C and C++. The if . . .else statement is also known as a conditional statement. For example:
if (condition)
statement
else
statement

The if. . .section of the statement or statement block is executed when the condition is true; if it’s false, control goes to the else statement or statement block. You can have a nested if . . .else statement with one of more else blocks. 

Bifurcation of Control

The break instruction.
Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. For example, we are going to stop the count down before it naturally finishes (an engine failure maybe):
// break loop example Output
#include 10, 9, 8, 7, 6, 5, 4, 3, countdown aborted!
int main ()
{
int n;
for (n=10; n>0; n--)
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
return 0;
} LEARN MORE>>

Iterative Statement: do-while Loop

It is another repetitive control structure provided by C++. It is an exit control loop. It evaluates its test-expression after executing its loop body statements. A do-while loop always executes at least once.
The syntax of the do-while loop is
do
{
statement;
}
while ;

LEARN MORE>>

Iterative Statement : While Loop

The while loop.
As mentioned earlier, the while loop is an entry controlled loop. The syntax of a while loop is
while (condition) statement
and its function is simply to repeat statement while expression is true. While is a reserved word of C++; condition is a Boolean expression; and statement can be simple or compound statement.
For example, we are going to make a program to count down using a while loop: LEARN MORE>>

Program Control Statements

The program flow in high - level languages is sequential. A program is usually not limited to a linear sequence of instructions. Therefore there is a use of control structures that serve to specify what to do with our program and how.
C++ provides statemets to perform efficiently and effectively. Such statements are called program control statements. These are selection statements ( if and switch), iteration statements (for, while and do-while) and jump statements such as (return, goto, exit(), break and continue)
Statements
Statements are instructions given to the computer to perform an action such as moving data, taking decisions or repeating actions. LEARN MORE>>