Programming Lessons: Let's learn a little of C# - Lesson 04
Hello Guys
Some days ago I started a new topic. I wanna teach how to program a little. As you probably know I'm a software developer so I wanna bring some of the stuff that I do daily. With this topic I do not wanna do something hard to understand but the basic concepts of programming.
We'll start with a simple topics and the language that we'll use is C#.
About C#
C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg. This tutorial covers basic C# programming and various advanced concepts related to C# programming language.
First of all, if you wanna try the examples that I'll provide you can use this site to program online C#. Alternatively, you can install the free version on Visual Studio, the best tool to program C# here.

Lesson 04: Decision Making
When you write a program you need to prepare yourself to take some decisions. Decisions like true or false is a fundamental thing when you program.
"Decision making structures requires the programmer to specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false."
source
The schema to understand the behavior of a structure of decision is: (source)

Code
// Example of a structure if-else in C#
bool condition = true;
// Inside the parenthises the result need to be true or false always!
if (condition)
{
Console.WriteLine("The variable is true");
Console.ReadLine();
}
else
{
Console.WriteLine("The variable is false");
Console.ReadLine();
}
You can find more information here
Console
> The variable is true
Press a key to continue...
Let's give you a more concrete example to you understand better what you can do with it
If you look at the Lesson 02, we talk about the rectangule. Let's imagine that you wanna know if the figure is a rectangule or a square. You only need to do this code:
// Rectangle Class
using System;
namespace RectangleApplication
{
class Rectangle
{
// Variables
double length = 5;
double width = 5;
if(length == width)
{
Console.WriteLine("The figure is a square");
}
else
{
Console.WriteLine("The figure is a rectangle");
}
}
}
This is a very simple example how you can use if-else structure. But, in the end the only purpose of using a if-else struct is to make decisions based on values.
The last lessons are here!
Programming Lessons: Let's learn a little of C# - Lesson 01
Programming Lessons: Let's learn a little of C# - Lesson 02
Programming Lessons: Let's learn a little of C# - Lesson 03
Hey @criptomaster! I just found this series. I've been self-teaching C# lately for a personal project. (Possibly converting a Monty Carlo Sim from Ruby to C#.)
I'm digging into your series, and definitely interested in more to come.
I just wanted to say, "Thanks!"