loops in c++ and some patterns
Loops in c++

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

for loop syntax

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 loop syntax

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 while loop syntax

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.

Patterns overview


Rectangular Pattern :-

Rectangular pattern syntax

#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.

No alt text provided for this image

??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 :-

No alt text provided for this image

#include <iostream>

using namespace std;

int main(){

for(int i = 1;i<=5; i++){ Outer loop This will print rows of the pattern.

No alt text provided for this image

? ? 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 :-

No alt text provided for this image

#include <iostream>

using namespace std;

int main(){

for(int i = 1;i<=5; i++){

? ? for(int j = i; j<=5; j++){

? ? ? ? cout<<" * ";

No alt text provided for this image

? ? }

? ? cout<<endl;

}

?return 0;

}

Full pyramid :-

Syntax

#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<<" ";

Output

? ? }

? ? 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 :-

No alt text provided for this image

#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<<" *";

No alt text provided for this image

? ? }

? ? 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;

}


#like #save #share #comment

#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 :-

#codewithharry #collegewallah #w3schools #tutorialspoint #apnacollege #google #greatlearning #microsoft #freecodecamp

要查看或添加评论,请登录

社区洞察

其他会员也浏览了