• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Structures » Conditional Statements in C++ or if Statements
Next →
← Prev

Conditional Statements in C++ or if Statements

By Dinesh Thakur

Conditional statements, also known as selection statements, are used to make decisions based on a given condition. If the condition evaluates to True, a set of statements is executed, otherwise another set of statements is executed.

The if Statement: The if statement selects and executes the statement(s) based on a given condition. If the condition evaluates to True then a given set of statement(s) is executed. However, if the condition evaluates to False, then the given set of statements is skipped and the program control passes to the statement following the if statement. The syntax of the if statement is

if (condition) {
  statement 1;
  statement 2;
  statement 3;
}

The if-else Statement: The if – else statement causes one of the two possible statement( s) to execute, depending upon the outcome of the condition.

The syntax of the if-else statements is

if (condition) // if part {
  statement1;
  statement2;
}
else // else part
  statement3;

Here, the if-else statement comprises two parts, namely, if and else. If the condition is True the if part is executed. However, if the condition is False, the else part is executed.

To understand the concept of the if -else statement, consider this example.

Example : A code segment to determine the greater of two numbers

if(x>y)
  cout<<"x is greater";
else
cout<<"y is greater";

Nested if-else Statement: A nested if-else statement contains one or more if-else statements. The if else can be nested in three different ways, which are discussed here.

• The if – else statement is nested within the if part.

The syntax is

if (condition1) {
  statement1;
if(condition2)
  statement2;
else
  statement3;
}
else
  statement4;

• The if-else statement is nested within the else part.

The syntax is

if(condition1)
  statement1;
else {
  statement4;
if (condition2)
  statement2;
else
  statement3;
}
  statement5;

• The if-else statement is nested within both the if and the else parts.

The syntax is

if(condition1) {
  statement1;
if (condition2)
  statement2;
else
  statement3;
}
else {
  statement4;
if (condition3)
  statement5;
else
  statement6;
}
statement7;

To understand the concept of nested if-else, consider this example.

Example : A code segment to determine the largest of three numbers

if (a>b) {
  if (a>c)
    cout<<"a is largest";
 }
else //nested if-else statement within else {
 if (b>c)
   cout<<"b is largest";
 else
   cout<<"c is largest";
}

The if-else-if ladder, also known as the if-else-if staircase, has an if-else statement within the outermost else statement. The inner else statement can further have other if-else statements.

The syntax of the if -e1se- if ladder is

if e1se if LadderTo understand the concept of the if-else-if ladder, consider this example.

Example: A program to determine whether a character is in lower-case or upper case

#include<iostream>
using namespace std;
int main() {
  char ch;
  cout<<"Enter an alphabet:";
  cin>>ch;
  if( (ch>='A') && (ch<='Z'))
    cout<<"The alphabet is in upper case";
  else
  if ( (ch>=' a') && (ch<=' z' ) )
    cout<<"The alphabet is in lower case";
  else
  cout<<"It is not an alphabet";
  return 0;
}

The output of this program is

Enter an alphabet: $

It is not an alphabet

Conditional Operator as an Alternative: The conditional operator ‘? :’ selects one of the two values or expressions based on a given condition. Due to this decision-making nature of the conditional operator, it is sometimes used as an alternative to if-else statements. Note that the conditional operator selects one of the two values or expressions and not the statements as in the case of an if-else statement. In addition, it cannot select more than one value at a time, whereas if-else statement can select and execute more than one statement at a time. For example, consider this· statement.

max = (x>y ? x : y)

This statement assigns maximum of x and y to max

The switch Statement: The switch statement selects a set of statements from the available sets of statements. The switch statement tests the value of an expression in a sequence and compares it with the list of integers or character constants. When a match is found, all the statements associated with that constant are executed.

The syntax of the switch statement

switch(expression) {
case <constant1>: statement1;
 [break;]
case <constant2>: statement2;
[break;]
case <constant3>: statement3;
[default: statement4;]
[break;]
}
Statement5;

The C++ keywords case and default provide the list of alternatives. Note that it is not necessary for every case label to specify a unique set of statements. The same set of statements can be shared by multiple case labels.

The keyword default specifies the set of statements to be executed in case no match is found. Note that there can be multiple case labels but there can be only one default label. The break statements in the switch block are optional. However, it is used in the switch block to prevent a fall through. Fall through is a situation that causes the execution of the remaining cases even after a match has been found. In order to prevent this, break statements are used at the end of statements specified by each case and default. This causes the control to immediately break out of the switch block and execute the next statement.

To understand the concept of switch statement, consider this code segment.

Example : A code segment to demonstrate the use of switch statement

cin>>x;
int x;
switch(x) {
case l: cout<<"Option1 is selected";
break;
case 2: cout<<"Option2 is selected";
break;
case 3: cout<<"Option3 is selected";
break;
case 4: cout<<"Option4 is selected;
break;
default: cout<<"Invalid option!";
}

In this example, depending upon the input, an appropriate message is displayed. That is, if 2 are entered, then the message Option 2 is selected is displayed. In case, 5 is entered, then the message Invalid option! is displayed.

Similar to if and if-else statements, switch statements can also be nested within one another. A nested switch statement contains one or more switch statements within its case label or default label (if any).

You’ll also like:

  1. Write A C++ Program To Solve The Function By Using The Nested Conditional Statement.
  2. Conditional Directives in C
  3. Conditional Operator in Java with Example
  4. What is Control Statements?Explain
  5. Iteration Statements or Loops in C++
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW