Control Statements
Control Statements
(if, if- else, switch statement, loops)
The if statement is implemented in two forms:
- Simple if statement
- if ... else statement
The Syntax of the two if statements are as follows:
if(expression){
action 1;
.
.
.
action n;
}
if(expression){
action 1;
.
.
.
action n;
}
else{
action 1;
.
.
.
action n;
}
In the first case Simple if statement, if the expression under the if statement returns true or 1 then the the statements under if will execute else nothing will execute.
In the Second case if-else statement, if the expression under the if statement returns true or 1 then the the statements under if will execute else the statements under the else will execute.
Example:
#include<iostream>
using namespace std;
int main(){
x = 5;
y = 20;
z = 9;
if(x == 5){
cout << "x is equal to 5"; // Simple if Statement.
}
if(y > 10){
cout << "y is Greater than 10"; // if-else statement.
}
else{
cout << "y is smaller than 10";
}
if(z < 10){
cout << "z is Greater than 10"; // if-else statement.
}
else{
cout << "z is smaller than 10";
}
return 0;
}
output:
x is equal to 5
y is Greater than 10
z is smaller than 10
Switch Statement:
the switch statement is used to avoid the use of the if statement where a variable is compared many times. the syntax is as follows:
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
Important points to be remembered for Switch statement:-
- The expression provided in the switch should result in a constant value.
- Duplicate case values are not allowed.
- The default statement is optional.
- The break statement is used inside the switch to terminate a statement sequence. if it is not used then all the test cases will be executed, and we don't want that.
- Nesting of switch statements are allowed, which means you can have switch statements inside another switch.
Example:
#include <iostream>
using namespace std;
int main()
{
int x = 2;
switch (x)
{
case 1:
cout << "Choice is 1";
break;
case 2:
cout << "Choice is 2";
break;
case 3:
cout << "Choice is 3";
break;
default:
cout << "Choice other than 1, 2 and 3";
break;
}
return 0;
}
output:
Choice is 2
Loops:
There are three types of loops are available in C++
- do-while
- while
- for
do-while :-
it is an exit-controlled loop. based on the condition the control is transferred back to a particular point in the program, the syntax is as follows:
do{
action 1;
.
.
.
action n;
}
while(condition is true);
Example:
#include<iostream>
using namesapce std;
int main(){
int x = 1;
do{
cout << x << " ";
x++;
}
while(x <= 10); //output: 1 2 3 4 5 6 7 8 9 10
}
while :-
this is also an loop, but it is an entry-controlled loop, the syntax is as follows:
while(condition is true){
action 1;
.
.
.
action n;
}
Example:
#include<iostream>
using namesapce std;
int main(){
int x = 1;
while(x <= 10){
cout << x; // output: 1 2 3 4 5 6 7 8 9 10
x++;
}
}
for:
The for is an entry controlled loop and is used when an action is to be performed for a predetermined number of times, the syntax is as follows:
for(initial value; test; increment/decrement){
action 1;
.
.
.
action n;
}
Example:
#include<iostream>
using namesapce std;
int main(){
int x;
for(x = 1; x <= 1; x++){ //output: 1 2 3 4 5 6 7 8 9 10
cout << x;
x++;
}
}
Comments
Post a Comment
if you have any doubts, let me know