Flow of control
A single statement is called a simple statement some examples of simple statements are given below
s = S + a;
count ++;
The compound statement
cin>>a>>b;
c=a+b;
cout<<c;
}
Selective execution(conditional statements)
In some cases, it is desired that a selected segment of a program be executed on the basis of a test depending upon the state of a particular condition being true or false in C++' if statement' is used for selective execution of the program
The if statement
This statement helps us in the selection of 1 out of two alternative courses of actions the general form of "if statement" is given below
if (expression)
{
statement sequence
}
Where if is a reserved word expression is a Boolean expression enclosed within a set of Parentheses. These Parentheses are necessary even if there is a single variable in the expression statement sequence is either a simple statement or a block. However, it cannot be declaration
Example of acceptable if statements are:
1-if (A>B) A=B;
2-if (total < 100)
{
total = total + val;
count = count+1;
}
The if-else statement
It may be observed from the above examples that the simple if statement does nothing when the expression is false an if-else statement takes care of this aspect
The general form of this construct is given below
{
statement sequential
}
else
{
statement sequential
}
where if is a reserved word expression is a Boolean expression returned within parentheses statement sequential can be a simple or a compound statement else is a reserved word statement sequential can be an as simple or compound statement
If (x == 100)
cout<<"Equal to 100";
else
cout<<"\n Not equal to 100";
0 Comments