Friday, September 21, 2012

Beginner in C#: The if statement

The if statement is one of the most important statement in almost all programming languages and in C#.

The main use of it is to specify an action to do but only when a condition of type bool is true.

For example lets make a simple console application that asks the user for an integer number and determines if the number given is an even number or an odd number.

        static void Main(string[] args)
            Console.Write("Enter a number: ");//asking the user to enter a number
            int number = int.Parse(Console.ReadLine());//reading the given number
            if (number % 2 == 0)//if 'number' is a multiple of two
                Console.WriteLine("It's an even number");//Writing the result
            else //this means "if not, do the following.."
                Console.WriteLine("It's an odd number");//Writing the result

If (you have a question)
    Leave a comment and I'll answer!