• How to Make Decision in C#

    by  • December 28, 2015 • Microsoft .NET C# (C-Sharp) • 0 Comments

    Any program that does any real work will have to make decisions. These decisions are based on meeting certain conditions—or rather, running code only , when a particular condition or state is met. The central piece of decision making is a special C# statement called an if-else statement. If-statements are a key part of programming. They let you make decisions about how things are going to work. No programming language is complete without some form of if-statement.

    ifelseimg2ifelseimg3

     

    if condition is mostly used with else statement. The If-Else ladder is a set of statements that is used to test a series of conditions. If the first if statement meet the result then code within the if block executes. If not, control passes to the else statement, which contains a second “if” statement. If second one meet the result then code within the if block executes. This continues as a series of else if statements. A default else code block may execute when no condition has been evaluated to true.

    Lets look at the following code

     

    In the above code, there is supposed to be a lucky draw and the user have to select a box to win a prize. So we asked user to enter the box number. Then there is decision making if-else structure of C# to show the prize won by use based on the selected box number.

    Output is shown below.

    output




    Namecheap.com

    Leave a Reply