Connect with us

Hi, what are you looking for?

Game Development

C# For Beginner Tutorial – 2

C# For Beginner Tutorial – 2
C# For Beginner Tutorial – 2

Welcome to part 2 of the C # tutorial, in this lesson we will learn the data types, variables, Arithmetic Operators, Relational Operators, Logical operators, Assignment operators, Conditional Statements, and more…

If you haven’t read the first article, you can read it here so you can refresh your knowledge or learn about topics you don’t know.

Topics To Learn

  • Data Types and Variables
  • Operators In C#
  • Arithmetic Operators
  • Relational Operators
  • Logical operators
  • Assignment operators
  • Conditional Statements (IF – ELSE IF – ELSE)
  • Conditional Statements 2 (Switch – Case)
  • Summary This Lesson

Data Types And Variables

data types tell the computer the type of data to be stored, for example, the datatype of an integer data is int.

The speech-language we use in daily life is a string data type for the computer, the computer perceives all texts as a string data type.

Or when the integers are not enough, the decimal numbers we use are the float data type for the computer.

int -> For integer value
float -> For decimal value
string -> For text value
bool -> For true or false value

We generate variables using these data types. Variables take and store the value of the specified data type.

The data type of the variables and the data in it can be changed. After learning about the variables, we will look at how data types can be exchanged between each other.

using System;

class Program
{
   static void Main(string[] args)
   {
     // Int value (x is variable)
     int x = 1;
            
     // Float value (y is variable)
     float y = 1.20f;

     // String value (z is variable)
     string z = "Hello"
            
     // Bool Value (k is variable)
     bool k = true
    }
}

As a syntax, enter the data type then types the variable name and assigns its value, it’s that easy. Let’s learn how to change the data types of these variables.

When we want to change a data type, we write the function of the data type, now let’s see it in the code.

Convert.ToDouble(variable_name) 
Convert.Int32(variable_name) 
Convert.ToString(variable_name) 
Convert.ToBoolean(variable_name) 

We also have a useful function GetType, this function gives us the data type of the variable. Let’s look at the GetType syntax and repeat what we’ve learned in a single example.

variable_name.GetType();

In the exercise we will do, we will create a variable with 3 different data types and print the information about these variables on the screen.

using System;

class Program
{
  static void Main(string[] args)
  {
    int x = 111;
    float y = 0.0f;
    bool z = true;

    Console.WriteLine("Information");
    Console.WriteLine(x.GetType());
    Console.WriteLine(y.GetType());
    Console.WriteLine(z.GetType());

    Console.WriteLine("Changed Information");
    Console.WriteLine(Convert.ToDouble(x).GetType());
    Console.WriteLine(Convert.ToBoolean(y).GetType());
    Console.WriteLine(Convert.ToInt32(z).GetType());
    }
}
Output:

Information
X type: System.Int32
Y type: System.Single
Z type: System.Boolean

Changed Information
X type: System.Double
Y type: System.Boolean
Z type: System.Int32

Operators In C#

Math operations can be done with variables or data in C #. Operators have divided into 6 main topics: Arithmetic Operators, Relational Operators, Logical operators, Assignment operators.

In this section, we will learn all the operators one by one. Let’s start with mathematical operations (Arithmetic Operators).

using System;

class Program
{
  static void Main(string[] args)
  {
    int a = 2;
    int b = 4;

    Console.WriteLine(a + b); 
    Console.WriteLine(b - a); 
    Console.WriteLine(b * a); 
    Console.WriteLine(b / a); 
    Console.WriteLine(b % a);
 }
}
Output:
6
2
8
2
0

In addition, C # also provides us with ++ and – operators to increase a value. These operators increase or decrease a value by 1 time.

int i = 0;

i++; // Increase 1
i--; // Decrease 1
Relational Operators

We use these operators in comparison operations, we will handle a little more in the condition statements section.

using System;

class Program
  {
    static void Main(string[] args)
    {
        bool b;
        int x = 2;
        int y = 10;

        b = (y > x); // y is bigger than x
        Console.WriteLine("value:" + b);

        b = (y < x); // y is smaller than x
        Console.WriteLine("value:" + b);

        b = (y == x); // y is equal is x
        Console.WriteLine("value:" + b);

        b = (y != x); // y isn't equal is x
        Console.WriteLine("value:" + b);
            
        b = (y >= x); // y is bigger than or equal to x
        Console.WriteLine("value:" + b);

        b = (y <=  x); // x is bigger than or equal to y
        Console.WriteLine("value:" + b);
    }
}
Output:
True
False
False
True
True
False
Logical Operators

We will use logic operators in Conditional statements in the same way, so we should understand its logic well.

&& -> the operator returns true when both the conditions in consideration are satisfied.

|| -> operator returns true when one (or both) of the conditions in consideration is satisfied.

! -> operator returns true the condition in consideration is not satisfied.

The names of these operators are “and”, “or” and “not” respectively. We will see these operators again in the conditional statements.

using System;

class Program
{
    static void Main(string[] args)
    {
        bool x = true;
        bool a = true; bool b = false;
          
        // AND operator
        x = a && b;
        Console.WriteLine("AND Operator: " + x);
              
        // OR operator
        x = a || b;
        Console.WriteLine("OR Operator: " + x);
              
        // NOT operator
        x = !a;
        Console.WriteLine("NOT Operator: " + x);
    }
}
Output:

AND Operator: False
OR Operator: True
NOT Operator: False
Assignment Operator

We don’t just use the equals operator to assign values, now we’ll examine the other assignment operators.

= -> to assign the left value to the right value
+= -> to increase the left value by the right value
-= -> to decrease the left value by the right value
*= -> multiplies the left value by the right value
/= -> divides the left value by the right value

All the concepts used here actually save the operation to be done from reassigning. For example, a += 1 (a = a + 1). Let’s use these operators in the example.

using System;

class Program
{
  static void Main(string[] args)
  {
    float x = 12.0f;

    x += 2.5f;
    Console.WriteLine(x);

    x -= 2.5f;
    Console.WriteLine(x);
    
    x *= 2.5f;
    Console.WriteLine(x);
    
    x /= 2.5f;
    Console.WriteLine(x);
 }
}
Output:
14,5
12
30
12

Conditional Statements

This topic is related to the previous topic, if there is any point you do not understand, it is recommended to repeat it.

Condition statements allow a program to produce more than one output according to changing situations instead of producing a single result.

For example, suppose we have two numbers, these two numbers could be equal to each other, the first number could be greater than the other number, and finally the second one could be greater than the first number.

In such cases, when we need more than one output, conditional statements come to help, let’s look at the syntax.

if(condition)
{
   // Code Block
}

else if(condition)
{
   // Code Block
}

else
{
   // Code Block
}

Here the first “if” is executed, then “else if” runs, then “else” runs. You can increase the else if structures as much as you want. Let’s do an example.

using System;

class Program
{
  static void Main(string[] args)
  {
    int a = 10;
    int b = 20;

    if(a < b)
    {
      Console.WriteLine(b + " is bigger than " + a);
    }

    else if(a > b)
    {
      Console.WriteLine(a + " is smaller than " + b);
    }

    else
    {
      Console.WriteLine(a + " is equal to " + b);
    }
 }
}
Output:
20 is bigger than 10

Here, if a was greater than b, the “else if” construct would work, but the “if” construct worked because the condition in “else if” was not met, if the values did not satisfy both conditions, the “else” construct would work.

Let’s now use the operators we learned earlier with if – else constructs and repeat what we learned.

using System;
class Program
{
  static void Main(string[] args)
  {
    int a = 10;
    int b = 20;

    bool c = true;
    bool d = true;
    bool k = false;

    if(a + b == 30)
    {
      Console.WriteLine(b + " is bigger than " + a);
    }

    if(c && d == true)
    {
      Console.WriteLine("Both True");
    }

    if(c || k == true)
    {
      Console.WriteLine("True");
    }

    else
    {
      Console.WriteLine("Error");
    }
 }
}
Output:
20 is bigger than 10
Both True
True

Here, we did not use “else if” to see all three values because if the “if” condition is met, the “else if” condition is not checked, so it does not work automatically.

Switch Case Statements

A separate condition in Switch Case is a statement. Same as if – else if – else it is used to check condition states. Let’s find out.

using System;

class Program
{
  static void Main(string[] args)
  {
    int a = 10; 

    switch(a)
    {
      case 5:
        Console.WriteLine("a is 5");
        break;

      case 10:
        Console.WriteLine("a is 10");
        break;
                  
      default:
        Console.WriteLine("Error");
        break;
    }
 }
}
Output: a is 10

We will see this, for now, we will continue to use the switch-case concept after learning about other data types.

Summary This Lesson

Yes, in this lesson we learned variables and some simple data types, then we finished the operators and examine the condition structures. In the next lesson, we will look at loops.

We will review the Function concept we learned earlier and use it in C #. Prepare well before moving on to the next lesson, and we’ll see you in the next lesson.

1 Comment

1 Comment

  1. malissaeck

    April 1, 2021 at 21:11

    Its not my first time to pay a visit this web page, i am browsing this website dailly and obtain pleasant information from here everyday.

Leave a Reply

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

You May Also Like

Programming

This article is prepared to explain concepts that will be important for programming beginners such as data types, variables, heap and stack, and operators....

Programming

This article was created for those who want to use functional programming in their career or who are learning new programming paradigms. We will...

Programming

Functions are pieces of code that combine the integrity of the code used to perform a task into a single structure. If you are...

Programming

Computers can solve an event in different ways, we call these ways algorithms, each algorithm works differently from the others and has different processing...