The if statement in each programming language is one of the most important statements, being able to establish conditional blocks of code is a fundamental principal of writing software. In C #, it is very easy to use the statement. If you have already used any other programming language, it is possible that you can use C # to use C #. In any case, read to see how it is used. If the statement requires a Boolean result, then it is true or false. In some programming languages, many datataps can be automatically converted to boolean, but in C #, you must create results for Boolean in particular needed. For example, if you can not use (numbers), but you can compare the number for something, so that it is true or false, as we do later.
In the previous chapter we saw the variable, so we will see how to use conditional logic to extend one of the examples.
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();
}
}
}
If we use 2, the number given to check the statements is between 0 and 10, and if the partner of the statement: The else keyword means that someone should speak English - if only then the statement If the situation is not complete, then it only provides the option of code.
As you can see, we do not use characters {and} to define the conditional blocks of the code. The rule is that if there is only one line of code in the block, the characters of the block are not required. Now, it looks like a lot of lines to check a number, is not it? This code can be done with a few lines, such as:
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 the set of brackets, and then we use it. Operator, which means "or", to check whether the number is greater than or less than 10 or not. You will use another operator and operator, which is written as follows: && We can use the end operator instead? Of course, we can turn it a little bit, like:
if((number <= 10) && (number >= 0))
Console.WriteLine("Good job!");
else
Console.WriteLine("Hey! The number should be 0 or more and 10 or less!");
This introduces a couple of new operators, the "less than or equal too" and the "bigger than or equal too".