Skip to content

Learn & Explore

Learn practical technology, programming, IT infrastructure, cybersecurity and real-world experiences.

  • Home
  • Learn
    • IT Fundamentals
      • What Is Information Technology?
      • History and Evolution of Computer Technology
      • Components of an Information Technology System
    • Java
      • Introduction to Java: What It Is & Why Learn It
    • Programming
    • Web Development
    • Networking
    • Cyber Security
    • Database
    • Roadmaps
      • Java Roadmap
  • Resources
    • Technology Pioneers
      • Charles Babbage
      • Ada Lovelace
      • Alan Turing
      • Grace Hopper
      • Tim Berners-Lee
      • Dennis Ritchie
      • Linus Torvalds
    • IT Degrees in Nepal
      • BITM
      • BSc CSIT(TU)
    • Universities
      • Tribhuvan University
      • Kathmandu University
      • Pokhara University
      • Purbanchal University
      • Mid-West University
      • Far Western University
      • Nepal Open University
    • Quotes
  • About
    • Contact
    • Privacy Policy
    • Terms and Conditions
  • Home
  • Programming
  • Decisions in C#

Decisions in C#

Posted on February 19, 2024February 19, 2024 By macharaja No Comments on Decisions in C#
C#, Programming

In C#, decisions or conditional statements are used to execute different blocks of code based on certain conditions. These conditions evaluate to either true or false, and depending on the result, specific code blocks are executed. Here are the primary conditional statements in C#:

if Statement: The if statement is the most basic conditional statement. It executes a block of code if a specified condition is true.

int x = 10;
if (x > 5)
{
    // This block will execute if x is greater than 5
    Console.WriteLine("x is greater than 5");
}

if-else Statement: The if-else statement allows us to execute one block of code if the condition is true and another block if it’s false.

int x = 3;
if (x > 5)
{
    // This block will execute if x is greater than 5
    Console.WriteLine("x is greater than 5");
}
else
{
    // This block will execute if x is not greater than 5
    Console.WriteLine("x is not greater than 5");
}

else-if Statement: We can use the else-if statement to check multiple conditions one by one and execute the block of code associated with the first true condition.

int x = 3;
if (x > 5)
{
    Console.WriteLine("x is greater than 5");
}
else if (x < 5)
{
    Console.WriteLine("x is less than 5");
}
else
{
    Console.WriteLine("x is equal to 5");
}

switch Statement: The switch statement is useful when we have multiple conditions to check against a single variable.

int day = 3;
switch (day)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    case 3:
        Console.WriteLine("Wednesday");
        break;
    default:
        Console.WriteLine("Other day");
        break;
}

In summary, conditional statements allow us to control the flow of our program based on different conditions, making our code more dynamic and flexible.

Tags: Decisions in C# If else

Post navigation

❮ Previous Post: Loops in C#
Next Post: Arrays in C# ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • IT Career Guide for College Students in Nepal: A Complete Roadmap to Success
  • Procedural Programming vs Object-Oriented Programming (POP vs OOP)
  • What is Programming? A Beginner’s Complete Guide
  • Introduction to Object-Oriented Programming (OOP): Concepts, Features, and Benefits
  • Difference Between C and C++

Categories

  • C & C++
  • C#
  • Course
  • CSS
  • Cyber Security
  • Database
  • HTML
  • IT Career
  • IT Degrees
  • IT Degrees in Nepal
  • IT Fundamentals
  • Java
  • JavaScript
  • Microsoft SQL Server
  • My Learnings
  • MySQL
  • Oracle
  • PHP
  • Programming
  • Quotes
  • Technology
  • Technology Pioneers
  • Universities
  • Web Development
  • Web Hosting

Copyright ©2026