C# Decision Making Conditional and Looping Construct
C# Decision Making Conditional and Looping Construct
if
if-else
if-else-if
Nested if
Switch
Nested switch
IF Statement
The if statement checks the given condition. If the condition
evaluates to be true then the block of code/statements will execute
otherwise not.
Syntax:
if(condition)
{
//code to be executed
}
Note: If the curly brackets { } are not used with if statements then
the statement just next to it is only considered associated with the if
statement.
Example:
if (condition)
statement 1;
statement 2;
In this example, only statement 1 is considered to be associated
with the if statement.
Flowchart:
Example:
Csharp
using System;
if (name == "Geek") {
Console.WriteLine("GeeksForGeeks");
Output:
GeeksForGeeks
IF – else Statement
The if statement evaluates the code if the condition is true but what
if the condition is not true, here comes the else statement. It tells
the code what to do when the if condition is false.
Syntax:
if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}
Flowchart:
Example:
Csharp
// C# program to illustrate
// if-else statement
using System;
if (name == "Geeks") {
Console.WriteLine("GeeksForGeeksr");
else {
Console.WriteLine("Geeks");
Output:
Geeks
If – else – if ladder Statement
The if-else-if ladder statement executes one condition from multiple
statements. The execution starts from top and checked for each if
condition. The statement of if block will be executed which
evaluates to be true. If none of the if condition evaluates to be true
then the last else block is evaluated.
Syntax:
if(condition1)
{
// code to be executed if condition1 is true
}
else if(condition2)
{
// code to be executed if condition2 is true
}
else if(condition3)
{
// code to be executed if condition3 is true
}
...
else
{
// code to be executed if all the conditions are
false
}
Flowchart:
Example:
Csharp
// C# program to illustrate
// if-else-if ladder
using System;
class GFG {
int i = 20;
if (i == 10)
Console.WriteLine("i is 10");
else if (i == 15)
Console.WriteLine("i is 15");
else if (i == 20)
Console.WriteLine("i is 20");
else
Output:
i is 20
Nested – If Statement
if statement inside an if statement is known as nested if. if
statement in this case is the target of another if or else statement.
When more than one condition needs to be true and one of the
condition is the sub-condition of parent condition, nested if can be
used.
Syntax:
if (condition1)
{
// code to be executed
// if condition2 is true
if (condition2)
{
// code to be executed
// if condition2 is true
}
}
Flowchart:
Example:
csharp
// C# program to illustrate
// nested-if statement
using System;
class GFG {
int i = 10;
if (i == 10) {
// Nested - if statement
// above it is true
if (i < 12)
Console.WriteLine("i is smaller than 12 too");
else
Output:
switch (expression)
{
case value1: // statement sequence
break;
case value2: // statement sequence
break;
.
.
.
case valueN: // statement sequence
break;
default: // default statement sequence
}
Flow Diagram of Switch – case :
Example:
Csharp
using System;
switch(number)
break;
break;
break;
Output:
case 30
Nested switch
Nested Switch case are allowed in C# . In this case, switch is
present inside other switch case. Inner switch is present in one of
the cases in parent switch.
Example:
Csharp
using System;
int j = 5;
switch (j)
case 5: Console.WriteLine(5);
switch (j - 1)
case 4: Console.WriteLine(4);
switch (j - 2)
case 3: Console.WriteLine(3);
break;
break;
break;
break;
default: Console.WriteLine(100);
break;
Output:
5
4
3
Switch Statement in C#
Last Updated : 22 Apr, 2020
// C# program to illustrate
using System;
// Main Method
int nitem = 5;
switch (nitem) {
case 1:
Console.WriteLine("case 1");
break;
case 5:
Console.WriteLine("case 5");
break;
case 9:
Console.WriteLine("case 9");
break;
default:
break;
Output:
case 5
Why do we use Switch Statements instead of if-else statements?
We use a switch statement instead of if-else statements because if-
else statement only works for a small number of logical evaluations
of a value. If you use if-else statement for a larger number of
possible conditions then, it takes more time to write and also
become difficult to read.
Example: Using if-else-if statement
// C# program to illustrate
// if-else statement
using System;
class GFG {
// Main Method
string topic;
string category;
(String.Compare(topic, "Variables") == 0) ||
category = "Basic";
}
// using compare function of string class
(String.Compare(topic, "Inheritance") == 0) ||
else
Output:
Category is OOPS Concept
Explanation: As shown in the above program the code is not
excessive but, it looks complicated to read and took more time to
write. So we use a switch statement to save time and write
optimized code. Using switch statement will provide a better
readability of code.
Example: Using Switch Statement
// C# program to illustrate
// switch statement
using System;
class GFG {
// Main Method
string topic;
string category;
topic = "Inheritance";
// using switch Statement
switch(topic)
case "Variables":
category = "Basic";
break;
case "Loops":
case"If Statements":
case"Jump Statements":
break;
case "Constructors":
break;
// default case
default:
break;
Output:
Category is OOPS Concept
Using goto in the Switch Statement
You can also use goto statement in place of the break in the switch
statement. Generally, we use a break statement to exit from the
switch statement. But in some situations, the default statement is
required to be executed, so we use the goto statement. It allows
executing default condition in the switch statement. The goto
statement is also used to jump to a labeled location in C# program.
Example:
using System;
// Main Method
int greeting = 2;
switch (greeting) {
case 1:
Console.WriteLine("Hello");
goto default;
case 2:
Console.WriteLine("Bonjour");
goto case 3;
case 3:
Console.WriteLine("Namaste");
goto default;
default:
break;
Output:
Bonjour
Namaste
Entered value is: 2
Explanation: In the above program, the goto statement is used in a
switch statement. Here first the case 2, i.e Bonjour is printed
because the switch contains the value of greeting is 2, then the
control transfers to the case 3 due to the presence of goto
statement, so it prints Namaste and in last the control transfer to
the default condition and print Entered value is: 2.
Note: You can also use continue in place of a break in switch
statement if your switch statement is a part of a loop, then continue
statement will cause execution to return instantly to the starting of
the loop.