Computer Programming – Decision Statements

Decision Statements

In real life, we make decisions all the time—if it rains, take an umbrella; otherwise, go without it.
Similarly, computer programs also need to make decisions based on certain conditions. Decision statements (also called conditional statements) allow a program to choose different actions depending on the situation.

These statements help programs behave intelligently by checking conditions and executing the appropriate block of code.


What Are Decision Statements?

Decision statements evaluate a condition and execute specific code only when that condition is true.
A condition typically involves relational or logical operators.

Example condition:

age >= 18

If this condition is true, the program may allow the user to vote; otherwise, it may show a message saying they are not eligible.


Types of Decision Statements

Most programming languages, including C, Java, and Python, use the following decision-making constructs:

decision statements, conditional statements, if else programming, switch statement, elif Python, decision making in programming, C if statement, Java conditions, Python conditions, programming basics

If Statement

Runs a block of code only if the condition is true.

Example in C

if (age >= 18) {
    printf("You are eligible to vote.");
}

Example in Java

if (age >= 18) {
    System.out.println("You are eligible to vote.");
}

Example in Python

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

If…Else Statement

Provides an alternative block of code when the condition is false.

Example

if (condition) {
    // runs if true
} else {
    // runs if false
}

Python

if age >= 18:
    print("Adult")
else:
    print("Minor")

Else If / Elif Ladder

Used when multiple conditions need to be checked in sequence.

C Example

if (marks >= 90) {
    printf("Grade A");
} else if (marks >= 75) {
    printf("Grade B");
} else {
    printf("Grade C");
}

Python (elif)

if marks >= 90:
    print("Grade A")
elif marks >= 75:
    print("Grade B")
else:
    print("Grade C")

Nested If

Placing one if statement inside another.

if (age >= 18) {
    if (citizen == true) {
        System.out.println("Eligible to vote");
    }
}

Switch Statement (C and Java Only)

Switch executes code blocks based on matching values. Python uses match (from Python 3.10+).

C Example

switch(day) {
    case 1: printf("Monday"); break;
    case 2: printf("Tuesday"); break;
    default: printf("Invalid day");
}

Python Match

match day:
    case 1: print("Monday")
    case 2: print("Tuesday")
    case _: print("Invalid day")

Why Decision Statements Matter

You use decision statements when your program must:

  • Check conditions
  • Validate user input
  • Control the flow of execution
  • Handle different outcomes
  • Make logical choices

They are fundamental to writing interactive and intelligent programs.

FAQ

What is a decision statement in programming?

A decision statement allows a program to choose different actions based on conditions.

What is the difference between if and else?

if executes when the condition is true; else runs when it is false.

Does Python have a switch statement?

Older versions do not, but Python 3.10+ introduced match (similar to switch).

Can we use multiple conditions?

Yes, using else if (C/Java) or elif (Python).

What is a nested if?

A nested if is an if statement placed inside another if, used for checking multiple related conditions.

Related Article
Computer Programming Tutorial for Beginners – Learn Coding Step by Step (C, Python, Java)

Computer Programming Tutorial for Beginners – Learn Coding Step by Step Computer programming is the process of writing a series Read more

Computer Programming Overview

Computer Programming Overview Computer programming begins with understanding what a computer program is and how it operates. A computer program Read more

Computer Programming Basics

Computer Programming – Basics Understanding computer programming begins with learning the foundational building blocks that every programming language is built Read more

Computer Programming Environment

Computer Programming – Environment Before writing your first computer program, you must prepare the correct programming environment. Although the environment Read more

Computer Programming – Basic Syntax

Before diving into advanced programming concepts, it’s important to understand how basic syntax works in different Computer Programming languages. Syntax Read more

Computer Programming – Data Types

Data types are one of the most important concepts in programming. A data type defines the kind of data a Read more

Computer Programming Variables

In any programming language, a variable is one of the most fundamental concepts. A variable acts as a named storage Read more

Computer Programming – Keywords

In earlier chapters, you learned two essential programming concepts: variables and data types. You also saw how different data types Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments