Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

C# Tutorial

C# HOME C# Intro C# Get Started C# Syntax C# Output C# Comments C# Variables C# Data Types C# Type Casting C# User Input C# Operators C# Math C# Strings C# Booleans C# If...Else C# Switch C# While Loop C# For Loop C# Break/Continue C# Arrays

C# Methods

C# Methods C# Method Parameters C# Method Overloading C# Scope C# Recursion

C# Classes

C# OOP C# Classes/Objects C# Class Members C# Constructors C# this Keyword C# Access Modifiers C# Properties C# Static C# Inheritance C# Polymorphism C# Abstraction C# Interface C# Enums C# Structs

C# Errors

C# Errors C# Debugging C# Exceptions

C# Data Structures

C# Data Structures C# List C# Dictionary C# HashSet C# Stack C# Queue

C# Advanced

C# Files C# Date and Time C# Nullable Types C# Generics C# Lambda C# LINQ

C# Cert

C# Certificate

C# How To

C# How Tos

C# Examples

C# Examples C# Compiler C# Exercises C# Quiz C# Practice Problems C# Server C# Syllabus C# Study Plan


C# Nested If


Share

Nested If

Sometimes you only want to check a condition when another condition is already true.

Think about how you log in to a website. First, the website checks your username. It only checks your password if the username is correct.

In C#, you can do this by placing an if statement inside another if statement. This is called a nested if:

Syntax

if (condition1)
{
  // code to run if condition1 is true

  if (condition2)
  {
    // code to run if condition1 and condition2 are both true
  }
}

The inner if is only checked when the outer condition is true.


A Simple Example

In this example, we first check if x is greater than 10. If it is, we also check if y is greater than 20:

Example

int x = 15;
int y = 25;

if (x > 10)
{
  Console.WriteLine("x is greater than 10");

  if (y > 20)
  {
    Console.WriteLine("y is also greater than 20");
  }
}

Try it Yourself »

Example explained

x is 15, so x > 10 is true. The program prints the first message, and then it checks the inner if.

y is 25, so y > 20 is also true, and the program prints the second message.

If x was 5, the outer condition would be false. Then C# skips the whole block, and it never checks y.



Nested If with else

You can add an else to both the outer and the inner if. Each else runs when its own condition is false.

This example checks the username first, and then the password. The == operator also works with text (strings):

Example

string username = "Liam";
string password = "abc123";

if (username == "Liam")
{
  if (password == "abc123")
  {
    Console.WriteLine("Welcome, Liam!");
  }
  else
  {
    Console.WriteLine("Wrong password.");
  }
}
else
{
  Console.WriteLine("Unknown user.");
}

Try it Yourself »

Both the username and the password are correct, so the program prints "Welcome, Liam!".

Try to change the values in the editor:

  • With a wrong password, the inner else runs and prints "Wrong password."
  • With a wrong username, the outer else runs and prints "Unknown user."

When to Use a Nested If

A nested if is useful when the second check only makes sense after the first one, and each step needs its own message, like "Unknown user." and "Wrong password." above.

You can place an if inside an if inside another if, but each new level makes the code harder to read. Try to keep it to two levels.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.