C# Decision Making (If, If-Else, If-else-If Ladder, Nested If, Switch, Nested Switch) - GeeksforGeeks
C# Decision Making (If, If-Else, If-else-If Ladder, Nested If, Switch, Nested Switch) - GeeksforGeeks
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
if (condition)
statement 1;
statement 2;
Example:
Csharp
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)
else
Flowchart:
Example:
Csharp
// C# program to illustrate
// if-else statement
using System;
Output:
Geeks
if(condition1)
else if(condition2)
else if(condition3)
...
else
Flowchart:
Example:
Csharp
// C# program to illustrate
// if-else-if ladder
using System;
class GFG {
if (i == 10)
Console.WriteLine("i is
else if (i == 15)
Console.WriteLine("i is
else if (i == 20)
Console.WriteLine("i is
else
Console.WriteLine("i is
}
}
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 {
if (i == 10) {
// Nested - if statement
// Will only be executed
// above it is true
if (i < 12)
Console.WriteLine("i
else
Console.WriteLine("i
}
}
}
Output:
Switch Statement
Switch statement is an alternative to long if-
else-if ladders. The expression is checked for
different cases and the one match is executed.
break statement is used to move out of the
switch. If the break is not used, the control will
flow to all cases below it until break is found or
switch comes to an end. There is default case
(optional) at the end of switch, if none of the
case matches then default case is executed.
Syntax:
switch (expression)
break;
break;
break;
Example:
Csharp
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
switch (j)
{
case 5: Console.WriteLin
switch (j - 1)
{
case 4: Console.
switch (
{
case 3:
}
break;
}
break;
case 10: Console.WriteLi
break;
case 15: Console.WriteLi
break;
default: Console.WriteLi
break;
}
}
}
Output:
CSharp-ControlFlow
Recommended Articles
1. Getting the Total Number of Days in a Month
Using If-else and Switch Statements in C#
2. C# - if else Statement
3. C# | How to use strings in switch statement
4. Switch Statement in C#
5. Switch Expression in C# 8.0
6. Nested Classes in C#
7. C# Program to Check a Specified Type is Nested
or not
8. How to get the Elements of the Nested
ValueTuple in C#?
9. C# Program for Nested Conditional Operator
10. C#- Nested loops
Company
About Us
Legal
Careers
In Media
Contact Us
Advertise with us
Explore
Job-A-Thon Hiring Challenge
Hack-A-Thon
DSA in JAVA/C++
Master CP
Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial
DSA Concepts
Data Structures
Arrays
Strings
Linked List
Algorithms
Searching
Sorting
Mathematical
Dynamic Programming
DSA Roadmaps
DSA for Beginners
Web Development
HTML
CSS
JavaScript
Bootstrap
ReactJS
AngularJS
NodeJS
Express.js
Lodash
Computer Science
GATE CS Notes
Operating Systems
Computer Network
Software Engineering
Engineering Maths
Python
Django Tutorial
Python Projects
Python Tkinter
Pandas Tutorial
NumPy Tutorial
NLP Tutorial
DevOps
Git
AWS
Docker
Kubernetes
Azure
GCP
Competitive
Open InProgramming
App