Control Statements in C#

 Control Statements in C#


Control statements are used to control the flow of execution in a program. C# has various control statements such as if-else, switch, while, do-while, for, foreach, etc. Here's an example code that uses if-else and switch statements:


int x = 5;

if (x > 0)

{

    Console.WriteLine("x is positive");

}

else if (x < 0)

{

    Console.WriteLine("x is negative");

}

else

{

    Console.WriteLine("x is zero");

}


int day = 2;

switch (day)

{

    case 1:

        Console.WriteLine("Monday");

        break;

    case 2:

        Console.WriteLine("Tuesday");

        break;

    case 3:

        Console.WriteLine("Wednesday");

        break;

    default:

        Console.WriteLine("Invalid day");

        break;

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...