C# - The if statement
Now we will learn the most important statement.
One of the single most important statements in every programming language is the if statement. Being able to set up conditional blocks of code is a fundamental principal of writing software. In C#, the if statement is very simple to use. If you have already used another programming language, chances are that you can use the if statement of C# straight away. In any case, read on to see how it's used. The if statement needs a boolean result, that is, true or false. In some programming languages, several datatypes can be automatically converted into booleans, but in C#, you have to specifically make the result boolean. For instance, you can't use if(number), but you can compare number to something, to generate a true or false, like we do later on.
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 = 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".
-
How to Make Your Own Miracle? Express Simple Gratitude Attitude.
| By Bruno_Vigneault | in Motivational
The attitude of expressing simple gratitude to the Divine is an important step to make your own miracle happen, to ...
-
How to Use the Regular Expressions in ASP.NET
| By 5min | in Software
Learn how use Regular Expressions to specify Input patterns....
-
How to Remove Projects from Visual Studio
| By 5min | in General
Visual Studio Remove Projects. How to remove projects from Visual Studio...
-
Microsoft Expressions Web - Understanding the Limitations of Expression Web
| By 5min | in Software
Learn about some of the limitations of Expressions Web and some of the most advanced and complex options...
-
Microsoft Expression Design - Introduction to Microsoft Expression Design
| By 5min | in Software
Introduction to Microsoft Expression Design software and Video Series and topics covered in this tutorial...
-
XA Transaction - Solution for Transaction More Than One Database | By H4d1 | in Programming
Have you ever think that it's too difficult for making database transaction in two different places (or databases) ...
-
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , is integer , check Is Zero , Get Que | By xxris | in Programming
Javascript functions for : trim, right trim, left trim, no Apostrophe, is Empty , is Digit , VarChar To Number , i...
-
How to access and use a Window's command line | By MaxwellPayne | in Programming
Learn about the Window's command line in DOS and how to use it....
-
How to Learn to Program Your Computer | By dsj8760 | in Programming
This article is about learning to program a computer. It is a general article giving tips on how to learn about pro...
-
Jailbroken iPhones get RickRolled | By explorer | in Programming
First iPhone worm, attacks via SSH and does the classic rick roll gag on the user....
-
C# - Looping in statements | By Victorius | in Programming
We must learn how we can loop in statements. Now here we go....
-
C# - The switch statement | By Victorius | in Programming
Another important statement is the switch statement. Now we will learn it :)...
-
C# - Variables in higher level | By Victorius | in Programming
In this article i will teach you how you can use variables in most of your program....
-
C# - Variable types | By Victorius | in Programming
I will teach you what kind of variables can you use in C#...
-
C# - Hello world explained | By Victorius | in Programming
I will show you what's the life after Hello Word :)...








No comments yet.