*** ***** ******* The second ric ** *** **** ***** ** ** *** ***
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
#include <iostream>
using namespace std;
int main()
{
char ch; //the fill character
int maxRowWidth; //the maximum width of the inverted triangle
cout << "Enter character for pattern ";
ch = cin.get();
cout << "Enter the maximum width of the the triangle \n";
cout << "This must be an odd number from 3 through 79 ";
cin >> maxRowWidth;
//input validation loop
while (maxRowWidth < 3 or maxRowWidth > 79)
{
cout << "Enter an odd number (from 3 up to 79) ";
cin >> maxRowWidth;
}
//make sure number of rows is odd by adding one to any even input value
if (maxRowWidth % 2 == 0)
maxRowWidth++;
cout << endl;
//This nested loop structure outputs an indented solid triangle with its
//longest row at the bottom of the triangle and against the left edge of the output screen
//outer loop controls output of the rows in the figure
//example: If maxRowWidth = 13 then there are 7 rows in triangle
//you can write the outer for loop differently if you wish
for (int i = 1; i <= maxRowWidth; i+=2) // i tracks the current row
{
//nested loop controls indentation by printing spaces
//nested loop controls printing one row of characters
}
//this nested loop structure outputs a right triangle with a vertical leg on its right
//the triangle is maxRowWidth rows high.
//outer loop controls the rows
for ( )
{
//loop does the spaces
//loop does the characters
}
return 0;
}
#include<iostream>
using namespace std;
int main(){
char ch; //the fill character
int maxRowWidth; //the maximum width of the inverted triangle
cout << "Enter character for pattern ";
ch = cin.get();
cout << "Enter the maximum width of the the triangle \n";
cout << "This must be an odd number from 3 through 79 ";
cin >> maxRowWidth;
//input validation loop
while (maxRowWidth < 3 or maxRowWidth > 79){
cout << "Enter an odd number (from 3 up to 79) ";
cin >> maxRowWidth;
}
//make sure number of rows is odd by adding one to any even input value
if (maxRowWidth % 2 == 0)
maxRowWidth++;
cout << endl;
//This nested loop structure outputs an indented solid triangle with its
//longest row at the bottom of the triangle and against the left edge of the output screen
//outer loop controls output of the rows in the figure
//example: If maxRowWidth = 13 then there are 7 rows in triangle
//you can write the outer for loop differently if you wish
for (int i = 1; i <= maxRowWidth; i+=2) // i tracks the current row
{
//nested loop controls indentation by printing spaces
for(int j=1;j<=(maxRowWidth-i)/2;j++){
cout << " ";
}
//nested loop controls printing one row of characters
for(int j=1;j<=i;j++){
cout << ch;
}
cout << endl;
}
//this nested loop structure outputs a right triangle with a vertical leg on its right
//the triangle is maxRowWidth rows high.
//outer loop controls the rows
for (int i=1;i<=maxRowWidth;i++){
//loop does the spaces
for(int j=1;j<=maxRowWidth-i;j++){
cout << " ";
}
//loop does the characters
for(int j=1;j<=i;j++){
cout << ch ;
}
cout << endl;
}
return 0;
}
Step by step
Solved in 2 steps with 1 images