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.

A statment forms the executable unit within a C++ program.

Statements are terminated by a semicolon (;). Statements can be of different forms as discussed below:

Null Statement

A null statemetn is used when the syntax of the language requires the presense of a statement but the logic of the program does not allow it. a null statement is just a semicolon.

For example:

; // Null statement

Compound statement

A compound statement is a sequence of statements enclosed by a pair of braces { }. Each statment ends with a semicolon.

For example,

{

cin>>a>>b;

area=a*b;

cout<<area;

}

The opening braces ' { ' indicate the beginning of a sequence of statements and ' } ' closing braces indicate the end of the statement.

Selective Execution ( Conditional Statements)

On the basis of a given condition a selected segment of the program is executed. It depends upon the state of a particular condition being true or false. Two types of selection statements are allowed in C++: if and switch. Condition operator ( ? : ) can also be used as an alternative to the if statement. Selection statemetns are also called condition or decision statements.

The if Statement

Its form is: if (condition) statement

where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues on the next instruction after the conditional structure.

For example,

the following code fragment prints out x is 100 only if the value stored in variable x is indeed 100:

if (x == 100)

cout << "x is 100";

If we want more than a single instruction to be executed in case that condition is true we can specify a block of instructions using curly brackets { }:

if (x == 100)

{

cout << "x is ";

cout << x;

}

The if-else statement

We can additionally specify what we want that happens if the condition is not fulfilled by using the keyword else. Its form used in conjunction with if is:

if (condition) statement1 else statement2

For example:

if (x == 100)

cout << "x is 100";

else

cout << "x is not 100";

prints out on the screen x is 100 if indeed x is worth 100, but if it is not -and only if not- it prints out x is not 100.

The if + else structures can be concatenated with the intention of verifying a range of values. The following example shows its use telling if the present value stored in x is positive, negative or none of the previous, that is to say, equal to zero.

if (x > 0)

cout << "x is positive";

else if (x < 0)

cout << "x is negative";

else

cout << "x is 0";

Remember that in case we want more than a single instruction to be executed, we must group them in a block of instructions by using curly brackets { }.

HOME LEARN C++ PREVIOUS NEXT