Conditional Statements in Programming

Conditional statements in programming are used to execute certain blocks of code based on specified conditions. They are fundamental to decision-making in programs. Here are some common types of conditional statements:

1. If Statement in Programming:

The if statement is used to execute a block of code if a specified condition is true.

C++




#include <iostream>
using namespace std;
int main()
{
    int a = 5;
    if (a == 5) {
        cout << "a is equal to 5";
    }
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int a = 5;
    if (a == 5) {
        printf("a is equal to 5");
    }
    return 0;
}


Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a = 5;
        if (a == 5) {
            System.out.println("a is equal to 5");
        }
    }
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        int a = 5;
        if (a == 5) {
            Console.WriteLine("a is equal to 5");
        }
    }
}


Javascript




let a = 5;
if (a === 5) {
    console.log("a is equal to 5");
}


Python3




a = 5
if a == 5:
    print("a is equal to 5")


Output

a is equal to 5

2. if-else Statement in Programming:

The if-else statement is used to execute one block of code if a specified condition is true, and another block of code if the condition is false.

C++




#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    if (a == 5) {
        cout << "a is equal to 5";
    }
    else {
        cout << "a is not equal to 5";
    }
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int a = 10;
    if (a == 5) {
        printf("a is equal to 5");
    }
    else {
        printf("a is not equal to 5");
    }
    return 0;
}


Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a = 10;
        if (a == 5) {
            System.out.println("a is equal to 5");
        }
        else {
            System.out.println("a is not equal to 5");
        }
    }
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        int a = 10;
        if (a == 5) {
            Console.WriteLine("a is equal to 5");
        }
        else {
            Console.WriteLine("a is not equal to 5");
        }
    }
}


Javascript




let a = 10;
if (a === 5) {
    console.log("a is equal to 5");
} else {
    console.log("a is not equal to 5");
}


Python3




a = 10
if a == 5:
    print("a is equal to 5")
else:
    print("a is not equal to 5")


Output

a is not equal to 5

3. if-else-if Statement in Programming:

The if-else-if statement is used to execute one block of code if a specified condition is true, another block of code if another condition is true, and a default block of code if none of the conditions are true.

C++




#include <iostream>
using namespace std;
int main()
{
    int a = 15;
    if (a == 5) {
        cout << "a is equal to 5";
    }
    else if (a == 10) {
        cout << "a is equal to 10";
    }
    else {
        cout << "a is not equal to 5 or 10";
    }
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int a = 15;
    if (a == 5) {
        printf("a is equal to 5");
    }
    else if (a == 10) {
        printf("a is equal to 10");
    }
    else {
        printf("a is not equal to 5 or 10");
    }
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a = 15;
        if (a == 5) {
            System.out.println("a is equal to 5");
        }
        else if (a == 10) {
            System.out.println("a is equal to 10");
        }
        else {
            System.out.println("a is not equal to 5 or 10");
        }
    }
}


C#




using System;
public class GFG {
    static public void Main()
    {
        int a = 15;
        if (a == 5) {
            Console.WriteLine("a is equal to 5");
        }
        else if (a == 10) {
            Console.WriteLine("a is equal to 10");
        }
        else {
            Console.WriteLine("a is not equal to 5 or 10");
        }
    }
}


Javascript




let a = 15;
if (a === 5) {
    console.log("a is equal to 5");
} else if (a === 10) {
    console.log("a is equal to 10");
} else {
    console.log("a is not equal to 5 or 10");
}


Python3




a = 15
if a == 5:
    print("a is equal to 5")
elif a == 10:
    print("a is equal to 10")
else:
    print("a is not equal to 5 or 10")


Output

a is not equal to 5 or 10

4. Ternary Operator or Conditional Operator in Programming:

In some programming languages, a ternary operator is used to assign a value to a variable based on a condition.

C++




#include <iostream>
using namespace std;
 
int main()
{
    int a = 10;
    cout << (a == 5 ? "a is equal to 5"
                    : "a is not equal to 5");
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int a = 10;
    printf("%s", (a == 5 ? "a is equal to 5"
                         : "a is not equal to 5"));
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a = 10;
        System.out.println(a == 5 ? "a is equal to 5"
                                  : "a is not equal to 5");
    }
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        int a = 10;
        Console.WriteLine(a == 5 ? "a is equal to 5"
                                 : "a is not equal to 5");
    }
    // Code
}


Javascript




let a = 10;
console.log(a === 5 ? "a is equal to 5" : "a is not equal to 5");


Python3




a = 10
print("a is equal to 5" if a == 5 else "a is not equal to 5")


Output

a is not equal to 5

5. Switch Statement in Programming:

In languages like C, C++, and Java, a switch statement is used to execute one block of code from multiple options based on the value of an expression.

C++




#include <iostream>
using namespace std;
int main()
{
    int a = 15;
    switch (a) {
    case 5:
        cout << "a is equal to 5";
        break;
    case 10:
        cout << "a is equal to 10";
        break;
    default:
        cout << "a is not equal to 5 or 10";
    }
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    int a = 15;
    switch (a) {
    case 5:
        printf("a is equal to 5");
        break;
    case 10:
        printf("a is equal to 10");
        break;
    default:
        printf("a is not equal to 5 or 10");
    }
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int a = 15;
        switch (a) {
        case 5:
            System.out.println("a is equal to 5");
            break;
        case 10:
            System.out.println("a is equal to 10");
            break;
        default:
            System.out.println("a is not equal to 5 or 10");
        }
    }
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        int a = 15;
        switch (a) {
        case 5:
            Console.WriteLine("a is equal to 5");
            break;
        case 10:
            Console.WriteLine("a is equal to 10");
            break;
        default:
            Console.WriteLine("a is not equal to 5 or 10");
            break;
        }
    }
}


Javascript




let a = 15;
switch (a) {
    case 5:
        console.log("a is equal to 5");
        break;
    case 10:
        console.log("a is equal to 10");
        break;
    default:
        console.log("a is not equal to 5 or 10");
}


Output

a is not equal to 5 or 10

Each programming language may have its own syntax and specific variations of these conditional statements.

Control flow statements in Programming

Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow Statements.

Table of Content

  • What are Control Flow Statements in Programming?
  • Types of Control Flow statements in Programming
  • Conditional Statements in Programming
  • Looping Statements in Programming
  • Jump Statements in Programming

Similar Reads

What are Control Flow Statements in Programming?

Control flow statements are fundamental components of programming languages that allow developers to control the order in which instructions are executed in a program. They enable execution of a block of code multiple times, execute a block of code based on conditions, terminate or skip the execution of certain lines of code, etc....

Types of Control Flow statements in Programming:

Control Flow Statements Type Control Flow Statement Description Conditional Statements if-else Executes a block of code if a specified condition is true, and another block if the condition is false. switch-case Evaluates a variable or expression and executes code based on matching cases. Looping Statements for Executes a block of code a specified number of times, typically iterating over a range of values. while Executes a block of code as long as a specified condition is true. do-while Executes a block of code once and then repeats the execution as long as a specified condition is true. Jump Statements break Terminates the loop or switch statement and transfers control to the statement immediately following the loop or switch. continue Skips the current iteration of a loop and continues with the next iteration. return Exits a function and returns a value to the caller. goto Transfers control to a labeled statement within the same function. (Note: goto is generally discouraged due to its potential for creating unreadable and error-prone code.)...

Conditional Statements in Programming:

Conditional statements in programming are used to execute certain blocks of code based on specified conditions. They are fundamental to decision-making in programs. Here are some common types of conditional statements:...

Looping Statements in Programming:

...

Jump Statements in Programming:

...

Contact Us