C#: Variable scope in nested block

This entry is part 26 of 71 in the series C#

A variable in a nested block can access variables declared in the outer block.

Consider the following example.

for (int x = 0; x < 5; x++)
{
    for (int y = 0; y < 3; y++)
    {
        Console.WriteLine(x);
        Console.WriteLine(y);
    }
}

The integer x is declared in the first line and it will be available for both the outer and the inner block.

The integer y is declared in the third line and it will be only available to this inner block.

The inner for loop block is printing out x that is declared in the outer block.  This is ok and running it will not generate an error.

Nested Code Block
using System;

namespace Nested
{
    class Program
    {
        static void Main()
        {
            for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 3; y++)
                {
                    Console.WriteLine(x);
                    Console.WriteLine(y);
                }
            }
            Console.ReadLine();
        }
    }
}

nested

C#: Variable scope

This entry is part 25 of 71 in the series C#

You can declare variables in several different places:

  • In a class body as class fields. Variables declared here are referred to as class-level variables.
  • As parameters of a method or constructor.
  • In a method’s body or a constructor’s body.
  • Within a statement block, such as inside a while or for block.

Variable scope refers to the accessibility of a variable. The rule is that variables defined in a block are only accessible from within the block. The scope of the variable is the block in which it is defined.

For example, consider the following for statement:

for (int x = 0; x < 5; x++)
{
    Console.WriteLine(x);
}

The variable x is declared within the for statement. As a result, x is only available from within this for block. It is not accessible or visible from anywhere else.

The next post talks about variables in a nested block.

C#: Variables

This entry is part 7 of 71 in the series C#

Variables are just like the variables that we learnt in Mathematics when we were in the ninth grade or secondary school.

Every variable has a type that determines what values can be stored in the variable.

Below, we assign a value of 25 to a variable age that is of type integer (int).

int age = 25
Case-sensitive

Similar to the original C programming language, C# variable declaration is case-sensitive.

The declaration below will create 2 variables age and Age.

int age = 25
int Age = 25

C# is a type-safe language, and the C# compiler guarantees that values stored in variables are always of the appropriate type.

Variable must be definitely assigned

A variable must be definitely assigned before its value can be obtained.

It has to be declared and assign a specific value.

Commonly used variables
using System;

namespace Variables
{
    class Program
    {
        static void Main()
        {
            int point = 90;
            string greeting = "Hello, World!";
            double bigNumber = 1e100;

            Console.WriteLine("{0} {1} {2}", point, greeting, bigNumber);
            Console.ReadLine();
        }
    }
}

hworld

The above example shows the commonly used variable types integer, string and double.

In line 13, notice how we need to put the variables into array like {} in the writeline line to output the 3 variables.

The {0} denotes the first variable and {1} denotes the second variable and so on.

MySQL: Using user-defined variables

This entry is part 9 of 26 in the series Learn MySQL

We can assign a value returned from a select statement to a user-defined variable.

This allows us to refer to it in later statements in the same session.

Those user-defined variables will not persist across sessions.

select @max_price := max(price) from artist;
select @max_price;

The max price was assigned to variable @max_price.

If you execute the 2 statements on Toad, the screenshot will look like below:

sql_variables

 

 

Javascript: Global variables

This entry is part 10 of 27 in the series Javascript Tutorial

The scope containing all of a program is called global scope.

This is the scope you are in when entering a script.  When you define a variable here, the variable can be accessed by all functions.

Inside the global scope, you can create a nested scope by defining a function.

<!DOCTYPE html>
<html>
<head>
    <title>Global Variables</title>
    
    <script>
        var global_str = 'I am a global string';

        function updateFunction() {
            var local_str = 'I am a local string';
            document.getElementsByTagName('p')[0].innerHTML = local_str;
        }
    </script>

</head>
<body>
<p>I am a string</p>
<button onclick='updateFunction()'>Click Me</button>
<br>
<script>
    document.write(global_str);
</script>
</body>
</html>