Basics of c#

Maleesha Wickramarathna
3 min readMar 14, 2021

Variables

There are several types of variables in c#. Variables are used to store some data and manipulate it in a program.

int — stores integers (Whole numbers) ex: — 123

int a = 123; //type variable_name = value;

double — stores floating point numbers including decimal values.

double a = 69.54;// type variable_name = value;

string — stores texts ex:- Hello world

string a = “Hello world!”;

When we are initializing variable values, we can get user inputs as well. So, when we are getting user inputs we have to add statements like below,

Console.Write(“insert your name : “ ) // this is for displaying purpose

string a = Console.Read();

Loops

For Loop

int i = 0;// declare integer type variable and assign value to 0.

i < 5 ; //This is the condition point in this loop. Until this condition getting false this loop will execute.

i++ // This is using to increment the value by one. It is same as (i = i + 1;) . Every for loop must have an increment statement or decrement statement other vice it will be an infinite loop.

For loop is the easiest loop as well as if we are using this loop, we must know the end value of the loop.

While Loop

Always we don’t know the starting value. So it may be user input. When I was doing this I thought to explain if we are asked to get user input,

NOTE: When we are getting always string value from “Console.ReadLine();”

Don’t worry thus you are having a string value. You can convert it into your preferred data type. There are several methods to convert. I will discuss it later. Now you can just remember, you can call Parse() method to convert.

Here what I have done, getting input value to int type variable called “a”. after that while loop is checking that “a” value is less than 5, if it is true then the loop will execute. Or else the loop will stop.

Do-While Loop

The do-while loop is special than other loops thus this loop checks the condition at the end.

Conditions

Conditional statements are more impotent of programming. When we have to make decisions we can use the “IF” condition or “SWITCH CASE”.

1. When we are considering conditional statements there are mainly two paths.

True part: when the condition is satisfied. the true part will be executed.

False part: when the condition is not satisfied. the false part will be executed.

IF ELSE statement
SWITCH CASE

Loops in c#

--

--

Maleesha Wickramarathna

I am a 3rd-year Software Engineering undergraduate at the Sri Lanka Institute of Information Technology- SLIIT ,Reg.no :IT19004778