TOC

This article is currently in the process of being translated into Czech (~36% done).

Control Structures:

The if statement

Jedním z nejdůležitějších výroků v každém programovacím jazyce je příkaz if. Schopnost nastavit podmíněné bloky kódu je základním principem psaní softwaru. Podmínka if je v C# velmi jednoduchá. Pokud jste již použili jiný programovací jazyk, je pravděpodobné, že můžete použít příkaz if v C# použít i z jiného jazyka. V každém případě si přečtěte, jak se používá. Podmínka if vyžaduje booleovský výsledek, tedy true nebo false. V některých programovacích jazycích lze několik datových typů automaticky převést na boolean, ale v C # musíte konkrétně převést výsledek na boolean. Například, pokud (číslo) nemůžete použít, ale můžete porovnat číslo s něčím a generovat true (pravda) nebo false (nepravda), jak to děláme později.

In the previous chapter we looked at variables, so we will expand on one of the examples to see how conditional logic can be used.

using System;

namespace ConsoleApplication1
{
    class Program
    {
static void Main(string[] args)
{
    int number;

    Console.WriteLine("Please enter a number between 0 and 10:");
    number = int.Parse(Console.ReadLine());

    if(number > 10)
Console.WriteLine("Hey! The number should be 10 or less!");
    else
if(number < 0)
    Console.WriteLine("Hey! The number should be 0 or more!");
else
    Console.WriteLine("Good job!");

    Console.ReadLine();
}
    }
}

We use 2 if statements to check if the entered number is between 0 and 10, and a companion of the if statement: The else keyword. Its meaning should be obvious to anyone speaking English - it simply offers an alternative to the code being executed if the condition of the if statement is not met.

As you may have noticed, we don't use the { and } characters to define the conditional blocks of code. The rule is that if a block only contains a single line of code, the block characters are not required. Now, this seems like a lot of lines to simply check a number, doesn't it? It can be done with fewer lines of code, like this:

if((number > 10) || (number < 0))
    Console.WriteLine("Hey! The number should be 0 or more and 10 or less!");
else
    Console.WriteLine("Good job!");

We put each condition in a set of parentheses, and then we use the || operator, which simply means "or", to check if the number is either more than 10 OR less than 0. Another operator you will be using a lot is the AND operator, which is written like this: &&. Could we have used the AND operator instead? Of course, we simply turn it around a bit, like this:

if((number <= 10) && (number >= 0))
    Console.WriteLine("Good job!");
else
    Console.WriteLine("Hey! The number should be 0 or more and 10 or less!");

Toto nám ukazuje pár nových porovnávacích znamének, které znamenají "menší nebo rovno" a "větší než nebo rovno".

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!