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.
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
static void Main(string args)
Console.WriteLine(“Please write the box# 1,2 or 3 to win a prize,press enter ");
string prize = "";
string userValue = Console.ReadLine();
if (userValue == "1")
prize = "You won BMW";
else if (userValue == "2")
prize = "You won a Luxuary Flat";
else if (userValue == "3")
prize = "You won a Luxuary Helicoptor";
prize = "Sorry,,... You Lost";
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.Best WordPress Themes and Plugins with Great Team and Support!