+ 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?
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;
^
+ 2
Lookup variable scope in C.