Skip to content
← Back to Community
C# Tutorial - Variables, If/Else, While Loop and More!
Profile icon

Hi and welcome to this C# tutorial! Hope this helps you out!

Important notes:

  • C# uses // for single line comments.
    • Ex: //computer doesn't read this
  • C# places a semicolon ; at the end of each statement.
    • Ex: int a = 5;


These are the main variables which will be dicussed here. There are more but here are the basics.

  • Strings:
    • They are made up of multiple characters.
    • They are surrounded by double quotes
    • Ex: string name = "Bob";
  • Integers:
    • Are integers. Are whole numbers.
    • Ex: int b = 6;
  • Doubles:
    • Have a decimal point and are more precise than integers.
    • Ex: double num = 5.01;
  • Booleans:
    • Are either true or false.
    • Ex: bool fun = true;


In the that is included with this tutorial there are some conversions.
.toString(); -> Converts to a string
Convert.toDouble(); -> Converts to a double
Convert.toInt32(); -> Converts to an int

Output and Input

  • Output:
    • Console.WriteLine(); is used to write something on a new line.
    • Console.Write(); writes something on the same line
      • Ex: Console.WriteLine("Hello World");
  • Input:
    • Console.ReadLine(); returns the input as a string.

If/Else Statements (Conditionals)

An if statement is executed when something results to true. An if statement has a condition and body. If the condition is true then the body will run.

An example of a condition is a == 5. Note that the == is used because it is comparing two things. A != is the comparison operator for not true.

Example of an if statement:

int choice = Convert.ToInt32(Console.ReadLine()); if(choice == 1) //addition { double addition_output = first_num + second_num; Console.WriteLine("You have choosen addition!"); Console.WriteLine("Output: " + addition_output); }

An else statement is only executed if the rest are false. If you use 3 if statements and then an else statement then the code will try each if statement but if you use an if- else if- else setup then the code will only try 1 if statement.

While Loop

A while loop can be used to continue to do something while a condition results to true.

In the calculator program included in this tutorial there is a variable declared at the very beginning like this: bool try_again = true;. Then at the end, once the person has finised, if they input something other than yes for continuing the game the else statement body contains code which is try_again = false which terminates the while loop. Every while loop needs a terminating condition or it will become an infinite loop!

Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon
Profile icon


Profile icon

How can I do a global variable?

Profile icon

What does "Int32" mean?

Profile icon

@SquishyFB8532XD Int32 is basically an integer variable and is used in conversion statements. When you are declaring a variable, instead of Int32, you have to use int, assign the variable a name and a value.

Profile icon

When I was leaning software development, I faced a lot of problems with the if statements. That was the main reason I abandoned learning javascript and then quit the course altogether. Afterwards, I bought myself the best TWS earbuds under 5000 rupees and really focused on what I wanted to get out of my life. Soon, I learned that I will do much better in Digital Marketing, which was my passion since childhood.

Profile icon

The Pipeline Expansion Plan supports this goal by bridging the gap between late-stage development pipelines at top rated essay writing services in need of innovative therapies with Janssen's deep expertise in traditional drug delivery formulation technologies.

Profile icon

While it might be more complex, is it possible to add Pi, Sin, Tan & Cos equations too? Or am I better off with something more along the lines of R?

Very good tutorial however! Helped learn the basics :D

Profile icon

@MyNamesNick Thank you! It would be possible to do so using Math.Sin(), Math.Cos(), and Math.Tan(). Here's an article explaining that:

Profile icon

This really helps. Thanks!

Profile icon

How did you get it to return to the beginning? I didn't see an "if" for "yes" only for something else.

Profile icon

@SquishyFB8532XD The if statement is inside of a while loop. The if statement checks if the input is not yes. If the input is not yes, then the boolean condition of the while loop (named try_again) is made false and therefore the while loop condition will not be evaluated to true and terminate.

Profile icon

In every programming language, loops run over and over until the condition is true.
// The sentence above is important. You should remember it.

For example, if I would only put the [try_again] variable name in the while loop (between the brackets), it would still work because the variable itself is a boolean which can only be [true or false] (and loops run until the condition in the brackets is true), so the loop will run until the variable's value is true.
The loop breaks when the variable is set to false, because the loop requires a true condition to run and the variable's value is not true.
Here's an examle for you:

using System;

class MainClass {
public static void Main (string[] args) {
bool try_again = true;
while(try_again) {
// the value of the variable is [true] so the loop runs
Console.WriteLine("The loop is running");
try_again = false
// the variable's value is set to [false] so the loop will stop because it only runs if the condition in it is [true]

I hope I explained it well enough =D

Profile icon


Profile icon

nice i love it