Design perfect website

We made it for You!

Design perfect website

We made it for You!
Conditional statement if-else C#
Blog > C# > Conditional statement if-else C#

Conditional statement if-else C#

PL

Conditional statement if-else C#


The conditional instruction (Conditional statement if-else C#) in programming is used to determine the conditions that must be met for a given event to take place. There are many examples of such a necessity: checking the user’s age, connection status, or group division due to the value of the selected parameter.



Syntax of the if condition


The structure of the conditional statement is as follows:


if(warunek_logiczny)
{
 // code that will be executed when the logic condition is met
}

In case the code provided is one instruction, the curly bracket is not necessary.


The logical condition returns true or false and can be written in parentheses at if or can be checked in advance. The following examples show the possible applications of conditions.


int i = 5; // int type variable
if(i > 2) // check if 5 is greater than 2
{
   // the code will be executed
}


bool sprawdz = 5 < 2; // a bool variable to which it will go
// the result of checking if 5 is less than 2, or false
if(sprawdz) // the check variable is false, so the code provided
if
// will not be executed. The entry is equivalent to "if(sprawdz == true)"
{
   // the code will NOT be executed
}


if(true) // no condition is checked, the value is always true,
// the code will always be executed
{
    // the code will be executed
}


int i = 5; // int type variable
if(i >= 1 && i <= 10)
// check if 5 is in the range
// numbers <1, 10>
{
    // the code will be executed
}

Nested instructions


If it is necessary to meet a greater number of conditions for an event, all of them must be written out. It can be done in the same way as in the previous example, that is, giving more conditions in the if bracket, which is a faster and more effective solution. There is sometimes a need to check several conditions, once one has been met – nested instructions are used for this.


// We want to display the best fitted dress for a woman's beauty

string plec, kolor_wlosow, karnacja;

if(plec == "kobieta") // The first condition allows you to choose from users
// only women
{
   // The conditions in this buckle will always apply only to women

   if(kolor_wlosow == "blond") // The selection condition among women these
   // with blond hair
      Console.WriteLine("sukienka czerwona");

   if(karnacja == "jasna") // The condition that chooses among women
   // with light complexion
      Console.WriteLine("sukienka niebieska");
}

The else keyword


Using the conditional statement if we execute the code when the given condition is met. However, there is a situation where we are also interested in the information that the condition has not been met and the number of possible other cases is high. The solution to this issue is the else keyword. It refers to one if condition and is executed when the if statement has not been executed. In else, you should not try to enter conditions because it only works if and if. In case the else code is one command, the use of curly brackets is also not necessary.


string kolor = "czerwony";

if(kolor == "czarny") //the condition was not met
{
   // the code will not be executed
}
else
{
   // the code will be executed
}

A practical example – Conditional statement if-else C#


Checking the user’s age range and executing the code for selected age ranges.


if(wiek >= 18) // Checking if we are an adult
{
   if(wiek >= 30 && wiek <=40)
   {
      Console.WriteLine("Masz między 30, a 40 lat");
      // Further code for people between 30 and 40
   }
   if(wiek >= 50)
   {
      Console.WriteLine("Ukończyłeś 50'kę");
      // Further code for people from the 50+ range
   }
}
else
{
   Console.WriteLine("Jesteś niepełnoletni");
   // Program code for underage people
}
TOP