Decision-Making Statements

It's time to introduce one of the most important topics in all of programming: decision-making—when to perform one action or another, and how to execute that action, whether it should be in a loop or a condition will indicate if it should be executed.

 

1 - Conditional Statements

These are used when we need to make a decision based on certain logic. To see this visually, imagine driving on a road and the road has a fork; if you take the fork, it’s like entering a conditional statement.

 

1.1 - If -else

The first conditional statement we are going to see is the If-Else statement, which literally means "if-else". Therefore, the conditional statement is linked with a logical operator and is represented as follows:

if (A < B){    Console.WriteLine("A es menor que B");}else{    Console.WriteLine("A no es menor que B");}

If the content within the if statement is a single line, you don't need to use parentheses, but I personally recommend it. This makes it easier to read later.

if (A < B)    Console.WriteLine("A es menor que B");else    Console.WriteLine("A no es menor que B");

Since version 7 of C#, you can create if statements in a single line, known as "one line statements". Be careful when creating these, as you can add as many as you want inside each block, and this practice can make it easy to get lost when reading code. At work, I’ve had several cases where it took longer to figure out what so many nested ifs did than actually fixing the original problem.

A < B ? Console.WriteLine("A es menor que B") :  Console.WriteLine("A no es menor que B");

If you only want to include the option inside the If because "else" will just continue the normal execution flow, you don’t need to add else.

if (A < B){    Console.WriteLine("A es menor que B");}

 

1.2 - Switch-case

Many times we find ourselves writing several IF statements consecutively one after the other. To solve this problem, we have the implementation of switch statements.

A switch statement takes a variable and checks it against each available case. As we see in the example, we write switch(variable) to analyze the variable. It can be of any type, not necessarily a string—it could be a number, a date, etc.

string mes = "enero";switch (mes){    case "enero":        Console.WriteLine("enero tiene 31 dias");        break;    case "febrero":        Console.WriteLine("febrero tiene 28 dias");        break;    default:        Console.WriteLine("mes no encontrado");        break; }

As shown in the example, we check each case and include a break statement at the end, which will exit the loop. For this example, we only used two months, so the statement is small, but if we included all months it would be much larger.

As everyone knows, months (except for February) have 30 or 31 days. Switch statements allow us to group several cases to produce the same output if we want.

string mes = "enero";switch (mes){    case "enero":    case "marzo":    case "mayo":    case "julio":    case "agosto":    case "octubre":    case "diciembre":        Console.WriteLine("enero tiene 31 dias");        break;    case "febrero":        Console.WriteLine("febrero tiene 28 dias");        break;    case "abril":    case "junio":    case "septiembre":    case "noviembre":        Console.WriteLine("febrero tiene 28 dias");    default:        Console.WriteLine("mes no encontrado");        break; }

With C# 8, its functionality has increased. You can take a look here.

 

 

2 - Iterative Statements - Loops

Commonly, when programming, we find ourselves with a list of objects and we need to calculate some value or print different information to the user for all of them (or the majority by applying conditional statements). Instead of handling each case one by one, we use loops so we can iterate over each element in a list.

 

2.1 - For Loop

The for loop is the most implemented loop, used to repeat actions a certain number of times.

for (int i = 0; i < 10 ; i++){    Console.WriteLine("Iteración número "+i);}

As we see, when we declare the for loop, we do so along with:

  • Declaration and initialization of the variable: int i = 0; 
  • Logical expression to decide if it needs to execute: i < 10;
  • Arithmetic operator for incrementing by one: i++;

 

2.2 - While Loop

We evaluate the logical expression inside the parentheses and execute the block if it is true. Once it ends, we re-evaluate the expression, and if it is still valid, we execute the block again.

int contador = 0;while(contador < 10){      Console.WriteLine("Iteración número "+i);      contador++;}

 

2.3 - Do-While Loop

Similar to the previous one, except in this case, the block is executed at least once and then continues executing while the expression is true.

int contador = 0;do{    Console.WriteLine("Iteración número "+contador);    contador++;}while(contador < 10);

One must be very careful when creating loops because it is possible—especially when we are learning—to create infinite loops. If this happens in our application during development, we’ll get an "out of memory" error because the computer will run out of memory; that's it, nothing else happens.

But if it happens in production, it would cause the application to stop working and this would be a much bigger issue.

One way to create an infinite loop is as follows:

int contador = 11;do{    Console.WriteLine("Iteración número "+contador);    contador++;}while(contador > 10);

As we can see, the initial value of the contador variable is greater than 10, so the loop never stops executing. You’d get the same result, for example, if you didn’t include the contador++ statement, as the variable would always have the same value.

 

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café