?? My First Program: Understanding Operators in C# ??

?? My First Program: Understanding Operators in C# ??

?? What Are Operators in C#?

Operators are symbols that perform operations on variables and values.

C# has different types of operators, including:

? Arithmetic Operators ? +, -, *, /, % (For calculations)

? Comparison Operators ? ==, !=, >, <, >=, <= (For conditions)

? Logical Operators ? &&, ||, ! (For decision-making)

? Assignment Operators ? =, +=, -=, *=, /= (For assigning values)

Let’s see them in action! ??

using System;

class program
{
    static void Main()
    {
        //  Arithmetic Operators
        int a = 10, b = 5;
        Console.WriteLine("Addition: " + (a + b));          // 15
        Console.WriteLine("Subtraction: " + (a - b));      // 5
        Console.WriteLine("Multiplication: " + (a * b));  // 50
        Console.WriteLine("Division: " + (a / b));            // 2
        Console.WriteLine("Modulus: " + (a % b));         // 0

        //  Comparison Operators
        Console.WriteLine("a is greater than b: " + (a > b));    // True
        Console.WriteLine("a is equal to b: " + (a == b));        // False
        Console.WriteLine("a is not equal to b: " + (a != b));   // True

        //  Logical Operators
        bool condition1 = (a > b);  // True
        bool condition2 = (a == b); // False
        Console.WriteLine("Logical AND: " + (condition1 && condition2)); // False
        Console.WriteLine("Logical OR: " + (condition1 || condition2));  // True
        Console.WriteLine("Logical NOT: " + (!condition1));              // False

        //  Assignment Operators
        int x = 10;
        x += 5;  // x = x + 5
        Console.WriteLine("Assignment (+=): " + x);  // 15
        x *= 2;  // x = x * 2
        Console.WriteLine("Assignment (*=): " + x);  // 30
    }
}        

?? Output of the Program:

Addition: 15  
Subtraction: 5  
Multiplication: 50  
Division: 2  
Modulus: 0  
a is greater than b: True  
a is equal to b: False  
a is not equal to b: True  
Logical AND: False  
Logical OR: True  
Logical NOT: False  
Assignment (+=): 15  
Assignment (*=): 30          

?? Key Takeaways:

? Arithmetic Operators perform mathematical operations.

? Comparison Operators help in decision-making.

? Logical Operators evaluate conditions using &&, ||, and !.

? Assignment Operators assign and modify variable values.

Mastering operators is a crucial step in learning C# programming! Now, I'm excited to explore loops, functions, and OOP concepts! ??

?? Have you tried using operators in C#? Share your thoughts and experiences in the comments! ??

要查看或添加评论,请登录

Divya Soni的更多文章

社区洞察