General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Control structures
  3. Selection (Conditional)

If/Else

If/Else Statement in Computer Programming

In computer programming, the if/else statement is a fundamental control flow structure that allows a program to execute certain blocks of code based on whether a specified condition is true or false. If the condition evaluates to true, the block of code inside the if part is executed. If the condition is false, the code inside the else part is executed instead. This allows for decision-making processes within the code.

Example in Python

age = 18
if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

This Python example checks if the age variable is greater than or equal to 18. If true, it prints that you are eligible to vote. Otherwise, it prints that you are not eligible to vote.

Example in C#

int number = 10;
if (number > 0) {
    Console.WriteLine("The number is positive.");
} else {
    Console.WriteLine("The number is not positive.");
}

In this C# example, the program checks if the number variable is greater than 0. If the condition is true, it prints that the number is positive. If false, it prints that the number is not positive.

ElseIf Statement

While if/else structures are essential for basic decision-making, the elseif (or else if) clause introduces more nuanced control. It allows you to specify an additional condition(s) to test if the initial if condition is false. The elseif block is executed only if its condition is true and all preceding if or elseif conditions were false. This is particularly useful for checking multiple conditions sequentially.

Example in Python

age = 20
if age < 18:
    print("You are a minor.")
elif age < 65:
    print("You are an adult.")
else:
    print("You are a senior.")

This Python example determines whether someone is a minor, an adult, or a senior based on their age. The elif clause is used here to add the additional condition for being an adult.

Example in C#

int score = 75;
if (score < 50) {
    Console.WriteLine("Fail");
} else if (score < 75) {
    Console.WriteLine("Good");
} else {
    Console.WriteLine("Excellent");
}

In this C# example, the program categorizes a score as "Fail," "Good," or "Excellent." It uses else if to add the condition for scoring 'Good.'

PreviousSelection (Conditional)NextTernary

Last updated 1 year ago

Was this helpful?