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; }
8 Respuestas
+ 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
+ 6
"int x = 0;"
+ 5
It was declared in the for loop
+ 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
+ 1
so,if I want to use some integer value or string value.. can I declare it in my code wherever it is needed??
+ 1
@Matte,thank you :)
+ 1
The variable's scope is essentially the block it is declared inside.
0
you're welcome :)