As we saw in If statement only execute the code if the condition is true, but if the condition is not true then what to do?
For this purpose we have C# if-else Statement.
An if-else statement in C# is used to tells the code what to do next when the if condition is not true (false).
The if… else construct is used to determine the program’s flow based on the value of the returned expression. It evaluates the comparison operator and then executes the statements based on the value. For example, if you want to run a piece of code when the prerequisites are met, you can use the if… else construct to select which code will be run. When no if condition matches, the default condition is executed. The following example will demonstrate how to use if… else constructions.
Syntax:
Graphical representation: C# if-else Statement
// if-else statement in C# using System; public class GFG { public static void Main(string[] args) { string value = "700"; if (value == "700") { Console.WriteLine("Right Value"); } else { Console.WriteLine("Wrong Value"); } } }