C++ Loops Complete Example

In this C++ article we are going to talk about Loops Complete Example in C++, we are going to learn different C++ Loops like For Loop, While Loop and Do While Loop.  There may be a situation, when you need to execute a block of code several number of times, using loops you can do these kind of functionalities. A loop statement allows us to execute a statement or group of statements multiple times. There are different types of loops that you can use in c++, for loop, while loop, do while loop, also there are different control statements like break and continue in c++ loops, using control statements we can change the execution of loops from its normal sequence.

 

Note: you can watch the complete video for this article(C++ Loops Complete Example ) at the end.

 

 

Also you can check more articles on c++ programming language

1: C++ Introduction & Program Structure 

2: C++ Variables And Data Types 

3: C++ Getting User Input 

4: C++ If Else Statement 

5: C++ Short Hand If Else

6: C++ Switch Statement 

 

 

1: For Loop

 

So now this is the code for this loop.

 

int i, this is the initial step of the loop and this step will be executed first and only once.

i < 10, this is the condition for the loop, if it is true the body of the loop is executed, if it is false body of the loop

does not execute and flow of the control jumps.

i++, in here we need to increment the value according to the loop condition.

 

 

 

If you execute the code this will be the result.

 

 

 

2: While Loop 

This is the simplest type of loops in c++, it will repeats statement while expression is true, and if the expression is no

longer true, the loop ends, and the program continues after that loop.

 

 

 

 

If you run the code this will be the result

 

 

 

 

3: Do While Loop

Do while loop is like while and for loop, but with the change that do while loop tests the condition at the bottom of the loop. and do while loop is guaranteed to execute at least one time. now in the for and while loop the condition is tested at the top of the code, but in do while loop it is done in the bottoms of the code.

 

 

 

 

 

If you execute the code this will be the result

 

 

 

 

Break Statement 

When we use break statement in a loop, the loop is immediately terminated and program control resumes  at the next statement.

 

 

 

 

 

If  you execute the code this will be the result

 

 

 

Break statement with do while loop

 

 

 

 

Execute the code this will be the result

 

 

 

 

Continue Statement 

Continue statement works like break statement, but instead of terminating the loop, it continues to the next iteration of the loop and skip any code that is in the between.

 

 

 

 

 

Run the code and this is the result

 

 

 

 

Also you can watch the complete video for this article

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×