Blog Introductions
In this blog contains tutorials about HTML Tutorials, Python Tutorials, CSS Tutorials, How to, PHP Tutorials, Java Tutorials, C++ Tutorials, Tutorials, Examples, Source code,Learning,
Tips and Software development services. C++ Loops for loop while loop and do while loop | C++ Tutorials Bestitworriors
C++ Loops for loop while loop and do while loop
C++ Loops for loop while loop and do while loop | C++ Tutorials Bestitworriors - Loop execute the block of code for multiple time untill specific given condation is matched.Loops execute many times still reach given condation.C++ have following loops for loop , while loop ,do while loop.The purpose is same of all loops but the structure is different.
Some Key Points of C++ Loops
- Loop execute block of code for multiple times
- If you know how many time your loop will iterate you can use for loop instead of while loop
- The syntex of for loop for(int i=0;i<19;i++){ //Code }
- The syntex of while loop is while(i>9){//Code}
- Do-while loop execute first and then check the condations
- We can use continue and break statement in loops
- Continue statement use for switch the current iteration
- Break statement use to jump out of loop when certain condation is matched
Code
#include <iostream>
using namespace std;
int main() {
int var1 = 0;
while (var1 < 5) {
cout << var1 << "\n";
var1++;
}
int var2 = 0;
do {
cout << var2 << "\n";
var2++;
}
while (var2 < 5);
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << "\n";
}
return 0;
}
OutPut
0
1
2
3
4
0
1
2
3
4
0
1
2
3
0
1
2
3
5
6
7
8
9
1
2
3
4
0
1
2
3
4
0
1
2
3
0
1
2
3
5
6
7
8
9
0 Comments