C++ Programming

C++ Programming

Statement Flow Control


Introduction

Generally a program executes its statements from beginning to end. But not many programs execute all their statements in strict order from beginning to end. Most programs decide what to do in response to changing circumstances. These programs not only store data but they also manipulate data in terms of consolidation, rearranging, modifying data. To perform their manipulative miracles, programs need tools for performing repetitive actions and for making decisions. C++ provides such tools by providing statements to attain so. Such statements are called program control statements.

Every programming language provides constructs to support sequence, selection and iteration.

Sequence

The sequence construct means the statements are being executed sequentially. Every C++ programs begins with the first statement of main( ) function. When the final statement of the program is executed, the program is done. This construct specifies the normal flow of control in a program. Thus this construct does not have its respective statement.

Selection

The selection construct means the execution of statements depending upon a condition test. If a condition evaluates to true, a course of action is followed. If a condition evaluates to false, another course of action is followed. This construct is also known as the decision construct because it helps in making a decision about which set of statements is to be executed.

Iteration

The iteration construct means the repetition of a set of statements depending upon a condition test. Till the time is true, a set of statements are repeated again and again. Finally when the condition evaluates to false, the repetition stops. This construct is also known as the looping construct.  

The set of statements that are repeated again and again is known as the body of the loop. The condition on which the execution of the loop depends is known as the test condition.

Every programming language must provide all these constructs as the sequential program execution is inadequate to the problems we must solve. C++ also provides statements that support these constructs. Since the selection construct is basically the default flow of control in a program, the selection statement does not exist. In the coming sections, we’ll discuss these C++ statements. 

Selection Statements

The selection statements allow us to choose the set of instructions for execution depending upon an expression’s truth value. C++ provides two types of selection statements: if and switch

if Statement

An if statement tests a particular condition; if the condition evaluates to true, a course of action is followed that is a set of statements ie executed. If the condition evaluates to false, the course of action is ignored. The syntax is as follows:

if( condition )
{

// body of the loop

}

The body of the loop may consist of a single statement or even multiple statements. If there’s a single statement then the curly brackets may be optional since that only statement will be considered as the body of the loop. Syntax:

if( condition )                                             
// The one statement

We’ll create a program based on if statement.

The Weekly Program

Now let’s create a program to find the largest of three numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z,max;
cout<<"Enter a number: ";
cin>>x;
cout<<"Enter another number: ";
cin>>y;
cout<<"Enter another number: ";
cin>>z;
if(x>y && x>z)
max=x;
if(y>z && y>x)
max=y;
if(z>x && z>y)
max=z;
cout<<"The largest no out of the three is: "<<max;
getch();
}

if(x>y && x>z)

The following if statement assigns the value x to the variable max only if x is greater than y and z.

if(y>z && y>x)

The following if statement assigns the value y to the variable max only if y is greater than x and z.

if(z>x && z>y)

The following if statement assigns the value z to the variable max only if z is greater than x and y. Thus with the following three if statements, we’ll find the largest of the three numbers.

Execution of the Program

Now Compile the program. If the compiler shows errors then check my program and resolve it. Then Run the program. Here’s the output:

Now it’s time to do it yourself and feel free to mess around with the program to understand in-depth what each statement does. Don’t forget to save your program.

That’s it for today, see you next week!!!