Conditional Statements in C++

Level : Beginner
Mentor: Shailendra Chauhan
Duration : 00:02:00

What are Conditional Statements in C++?

In C++, conditional statements are control structures that allow code to make decisions. These statements are also referred to as decision-making statements. These conditions are provided by a series of decision-making statements with Boolean expressions. Boolean expressions are evaluated as true or false. These types include if statement, if-else, if-else-if ladder, switch, and more. These statements determine how the program is executed.

Types of Conditional Statements in C++

Types of Conditional Statements in C++ include:

  • If Statement in C++
  • If-Else Statement in C++
  • If Else-If Ladder in C++
  • Nested If Statement in C++
  • Switch Statement in C++

If Statement

An if statement is made up of a Boolean expression that is evaluated into a Boolean value. If the value is true, the block is executed; else, the next statement is executed.

If-Else Statement

An if-else statement consists of two statements: the if statement and the else statement. When an expression in an if-statement is evaluated as true, the if block is executed; otherwise, the else block is executed.

If-Else-If Statement or Ladder

An if-else-if statement, often known as a ladder statement, checks a set of conditions using subsequent if-else-if statements. The statement that meets the outcome will be performed. When no conditions match the result, the default else statement is executed.

Nested If Statement

An if-else block can be nested within another if-else block. This generates a nested if-else expression.

Switch Statement

A switch statement evaluates a set of conditions using one or more cases that are compared to the switch expression. The case that meets the result will be performed. If no cases match the result, the default case will be executed.

Rules for using the Switch Statement in C++

  • The data type of the switch expression must be either an integer or a character.
  • Each case label must be followed by a colon ":".
  • The case value should be an integer or a character constant.
  • The break keyword is optional, although it can reduce the execution duration of your program. It will stop the wasteful execution of other cases once a match has been detected.
Self-paced Membership
  • 24+ Video Courses
  • 850+ Hands-On Labs
  • 500+ Quick Notes
  • 225+ Skill Tests
  • 120+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this