Skip to content

Instantly share code, notes, and snippets.

@yoshionimusha
Created May 1, 2021 01:45
Show Gist options
  • Save yoshionimusha/2f04ffd4c6659619cf30f35b0f84419c to your computer and use it in GitHub Desktop.
Save yoshionimusha/2f04ffd4c6659619cf30f35b0f84419c to your computer and use it in GitHub Desktop.
namespace SoloLearn //example 1
{
class Program
{
static void Main(string[] args)
{
int[ ] arr = {11, 35, 62, 555, 989};
int sum = 0;
int x = 0; //why does this work when example 2 doesn't work
for ( ; x < 5; x++) {
sum += arr[x];
}
Console.WriteLine(sum);
}
}
}
namespace SoloLearn //example 2. (similar to 3) why no output?
{
class Program
{
static void Main(string[] args)
{
int[ ] a = new int[10];
int k = 0; //leads to no output. why?
for ( ; k < 10; k++)
a[k] = k*2;
for ( ; k < 10; k++)
Console.WriteLine(a[k]);
}
}
}
namespace SoloLearn //example 3
{
class Program
{
static void Main(string[] args)
{
int[ ] a = new int[10];
int k = 0; /* why does it cause an error message? how to understand scope better? A local or parameter named 'k' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter */
for ( ; k < 10; k++)
a[k] = k*2;
for (int k = 0; k < 10; k++) //int k = 0; in for loop
Console.WriteLine(a[k]);
}
}
}
namespace SoloLearn //example 4
{
class Program
{
static void Main(string[] args)
{
int[ ] a = new int[10];
for ( int k = 0; k < 10; k++)
a[k] = k*2;
for (int k = 0; k < 10; k++)
Console.WriteLine(a[k]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment