Control Structures in C++
The work of control
structures is to give flow and logic to a program. There are three types of
basic control structures in C++.
1.
Sequence Structure
Sequence structure refers to the sequence in which program execute instructions one after another.
2.
Selection Structure
Selection structure
refers to the execution of instruction according to the selected condition,
which can be either true or false. There are two ways to implement selection
structures, by “if-else statements” or by “switch case statements”.
Figure
2: Selection Structure
If else statement
The if/else statement executes a block of code if a specified condition is true. If the condition is false, another block of code can be executed.
#include <iostream>
using namespace std;
int main () {
int day;
cout<<"enter a number for print
day ";
cin>>day;
switch(day) {
case 1 :
cout << "Monday \n";
break;
case 2 :
cout << "Tuesday \n";
break;
case 3 :
cout << "Wednesday
\n";
break;
case 7 :
cout << "sunday \n";
break;
default :
cout << "Invalid number
\n";
}
return 0;
}
3.
Loop Structure
Loop structure refers to
the execution of an instruction in a loop until the condition gets false.
Figure 3: Loop Structure
Continue Statement in C/C++
Continue is also a loop control statement just like break statement. continue statement is opposite to that of break statement,
instead of terminating the loop, it forces to execute the next iteration of the
loop.
As the name suggest the continue statement forces the loop to continue or
execute the next iteration. When the continue statement is executed in the
loop, the code inside the loop following the continue statement will be skipped
and next iteration of the loop will begin.
Example:
Consider the situation when you need to write a program which prints number
from 1 to 10 and but not 6. It is specified that you have to do this using loop
and only one loop is allowed to use.
Here comes the usage of continue statement. What we can do here is we can run a
loop from 1 to 10 and every time we have to compare the value of iterator with
6. If it is equal to 6 we will use the continue statement to continue
to next iteration without printing anything otherwise we will print the value.
#include <iostream>
Using namespace std;
int main() {
// loop from 1 to 10
for (int i = 1; i <= 10; i++)
{
if (i ==
6)
continue;
else
cout<<i<<”\t”;
}
return 0;
}
Output:
1 2 3 4 5 7 8
9 10
Pointer
A pointer however,
is a variable that stores the memory address as its value.
A pointer
variable points to a data type (like int or string) of the same
type, and is created with the * operator. The address of the variable
you're working with is assigned to the pointer:
Example
Main()
{
string
food = "Pizza"; // A food variable of type string
string* ptr = &food; // A pointer variable,
with the name ptr, that stores the address of food
// Output the value of food (Pizza)
cout << food << "\n";
// Output the memory address of food (0x6dfed4)
cout << &food << "\n";
// Output the memory address of food with the pointer (0x6dfed4)
cout << ptr << "\n";
return 0;
}
0 Comments