+ 1

Why would this code give an error?

Got this question in a quiz, it was created by a user. It goes as followed : What is the output of this code? #include <iostream> using namespace std; int main(){ for (int i =0, i <7, i++){} cout<<i; I thought the correct answer would be 7? But i was wrong, the code would result in an error. Can anyone explain why? I think it was a syntax error?

11th Apr 2019, 12:52 AM
Kirsten Leonard
Kirsten Leonard - avatar
2 odpowiedzi
+ 2
Variable "i" only 'exists' within the for loop. When trying to call it outside of the loop this message appears. ..\Playground\:6:7: error: 'i' was not declared in this scope cout<<i; ^
11th Apr 2019, 1:34 AM
Diego
Diego - avatar
+ 2
Lookup variable scope in C.
11th Apr 2019, 3:45 AM
Sonic
Sonic - avatar