0

I was going through the tutorial. But in one e.g.:there is no declaration of integer 'x' in the code.why? Or it isn't necessary?

#include <iostream> using namespace std; int main() { int arr[] = {11, 35, 62, 555, 989}; int sum = 0; for (int x = 0; x < 5; x++) { sum += arr[x]; } cout << sum << endl; return 0; }

28th Jul 2017, 9:50 PM
Vinamra Kishore
Vinamra Kishore - avatar
8 odpowiedzi
+ 4
@Vinamra yes but pay attention because there are Global and Local variables. a global variable is usually declared at the beginning of the code, outside the main() and you can use it anywhere a local variable is defined only inside a function or a loop and outside of it the variable doesn't exist try to take a look at this code https://code.sololearn.com/cllhemp4oUo7/?ref=app
28th Jul 2017, 10:07 PM
Matte
Matte - avatar
+ 6
"int x = 0;"
28th Jul 2017, 9:52 PM
Rrestoring faith
Rrestoring faith - avatar
+ 5
It was declared in the for loop
28th Jul 2017, 9:53 PM
Marko Majstorovic
Marko Majstorovic - avatar
+ 4
in the for there is "int x=0" this is a declaration useful when you want to use the variable in a for loop
28th Jul 2017, 9:54 PM
Matte
Matte - avatar
+ 1
so,if I want to use some integer value or string value.. can I declare it in my code wherever it is needed??
28th Jul 2017, 9:57 PM
Vinamra Kishore
Vinamra Kishore - avatar
+ 1
@Matte,thank you :)
28th Jul 2017, 10:12 PM
Vinamra Kishore
Vinamra Kishore - avatar
+ 1
The variable's scope is essentially the block it is declared inside.
28th Jul 2017, 10:56 PM
Rrestoring faith
Rrestoring faith - avatar
0
you're welcome :)
28th Jul 2017, 10:13 PM
Matte
Matte - avatar