loops in c++ and some patterns
Hello all dear lectiophiles. I am back again with new topic regarding c++ programming language . Today i will share information about loops in c++ and then i will share the most common patterns that we code using loops . Hope you will understand it easily.
In c++ whenever we have to execute a statement repeatedly , then we make use of loops .
There are three types of loops in c++
For loop
the loop is consisted of following: -
for(initialization; condition; updation)
{
? body(code)
}
Initialisation means the value of i is starting from zero.
the loop will run as long as condition is true . here in this case as long as 'i' is less than or equal to 4 the loop will run.
Then the body of loop will be executed i.e. it will print 'i' .
Then is the role of updation and here the value of i will increase by one as we have used an increment operator.
While loop
while(condition):
{
? ? statements;
updation;
}
This is also same as 'for loop'.
It also consists of condition, statements, updation.
Usually we use while loop when no. of iteration to be performed are not known .
Do while loop
do
{
? ? statements;
updation;
}while(condition);
Here also we have statements , updation and condition.
But the one thing that creates the change is that in do while loop statement is executed at least once . This is because for the first time when loop executes it print statement first and then checks the condition.
Also don't forget to write semicolon(;) after do while loop .
Now with the help of loops we can code a number of patterns.
Rectangular Pattern :-
#include <iostream>
using namespace std;
int main(){
for(int i = 1;i<=5; i++){ Outer loop This loop will print rows in the pattern and here according to condition 5 rows will be printed.
??for(int j = 1; j<=4; j++){ // Inner loop This loop will print stars and according to condition 4 stars will be printed in one row .
????cout<<" * ";
??}
??cout<<endl; Here this statement is written outside the inner loop and it will print the next row in next line.
}
??return 0;
}
Half pyramid pattern :-
#include <iostream>
using namespace std;
int main(){
for(int i = 1;i<=5; i++){ Outer loop This will print rows of the pattern.
? ? for(int j = 1; j<=i; j++){ Inner loop This loop will print stars and according to the condition it will print stars increasing by one after each row.
? ? ? ? cout<<" * ";
? ? }
? ? cout<<endl; It is written outside the inner loop and it will insert a row.
}
?return 0;
领英推荐
}
Inverted half pyramid :-
#include <iostream>
using namespace std;
int main(){
for(int i = 1;i<=5; i++){
? ? for(int j = i; j<=5; j++){
? ? ? ? cout<<" * ";
? ? }
? ? cout<<endl;
}
?return 0;
}
Full pyramid :-
#include <iostream>
using namespace std;
int main(){
for(int i = 1;i<=5; i++){ Outer loop This loop is used for rows in pattern.
? ? for(int j = i; j<=5; j++){ Inner loop This loop is for spaces in the pattern.
? ? ? ? cout<<" ";
? ? }
? ? for(int j = 1; j<=i;j++){ Inner loop This loop is for stars in increasing order in the pattern.
? ? ? ? cout<<"* ";
? ? }
? ? cout<<endl;
}
?return 0;
}
Diamond pattern :-
#include <iostream>
using namespace std;
int main(){
for(int i = 1;i<=5; i++){ Outer loop This loop is for rows.
? ? for(int j = i; j<=5; j++){ Inner loop This loop is for spaces.
? ? ? ? cout<<" ";
? ? }
? ? for(int j = 1; j<i;j++){ Inner loop This loop is for stars.
? ? ? ? cout<<" *";
? ? }
? ? cout<<endl;
}
// this is half diamond
for(int i = 1;i<=5;i++){ again for rows.
for(int j = 1; j<=i; j++){ for spaces.
? ? cout<<" ";
? ? }
? ? for(int j = i; j<=5; j++){ for stars.
? ? ? ? cout<<"* ";
? ? }
? ? cout<<endl;
}
?return 0;
}
#linkedin #linkedinfamily #cpp #cpplus #coding #programmer #programming #software #loops #softwareengineering #enginnering #language #notes #freelearning #skills #post #cplusplus #cplusplusdeveloper #pattern #patterns #code #browser #browsers #tutorial #tutorials
RESOURCES :-