Note: The reference code in this article is written in C#.
So, you got FIVE minutes? Let's clear up the AIR first and begin exploring programming fundamentals.
I would highly recommend, if you can just FIRE-UP a console Application in Visual Studio and practice alongside!
Data Types
Data types define what kind of value a variable can store. Some store numbers, some store text, while others store true or false values.
Basic Types
int— 32-bit integerlong— 64-bit integershort— 16-bit integerbyte— 8-bit unsigned integersbyte— 8-bit signed integeruint— unsigned 32-bit integerulong— unsigned 64-bit integerushort— unsigned 16-bit integer
Decimal & Logic Types
float— 32-bit decimaldouble— 64-bit decimaldecimal— high precision decimalbool— true or falsechar— single character
Custom Value Types
struct— custom value typeenum— named constants
Reference Types
string— text valuesobject— base type of all objectsclass— custom reference typeinterface— contract definitiondelegate— method referencearray— collection of same typedynamic— resolved at runtime
Declaration & Assignment
Variables are used to store values in memory. Before using a variable, it must first be declared.
int x = 10;— variable declarationstring value = "DotNetVerse";— a value/textint a = 1, b = 2;— multiple assignments
public static void Main()
{
Console.WriteLine("Examples for the explained code @ DotNetVerse");
int x = 10;
string value = "DotNetVerse";
int a = 1, b = 2; //Multiple Assignments
Console.WriteLine(x);
Console.WriteLine(value);
Console.WriteLine("{0},{1}",a,b);
}
Examples for the explained code @ DotNetVerse 10 DotNetVerse 1,2
“Now that we can store data, let’s make decisions using conditions.”
Conditional Statements
Conditional statements help programs make decisions based on conditions.
if— executes when condition is trueelse if— checks another conditionelse— executes when all conditions fail
We have three types of conditional statement checking:
First is equal to ( == ) Second is greater than ( > ) Third is smaller than ( < )We can also combine checks: [ > / < with == ]
if(x == 10)
{
Console.WriteLine("x is 10");
}
else if(a>b)
{
Console.WriteLine("a is greater than b");
}
else if (a<b)
{
Console.WriteLine("a is smaller than b");
}
Examples for the explained code @ DotNetVerse x is 10 a is smaller or equal to b
Loops
“Based on our condition, let’s print out without rewriting again and again using loops.”
Loops allow repetitive execution of code blocks without writing the same code multiple times.
-
for— classic counter-based looppublic class Program { public static void Main() { Console.WriteLine("Examples for the explained code @ DotNetVerse"); for (int i = 0; i < 5; i++) { Console.WriteLine(i); // prints 0 to 4 } } }Examples for the explained code @ DotNetVerse 0 1 2 3 4
-
foreach— iterates through collectionsusing System; public class Program { public static void Main() { Console.WriteLine("Examples for the explained code @ DotNetVerse"); string[] names = { "Alice", "Bob", "Charlie" }; foreach (string name in names) { Console.WriteLine(name); } } }Examples for the explained code @ DotNetVerse Alice Bob Charlie
-
while— checks condition before executionIf count starts at 10, the body never runs — condition is checked first.
public class Program { public static void Main() { Console.WriteLine("Examples for the explained code @ DotNetVerse"); int count = 0; while (count < 5) { Console.WriteLine(count); count++; } } }Examples for the explained code @ DotNetVerse 0 1 2 3 4
-
do while— executes once before checking conditionEven though count < 5 is false from the start, it still prints 10 once — that's the key difference from while.
public class Program { public static void Main() { Console.WriteLine("Examples for the explained code @ DotNetVerse"); int count = 10; do { Console.WriteLine(count); // prints 10 - runs at least once count++; } while (count < 5); } }Examples for the explained code @ DotNetVerse 10
Try above with your scenarios, imagine real-life scenarios and events to implement these once and for all, if you feel like something is out/missing please comment below, YOUR review is appreciated.