- Control statements in C++ define the way you develop the logic.
- Controls are available in all programming and scripting language.
- Controls mainly decides the logic flow of data.
- As per google, control statements in computer science are programming commands for decision-making.
- C++ supports following type of control statements.
1) Selection statement
There are three types of selection. One way, Two way and Multi way selection.
if( ) statement
if … else statement
switch( ) statement
2) Iteration statement
There are two types of iteration. Pre-tested iteration and post-tested iteration.
while( ) statement
do..while( ) statement
for( ) statement
3) Jump statement
These are unconditional branching statement.
goto statement
continue statement
break statement
return statement
if Control Statements in C++
Syntax:
if (<expL>)
{
statements
}
- <expL> is the logical expression. It must be evaluated as either true or false.
- This statement provides the one-way selection facility.
- The logical expression specified with if( ) is evaluated as true or false.
- On execution, if the expression is evaluated as true then only the statement within { } are executed otherwise control is transferred to the statement followed by if{ }
- Example:
if(a>b)
{
larger=a;
smaller=b;
} - In above example, when compiler encounters if statement, it evaluates the specified logical expression a>b. If it is evaluated as true then it executes the statements within { } otherwise transfer the control to next statement followed by { }
- The { } can contain multiple statements.
- If the { } contains single statement then { } is optional.
Program to compare two numbers using if() statement
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,L;
clrscr();
cout<<"Give two number:";
cin>>a>>b;
if(a>b)
{
cout<<a<<"is larger than"<<b;
}
if(b>a)
{
cout<<b<<"is larger than"<<a;
}
if(a==b)
{
cout<<"both numbers are equal";
}
}
Output:
Give two number: 123 234
234 is larger than 123
Give two number: 456 456
both numbers are equal
if…else statement
Syntax:
if (<expL>)
{
statements1
}
else
{
statements2
}
- This statement provides the two -way selection facility.
- The logical expression must be evaluated as either true or false
- On execution, the expression is evaluated and if it is true then the statement 1 is executed otherwise it executes statement2.
- Example:
if(a>b)
{
larger=a; smaller=b;
}
else
{
larger=b; smaller=a;
} - In above example, if the expression a>b is true then only the statement in first { } are executed otherwise compiler executes statement with else { }
- The { } can contain multiple statements. If the { } contains single statement then { } is optional.
- The if…else statement has following two variations.
1) nested if else (if…else within if…else)
if (<expL1>)
{
if(<expL2>)
The if…else statement has following two variations.
1) nested if else (if…else within if…else)
if (<expL1>)
{
if(<expL2>)
{
—
—
—
}
else
{
—
—
—
}
}
else
{
if(<expL3>)
{
—
—
}
else
{
—
—
}
}
![Mastering Control Statements in C++: Key Techniques for Success in 2023 5 Untitled Diagram.drawio1](https://codejunctionpro.com/wp-content/uploads/2023/10/Untitled-Diagram.drawio1.png)
2. if…else ladder
That is, every else contains if ( ), expect the last else.
if(<expL1>)
{
—
—
}
else if(<expL2>)
{
—
—
}
else if(<expL3>)
{
—
—
}
else
{
—
—
}
In ladder, true part contains action and every false part contain the condition except the last.
switch() statement
Syntax:
switch(<expression>)
{
case value1:
statements1;
case value2:
statements2;
:
:
case valueN:
statementsN;
[default:
statements;]
}
- This statement provides multiple selection facility.
- The expression must be evaluated as either integer or constant.
- On execution, the expression is evaluated and depending on the evaluated value, compiler transfer the control to the respective case.
- If the evaluated value does not match with any case the control is transferred to default part.
- The default part is optional.
- Case values need not be in any sequence.
- To execute only one case at a time, use break statement at the end of each case,except the last.
- Generally default is at the end of all cases but this is not the rule.
- Example:
switch(day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
cout<<“Working Day”;
break;
case 6:
case 7:
cout<<“Week End”;
break;
default:
cout<<“Invalid Day”;
} - In above statement, if value of day is other than 1…7 then control goes to default part.
goto statement
- Syntax:
goto label;
where, label is an identifier that must be in the same function. - This statement is always followed by a label.
- Label may be an identifier.
- Special characters are not allowed in the label.
- A label is always followed by colon (:)
- On execution control is transferred to the labeled statement.
- Generally goto is used with some conditions but can also be used as a separate statement.
- Label may be anywhere in the program.
- If the label is before the goto statement then it is called backward jump.
- If the label is after the goto statement then it is called forward jump.
- In case of backward jump, execution of some statements is repeated whereas in case of forward jump, some statements are skipped.
- Example:
if(a==b)
goto same:
else
goto different: - In the above example, value for a and b is compared, if equal then control jump to the label same otherwise to the label different.
continue statement
- Syntax:
continue; - This statement can be used only in the iteration statements i.e. do, while and for.
- It causes the transfer of control to the loop continuation part of the loop and re-evaluates the condition.
- This statement has no argument.
- Example:
while(i<=n)
{
if(n%i==0)
{
n=n/i;
continue;
}
i++;
}
break statement
- This statement is valid in any switch( ), do( ), while( ) and for( ) statements only.
- It terminates the execution of { } in which it is used.
- Syntax:
break; - If it is in nested { } then it terminates execution of inner { } and passes the control the statement followed by inner { }.
- This statement may be used, when we want to jump out of a loop without waiting for the execution of any other condition.
- Example 1:
for (a=1; a<=n; a++)
{
if (n%a==0)
break;
else
cout<<a;
} - Example 2:
switch(a)
{
case 1:
cout<<“Better”;
break;
case 2:
cout<<“Best”;
break;
default:
cout<<“Good”;
}