Hi everyone I'm trying to do for loops on my own and I'm having a hard time (beginner c++) its coming out as no output why? | Sololearn: Learn to code for FREE!
0

Hi everyone I'm trying to do for loops on my own and I'm having a hard time (beginner c++) its coming out as no output why?

#include <iostream> using namespace std; int main() { for(int i = 12; i >= 18; i+=2){ cout << i << " "; } } // im trying to do 12 14 16 18 20

9th Nov 2020, 3:29 AM
boba
boba - avatar
3 odpowiedzi
+ 3
for(int i = 12; i >= 18; i+=2) Problem is here. Look at the condition, i >= 18, i is 12 so this condition will be false and loop will not run. Instead change the condition to: i <= 18, this condition is true and will give required output. for(int i = 12; i <= 18; i+=2) //Correct statement //Corrected code #include <iostream> using namespace std; int main() { for(int i = 12; i <= 18; i+=2){ cout << i << " "; } }
9th Nov 2020, 3:39 AM
ツSampriya😘ツ
ツSampriya😘ツ - avatar
+ 1
The loop condition is the problem, look again, and you'll see why the loop isn't running.
9th Nov 2020, 3:36 AM
Ipang
+ 1
Change your code like that i<=18;I+=2 because in your loop is not executed due to wrong condition And use endl to replace " ".
9th Nov 2020, 3:36 AM
Sâñtôsh
Sâñtôsh - avatar