Conditional Statements in Programming

Last Updated : 28 Mar, 2026

Conditional statements help a program make decisions. They check whether a condition is true or false and execute different blocks of code based on the result. This allows programs to behave differently in different situations.


If Conditional Statement

The if statement checks a condition and executes a block of code only when the condition is true.

C++
using namespace std;

int main()
{
    int x = 7;
    if (x > 0)
    {
        cout << "x is positive";
    }
    return 0;
}
C
#include <stdio.h>

int main()
{

    int x = 7;
    if (x > 0)
    {
        printf("x is positive");
    }
    return 0;
}
Java
public class GFG {
    public static void main(String[] args)
    {

        int x = 7;
        if (x > 0) {
            System.out.println("x is positive");
        }
    }
}
Python
x = 7

if x > 0:
    print("x is positive")
C#
using System;

class GFG {
    static void Main()
    {

        int x = 7;
        if (x > 0) {
            Console.WriteLine("x is positive");
        }
    }
}
JavaScript
let x = 7;

if (x > 0) {
    console.log("x is positive");
}

Output
x is positive

If-Else Conditional Statement

The if-else statement checks a condition and runs one block of code if the condition is true, and another block of code if the condition is false.

Flowchart of If-Else Statement

flowchart_of_if_else_in_c C++
using namespace std;

int main()
{
    int x = 7;

    if (x > 0)
    {
        cout << "x is positive";
    }
    else
    {
        cout << "x is not positive";
    }
    return 0;
}
C
#include <stdio.h>

int main()
{
    int x = 7;

    if (x > 0)
    {
        printf("x is positive");
    }
    else
    {
        printf("x is not positive");
    }
    return 0;
}
Java
public class GFG {
    public static void main(String[] args)
    {

        int x = 7;
        if (x > 0) {
            System.out.println("x is positive");
        }
        else {
            System.out.println("x is not positive");
        }
    }
}
Python
x = -5

if x > 0:
    print("x is positive")
else:
    print("x is not positive")
C#
using System;

class GFG {
    static void Main()
    {
        int x = 7;

        if (x > 0) {
            Console.WriteLine("x is positive");
        }
        else {
            Console.WriteLine("x is not positive");
        }
    }
}
JavaScript
let x = 7;

if (x > 0) {
    console.log("x is positive");
}
else {
    console.log("x is not positive");
}

Output
x is positive

if-Else if Conditional Statement

The if-else if statement is used to check multiple conditions. The program evaluates each condition one by one and executes the block of code for the first condition that is true.

Flowchart of If-Else if Statement


statements_1


C++
using namespace std;

int main()
{
    int x = 0;

    if (x > 0)
    {
        cout << "x is positive";
    }
    else if (x < 0)
    {
        cout << "x is negative";
    }
    else
    {
        cout << "x is zero";
    }
    return 0;
}
C
#include <stdio.h>

int main()
{
    int x = 0;

    if (x > 0)
    {
        printf("x is positive");
    }
    else if (x)
    {
        printf("x is negative");
    }
    else
    {
        printf("x is zero");
    }

    return 0;
}
Java
public class GFG {
    public static void main(String[] args)
    {
        int x = 0;

        if (x > 0) {
            System.out.println("x is positive");
        }
        else if (x < 0) {
            System.out.println("x is negative");
        }
        else {
            System.out.println("x is zero");
        }
    }
}
Python
x = 0

if x > 0:
    print("x is positive")
elif x < 0:
    print("x is negative")
else:
    print("x is zero")
C#
using System;

class GFG {
    static void Main()
    {
        int x = 0;

        if (x > 0) {
            Console.WriteLine("x is positive");
        }
        else if (x < 0) {
            Console.WriteLine("x is negative");
        }
        else {
            Console.WriteLine("x is zero");
        }
    }
}
JavaScript
let x = 0;

if (x > 0) {
    console.log("x is positive");
}
else if (x < 0) {
    console.log("x is negative");
}
else {
    console.log("x is zero");
}

Output
x is positive

Switch Conditional Statement

The switch statement checks a variable against multiple possible values. Each option is written as a case, and the program executes the matching case. A break statement is usually used to stop execution after a case runs.

Flowchart of Switch Statement

switch-case-in-c

Rules of the Switch Statement

When using switch statements, there are a few important rules.

  • Case values must be constant values.
  • Duplicate case values are not allowed.
  • A switch statement can contain multiple case blocks.
  • The break statement is optional, but recommended to prevent fall-through.
  • The default case is optional but useful for handling unexpected values
C++
using namespace std;

int main()
{
    int day = 2;

    switch (day)
    {
    case 1:
        cout << "Monday" << endl;
        break;
    case 2:
        cout << "Tuesday" << endl;
        break;
    case 3:
        cout << "Wednesday" << endl;
        break;

    default:
        cout << "Invalid day!" << endl;
    }

    return 0;
}
C
#include <stdio.h>

int main() {
    int day = 2;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        default:
            printf("Invalid day!\n");
    }

    return 0;
}
Java
public class GFG {
    public static void main(String[] args) {
        int day = 2;

        switch (day) {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            default:
                System.out.println("Invalid day!");
        }
    }
}
Python
day = 3

match day:
    case 1:
        print("Monday")
    case 2:
        print("Tuesday")
    case 3:
        print("Wednesday")
    case _:
        print("Invalid day!")
C#
using System;

class GFG {
    static void Main() {
        int day = 2;

        switch (day) {
            case 1:
                Console.WriteLine("Monday");
                break;
            case 2:
                Console.WriteLine("Tuesday");
                break;
            case 3:
                Console.WriteLine("Wednesday");
                break;
            default:
                Console.WriteLine("Invalid day!");
                break;
        }
    }
}
JavaScript
let day = 2;

switch(day) {
    case 1:
        console.log("Monday");
        break;
    case 2:
        console.log("Tuesday");
        break;
    case 3:
        console.log("Wednesday");
        break;
    default:
        console.log("Invalid day!");
}

Key Considerations for Switch Case Statements

1. Constant Expression: A switch expression must evaluate to a constant value. This can include constants or arithmetic operations.

C++
using namespace std;

int main()
{
    const int x = 10;
    const int y = 5;

    switch (x + y)
    {
    case 15:
        cout << "Result is 15." << endl;
        break;
    case 20:
        cout << "Result is 20." << endl;
        break;
    default:
        cout << "No match found." << endl;
    }

    return 0;
}
C
#include <stdio.h>

int main()
{
    const int x = 10;
    const int y = 5;

    switch (x + y)
    {
    case 15:
        printf("Result is 15.\n");
        break;
    case 20:
        printf("Result is 20.\n");
        break;
    default:
        printf("No match found.\n");
    }

    return 0;
}
Java
public class GFG {
    public static void main(String[] args)
    {
        final int x = 10;
        final int y = 5;

        switch (x + y) {
        case 15:
            System.out.println("Result is 15.");
            break;
        case 20:
            System.out.println("Result is 20.");
            break;
        default:
            System.out.println("No match found.");
        }
    }
}
Python
x = 10
y = 5

match x + y:
    case 15:
        print("Result is 15.")
    case 20:
        print("Result is 20.")
    case _:
        print("No match found.")
C#
using System;

class Program {
    static void Main()
    {
        int x = 10;
        int y = 5;
        int sum = x + y; // now sum is a variable

        switch (sum) {
        case 15:
            Console.WriteLine("Result is 15.");
            break;
        case 20:
            Console.WriteLine("Result is 20.");
            break;
        default:
            Console.WriteLine("No match found.");
            break;
        }
    }
}
JavaScript
const x = 10;
const y = 5;

switch (x + y) {
case 15:
    console.log("Result is 15.");
    break;
case 20:
    console.log("Result is 20.");
    break;
default:
    console.log("No match found.");
}

Output
Result is 15.

2. Limited to Certain Types: Switch statements are mainly designed for int, char, or string values depending on the language.

C++
#include <iostream>
using namespace std;

int main()
{
    char grade = 'B';

    switch (grade)
    {
    case 'A':
        cout << "Excellent!" << endl;
        break;
    case 'B':
        cout << "Good!" << endl;
        break;
    default:
        cout << "Not specified." << endl;
    }

    return 0;
}
C
#include <stdio.h>

int main()
{
    char grade = 'B';

    switch (grade)
    {
    case 'A':
        printf("Excellent!\n");
        break;
    case 'B':
        printf("Good!\n");
        break;
    default:
        printf("Not specified.\n");
    }

    return 0;
}
Java
public class GFG {
    public static void main(String[] args)
    {
        char grade = 'B';

        switch (grade) {
        case 'A':
            System.out.println("Excellent!");
            break;
        case 'B':
            System.out.println("Good!");
            break;
        default:
            System.out.println("Not specified.");
        }
    }
}
Python
grade = 'B'

match grade:
    case 'A':
        print("Excellent!")
    case 'B':
        print("Good!")
    case _:
        print("Not specified.")
C#
using System;

class GFG {
    static void Main()
    {
        char grade = 'B';

        switch (grade) {
        case 'A':
            Console.WriteLine("Excellent!");
            break;
        case 'B':
            Console.WriteLine("Good!");
            break;
        default:
            Console.WriteLine("Not specified.");
            break;
        }
    }
}
JavaScript
let grade = "B";

switch (grade) {
case "A":
    console.log("Excellent!");
    break;
case "B":
    console.log("Good!");
    break;
default:
    console.log("Not specified.");
}

Output
Good!

Ternary Expression Conditional Statement

The ternary operator is a short way to write an if-else statement. It evaluates a condition and returns one value if the condition is true, and another value if the condition is false.

  • It is called a ternary operator because each ternary expression uses three parts.
  • Multiple ternary expressions can also be nested to check more conditions.

Flowchart of Ternary Condition
conditional or ternary operator in c

C++
using namespace std;

int main()
{

    int x = 10;
    cout << (x > 0 ? "x is positive" : "x is not positive");
    return 0;
}
C
#include <stdio.h>

int main()
{

    int x = 10;
    printf(x > 0 ? "x is positive" : "x is not positive");
    return 0;
}
Java
import java.util.Scanner;

public class GFG {
    public static void main(String[] args)
    {

        int x = 10;
        System.out.println(x > 0 ? "x is positive"
                                 : "x is not positive");
    }
}
Python
x = 10

print("x is positive" if x > 0 else "x is not positive")
C#
using System;

class GFG {
    static void Main()
    {
        int x = 10;
        Console.WriteLine(x > 0 ? "x is positive"
                                : "x is not positive");
    }
}
JavaScript
let x = 10;
console.log(x > 0 ? "x is positive" : "x is not positive");

Output
x is positive

Nested Ternary Condition: A nested ternary condition is a ternary operator placed inside another ternary operator. It is used when you need to check multiple conditions in a single line. The inner ternary executes only if required by the outer condition.

Flowchart of Nested Ternary Condition

resultant_value
C++
using namespace std;

int main()
{

    int x = 0;
    cout << (x > 0 ? "x is positive" : (x < 0 ? "x is not positive" : "x is zero"));
    return 0;
}
C
#include <stdio.h>

int main()
{

    int x = 0;
    printf(x > 0 ? "x is positive" : (x < 0 ? "x is not positive" : "x is zero"));
    return 0;
}
Java
import java.util.Scanner;

public class GFG {
    public static void main(String[] args)
    {

        int x = 0;
        System.out.println(
            x > 0 ? "x is positive"
                  : (x < 0 ? "x is not positive"
                           : "x is zero"));
    }
}
Python
x = 0
print("x is positive" if x > 0 else (
    "x is not positive" if x < 0 else "x is zero"))
C#
using System;

class GFG {
    static void Main()
    {
        int x = 0;
        Console.WriteLine(x > 0
                              ? "x is positive"
                              : (x < 0 ? "x is not positive"
                                       : "x is zero"));
    }
}
JavaScript
let x = 0;
console.log(
    x > 0 ? "x is positive"
          : (x < 0 ? "x is not positive" : "x is zero"));

Output
x is zero
Comment