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
3 Answers
+ 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 << " ";
}
}
+ 1
The loop condition is the problem, look again, and you'll see why the loop isn't running.
+ 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 " ".