Lesson 5 of 24

Loops: for, while and range-for

9 min 25 XP

for, while and do-while work like C. C++11 adds the range-based for: for (int x : list) visits every element.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Challenge

+25 XP

Read n and print the first n Fibonacci numbers separated by spaces (0 1 1 2 ...). Use long long. Input: 8 → Output: 0 1 1 2 3 5 8 13

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.