VOOZH about

URL: https://www.geeksforgeeks.org/dsa/conditional-statements-in-programming/

⇱ Conditional Statements in Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Conditional Statements in Programming

Last Updated : 22 May, 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.


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

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



Output
x is zero

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

Output
Tuesday

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.


Output
Result is 15.

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


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


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

Output
x is zero
Comment
Article Tags:
Article Tags: