C# Keywords Tutorial Part 45: int

C# Keywords Tutorial Part 45: int

C# is a popular object-oriented programming language used in building desktop applications, web applications, and games. One of the fundamental data types in C# is the integer data type, which is represented using the “int” keyword. In this blog post, we’ll explore the “int” keyword in C# and provide some code examples to illustrate its usage.

The “int” keyword is a reserved word in C#, and it is used to declare variables of type integer. The integer data type is used to store whole numbers within a specified range. In C#, an integer can be represented using 32 bits, which means that it can store values between -2147483648 and 2147483647.

Here is an example of declaring a variable of type integer:

int age = 30;

In the above example, we’ve declared a variable called “age” and assigned it a value of 30. The compiler knows that “age” is an integer because we’ve declared it using the “int” keyword.

We can also perform mathematical operations on integers. Here’s an example:

int num1 = 10;
int num2 = 5;
int result = num1 + num2;
Console.WriteLine(result); // Output: 15

In the above example, we’ve declared two variables, “num1” and “num2”, and assigned them the values 10 and 5, respectively. We’ve then declared a third variable called “result” and assigned it the value of the sum of “num1” and “num2”. Finally, we’ve printed the value of “result” to the console.

We can also use the “int” data type in loops. Here’s an example of using an “int” variable in a “for” loop:

for (int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}

In the above example, we’ve declared an integer variable called “i” and initialized it to 0. We’ve then created a “for” loop that will execute as long as “i” is less than 10. In each iteration of the loop, we print the value of “i” to the console and increment it by 1.

Share this post

Comment (1)

  • Johnson Reply

    Nice blog here! Additionally your website so much up fast! What web host are you using? Can I get your affiliate link in your host? I desire my website loaded up as fast as yours lol.

    April 19, 2024 at 8:18 PM

Leave a Reply

Your email address will not be published. Required fields are marked *