0% found this document useful (0 votes)
154 views

C# Decision Making (If, If-Else, If-else-If Ladder, Nested If, Switch, Nested Switch) - GeeksforGeeks

The document discusses different conditional statements in C# including if, if-else, if-else-if ladder, nested if, switch, and nested switch. Syntax and examples are provided for each statement to explain how they work and how to write them in code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

C# Decision Making (If, If-Else, If-else-If Ladder, Nested If, Switch, Nested Switch) - GeeksforGeeks

The document discusses different conditional statements in C# including if, if-else, if-else-if ladder, nested if, switch, and nested switch. Syntax and examples are provided for each statement to explain how they work and how to write them in code.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

GEEKSFORGEEKS

C# Decision Making (if, if-else, if-


else-if ladder, nested if, switch,
nested switch)
Decision Making in programming is similar to
decision making in real life. In programming too,
a certain block of code needs to be executed
when some condition is fulfilled.
A programming language uses control
statements to control the flow of execution of
program based on certain conditions. These are
used to cause the flow of execution to advance
and branch based on changes to the state of a
program.
The conditional statements of C#:

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

// C# program to illustrate if state


using System;

public class GFG {

public static void Main(string[]


{
string name = "Geek";
if (name == "Geek") {
Console.WriteLine("Geeks
}
}
}

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;

public class GFG {

public static void Main(string[]


{
string name = "Geek";
if (name == "Geeks") {
Console.WriteLine("Geeks
}
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 conditio

else if(condition2)

// code to be executed if conditio

else if(condition3)

// code to be executed if conditio

...

else

// code to be executed if all the

Flowchart:

Example:

Csharp

// C# program to illustrate
// if-else-if ladder
using System;

class GFG {

public static void Main(String[]


{
int i = 20;

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 {

public static void Main(String[]


{
int i = 10;

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:

i is smaller than 12 too

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)

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

// C# example for switch case


using System;

public class GFG


{
public static void Main(String[]
{
int number = 30;
switch(number)
{
case 10: Console.WriteLine("
break;
case 20: Console.WriteLine("
break;
case 30: Console.WriteLine("
break;
default: Console.WriteLine("
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

// C# example for nested switch case


using System;

public class GFG


{
public static void Main(String[]
{
int j = 5;

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:

Article Tags : C# CSharp-Basics

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

Read Full Article

A-143, 9th Floor, Sovereign Corporate


Tower, Sector-136, Noida, Uttar Pradesh -
201305

[email protected]

Company
About Us

Legal

Careers

In Media

Contact Us

Advertise with us

Campus Training Program

Explore
Job-A-Thon Hiring Challenge

Hack-A-Thon

GfG Weekly Contest

Offline Classes (Delhi/NCR)

DSA in JAVA/C++

Master System Design

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

Basic DSA Coding Problems

DSA Roadmap by Sandeep Jain

DSA with JavaScript

Top 100 DSA Interview Problems

All Cheat Sheets

Web Development

HTML

CSS

JavaScript

Bootstrap

ReactJS

AngularJS

NodeJS

Express.js

Lodash

Computer Science

GATE CS Notes

Operating Systems

Computer Network

Database Management System

Software Engineering

Digital Logic Design

Engineering Maths

Python

Python Programming Examples

Django Tutorial

Python Projects

Python Tkinter

OpenCV Python Tutorial

Python Interview Question

Data Science & ML


Data Science With Python

Data Science For Beginner

Machine Learning Tutorial

Maths For Machine Learning

Pandas Tutorial

NumPy Tutorial

NLP Tutorial

Deep Learning Tutorial

DevOps

Git

AWS

Docker

Kubernetes

Azure

GCP

Competitive
Open InProgramming
App

You might also like