From 63aae858d72a1ff91ea7c06f12d8f6519b887f9b Mon Sep 17 00:00:00 2001 From: kzellers Date: Wed, 23 Jan 2019 12:53:37 -0800 Subject: [PATCH] added enhanced for loop example (#23915) * added enhanced for loop example * Update index.md * Update index.md * Update index.md --- guide/english/cplusplus/loops/index.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/guide/english/cplusplus/loops/index.md b/guide/english/cplusplus/loops/index.md index a63a6bc008..adfa7442c4 100644 --- a/guide/english/cplusplus/loops/index.md +++ b/guide/english/cplusplus/loops/index.md @@ -86,8 +86,7 @@ Now let's discuss how the for loop works. So, in this way the for loop works - If you want to print even numbers from 1 to 1000 then your program will look like this - + If you want to print even numbers from 1 to 1000 then your program will look like this: ``` c++ for (int i = 0;i<=1000;i=i+2) @@ -99,7 +98,7 @@ for (int i = 0;i<=1000;i=i+2) * The difference between the first program and second is the increment part. The rest of the code is the same. This program will print 0 and then add 2 to it and print 2 on console and so on upto value of i becomes equal to 1000. - Our final program to print even numbers from 0 to 1000 will look like this. + Our next program to print even numbers from 0 to 1000 will look like this: ``` c++ #include @@ -113,3 +112,21 @@ int main() return 0; } ``` +Another type of for loop is the [Range-based for loop](https://en.cppreference.com/w/cpp/language/range-for). + + ``` c++ + #include +using namespace std; +int main() +{ + int a[5] = {1, 2, 3, 4, 5}; + for (int &i : a) + { + cout << i << endl; + } + return 0; +} + ``` + + +