How do I use if-else statement?

Category: Introduction, viewed: 144 time(s).

The example below shows you how to use the if-else statement for checking a condition. The if block will be executed when the boolean expression passed resulted in a true value otherwise the else block will be executed.

using System;

namespace Kodecsharp.Example.Intro
{
    class IfElseDemo
    {
        public static void Main(string[] args)
        {
            int a = 10;
            int b = 25;

            //
            // A simple if-else block
            //
            if (a <= b)
            {
                Console.WriteLine("a is less than or equals to b");
            }
            else
            {
                Console.WriteLine("b is greater than a");
            }

            //
            // An if-else if-else block
            //
            if (a * 4 < b)
            {
                Console.WriteLine("a * 4 is less than b");
            }
            else if (a * 5 - 8 < b)
            {
                Console.WriteLine("a * 5 - 8 is less that b");
            }
            else
            {
                Console.WriteLine("a = " + a + "; b = " + b);
            }

            Console.ReadLine();
        }
    }
}

Our program result:

a is less than or equals to b
a = 10; b = 25
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Our Friends

Network Sites

Statistics

Locations of visitors to this page