paint-brush
The 30-Day .NET Challenge Day 2: Variable Scope & Logic Controlby@ssukhpinder
124 reads

The 30-Day .NET Challenge Day 2: Variable Scope & Logic Control

by Sukhpinder SinghMarch 21st, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Code blocks in programming are essential for grouping code lines and controlling variable accessibility. Code blocks play a crucial role in determining the scope of variable declarations. A variable declared within a code block is locally scoped, meaning it is accessible only within that specific block. Attempting to access the variable outside the block will result in a compiler error.
featured image - The 30-Day .NET Challenge Day 2: Variable Scope & Logic Control
Sukhpinder Singh HackerNoon profile picture

Introduction

Code blocks in programming are essential for grouping code lines and controlling variable accessibility. Variable scope, which determines where a variable can be accessed, is influenced by code blocks.

Learning Objectives:

  • Gain insight into the implications of declaring and initializing variables within and outside code blocks.

Prerequisites for Developers

  • Declaring and initializing variables.
  • Utilizing if-else selection statements.
  • Employing for each iteration statement.
  • Calling methods from classes within the .NET Class Library.

Getting Started

How do code blocks impact variable scope?

Code blocks play a crucial role in determining the scope of variable declarations. Variable scope refers to the visibility of a variable within your application’s code. A variable declared within a code block is locally scoped, meaning it is accessible only within that specific block. Attempting to access the variable outside the block will result in a compiler error.

Declare a variable inside the code block.

To begin, create a static class file called “CodeBlocksAndScope.cs” within the console application. Insert the provided code snippet into this file.

/// <summary>
/// Output
/// Inside the code block: 10
/// </summary>
public static void VariableInCodeBlock()
{
    bool flag = true;
    if (flag)
    {
        int value = 10;
        Console.WriteLine($"Inside the code block: {value}");
    }
}

Execute the code using the main method as follows.

#region Day 2 - Variable Scope & Logic Control with Code Blocks

CodeBlocksAndScope.VariableInCodeBlock();

#endregion

Console Output

// Console Output
Inside the code block: 10

Access a variable outside the code block

Add another method into the same static class wherein the code attempts to access the variable outside the code block.

/// <summary>
/// Outputs
/// Program.cs(7,46): error CS0103: The name 'value' does not exist in the current context
/// </summary>
public static void VariableOutCodeBlock()
{
    bool flag = true;
    if (flag)
    {
        int value = 10;
        Console.WriteLine($"Inside the code block: {value}");
    }

    //Uncomment below line to validate
    //Console.WriteLine($"Outside the code block: {value}");
}

Execute the code from the main method as follows.

#region Day 2 - Variable Scope & Logic Control with Code Blocks

CodeBlocksAndScope.VariableOutCodeBlock();

#endregion

Console Output

// Console Output
Program.cs(7,46): error CS0103: The name 'value' does not exist in the current context

This error is generated because a variable that’s declared inside a code block is only accessible within that code block.

Declare a variable unassigned above the code block & access inside the block.

Add another method into the same static class wherein the code attempts to access the variable, i.e., declared above the code block, but it is not initialized.

/// <summary>
/// Outputs
/// Program.cs(6,49): error CS0165: Use of unassigned local variable 'value'
/// </summary>
public static void VariableAboveCodeBlock()
{
    bool flag = true;
    int value;

    if (flag)
    {
        //Uncomment below line to validate
        //Console.WriteLine($"Inside the code block: {value}");
    }

    value = 10;
    Console.WriteLine($"Outside the code block: {value}");
}

Execute the code using the main method as follows.

#region Day 2 - Variable Scope & Logic Control with Code Blocks

CodeBlocksAndScope.VariableAboveCodeBlock();

#endregion

Console Output

// Console Output
Program.cs(6,49): error CS0165: Use of unassigned local variable 'value'

Declare a variable assigned above the code block & access inside the block

Add another method into the same static class wherein the code attempts to access the variable i.e., declared above the code block, and assign a value

/// <summary>
/// Outputs
/// Inside the code block: 0
/// Outside the code block: 10
/// </summary>
/// <returns></returns>
public static void VariableAboveCodeBlockv1()
{
    bool flag = true;
    int value = 0;

    if (flag)
    {
        Console.WriteLine($"Inside the code block: {value}");
    }

    value = 10;
    Console.WriteLine($"Outside the code block: {value}");
}

Execute the code from the main method as follows

    #region Day 2 - Variable Scope & Logic Control with Code Blocks
    
    CodeBlocksAndScope.VariableAboveCodeBlockv1();
    
    #endregion

Console Output

    // Console Output
    Inside the code block: 0
    Outside the code block: 10

Complete Code on GitHub

GitHub - ssukhpinder/30DayChallenge.Net

C# Programming🚀

Thank you for being a part of the C# community! Before you leave:

If you’ve made it this far, please show your appreciation with a clap and follow the author! 👏️️

Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter Visit our other platforms: GitHub | Instagram More content at C# Programming


Also published here.