TOPIC: C++ Control Structures
Control Structures are used to simplify code structures as well as shorten
the code lengths. They are used so that there is no complexity in the
format of the code and make it easy to understand. They are also used
in order to perform manipulative tasks, repetitive actions and make
decisions. So the basic
Control Structures are as follows:
1. SEQUENCE STRUCTURE
2. SELECTION STRUCTURE
3. LOOP STRUCTURE
Types of Loop Structure:
if(expression)
statement;
Let us look at the code about if statement
// Program to illustrate if statement
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a, b;
cout << "Enter the value of a and b: " << endl;
cin >> a >> b;
if (a < b)
{
cout << b << " is the biggest";
}
getch();
return 0;
}
Enter the value of a and b:
3
4
4 is the biggest
if(expression)
statement1;
else
statement2;
Let us look at the code about if-else statement
// Program to illustrate if-else statement
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int age;
cout << "Enter your age: " << endl;
cin >> age;
if (age < 18)
{
cout << "Your age is " << age << " and you are minor";
}
else
{
cout << "Your age is " << age << " and you are not minor";
}
getch();
return 0;
}
Output 1:
Enter your age:
15
Your age is 15 and you are minor
Output 2:
Enter your age:
45
Your age is 45 and you are not minor
if(expression1)
statement1;
else if(expression2)
statement2;
else if(expression3)
statement3;
.
.
.
else
statement;
Let us look at the code about ladder if-else statement
// Program to illustrate ladder if-else statement
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int age;
cout << "Tell me your age:" << endl;
cin >> age;
if ((age < 18) && (age > 0))
{
cout << "You cannot come to vote" << endl;
}
else if (age == 18)
{
cout << "You will get a pass to vote" << endl;
}
else if (age < 1)
{
cout << "You are not yet born!!!" << endl;
}
else
{
cout << "You can come to vote" << endl;
}
getch();
return 0;
}
Output 1:
Tell me your age:
18
You will get a pass to vote
Output 2:
Tell me your age:
0
You are not yet born!!!
Output 3:
Tell me your age:
45
You can come to vote
Output 4:
Tell me your age:
12
You cannot come to vote
switch (expression)
{
case n:
{
action1;
break;
}
.
.
.
default:
action;
break;
}
Let us look at the code about switch statement
// Program to illustrate switch statement
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int age;
cout << "Tell me your age:" << endl;
cin >> age;
switch (age)
{
case 18:
cout << "You are 18." << endl;
break;
case 22:
cout << "You are 22." << endl;
break;
case 2:
cout << "You are 2." << endl;
break;
default:
cout << "No special cases." << endl;
break;
}
getch();
return 0;
}
Output 1:
Tell me your age:
18
You are 18.
Output 2:
Tell me your age:
22
You are 22.
Output 3:
Tell me your age:
2
You are 2.
Output 4:
Tell me your age:
45
No special cases.
TO BE NOTED:
More blog posts coming soon!
Coding is the language that empowers us to speak to machines, creating a symphony of possibilities.
- Unknown