C++ if-else Statements
w3hello.com logo
C++ Basics
What is C++
Introduction to C++
C++ Installation
Comments in C++
C++ Variables
C++ Pointer Operators
C++ Data Types
C++ Variable
C++ Loop Types
Discuss C++
C++ While Loop
C++ do while loops
C++ while loops
C++ Increment and Decrement Operators
C++ For Loop
C++ if Statements
C++ nested if statements
C++ Comments
C++ for loops
C++ do...while loop
C++ if-else Statements
C++ switch
C++ if...else statement
C++ OOPs Concepts
Operators in C++
C++ Operators
C++ Loops
C++ Member (dot & arrow) Operators
C++ Basic Input Output (cin, cout, endl)
Operators Precedence in C++
C vs C++
C++ switch Statements
C++ if-else
C++ Exception Handling
Constructors and Destructors in C++
C++ File and Stream
C++ Continue Statement
C++ Object Oriented
C++ Arithmetic Operators
C++ Pointers
Pointers in C++
C++ try-catch
C++ Enumeration
ANSI C++ New Features
C++ Recursion
C++ Modifier Types
C++ Break Statement
C++ ios_base init
C++ Compute the Sum and Average of Two Numbers
C++ Preprocessor
C++ Class Constructor and Destructor
Matrix Multiplication in C++
C++ Strings
C++ Basic Input/Output
C++ Qualifiers and Storage Classes
C++ Assignment Operators
Data Abstraction in C++
C++ Class Access Modifiers
C++ Structs
Interfaces in C++ (Abstract Classes)
String Manipulation in C++
C++ References
C++ Date and Time
C++ Useful Resources
C++ ios_base openmode
C++ Aggregation
C++ Introduction
Call by value and call by reference in C++
C++ ios_base fmtflags
C++ Templates
C++ Namespaces
C++ Fstream fpos
C++ ios_base event
Returning values by reference in C++
C++ Polymorphism
C++ nested loops
C++ Constants
C++ Casting Operators
C++ Objects and Classes
C++ static
Storage Classes in C++
C++ ios_base seekdir
Data Encapsulation in C++
C++ Null Pointers
C++ Inheritance
Armstrong Number in C++
C++ switch statement
C++ Encapsulation
Pointer to C++ Classes
C++ Files and Streams
C++ Interfaces
Data Types in C++
Variable Scope in C++
C++ Destructor
C++ Manipulators
Static Members of a C++ Class
Fibonacci Series in C++
C++ this Pointer
C++ Object Class
Polymorphism in C++
C++ Basics
Previous : C++ do...while loop Next : C++ switch

C++ if-else Statements


If else statements in C++ is also used to control the program flow based on some condition, only the difference is: it’s used to execute some statement code block if expression is evaluated to true, otherwise executes else statement code block.

The basic format of if else statement is:

Syntax:
if(test_expression)
{
   //execute your code
}
else
{
   //execute your code
}

Figure – Flowchart of if else Statement:

c++-if-else

Example of a C++ Program to Demonstrate if else Statement

Example:
#include <iostream>
using namespace std;

int main()
{
  int a = 15, b = 20;

  if (b > a) {  
    cout << "b is greater" << endl;
  } else {  
    cout << "a is greater" << endl;
  }  
  system("PAUSE");
}
Program Output:

cplusplus-if-else

Example:
#include
<iostream>
using namespace std;

int main()
{
    char name;
    int password;

    cout << "Enter the name: "; cin >> name;
    cout << " Enter your password: "; cin >> password;
    if (name == 'GG') {
        if (password == 1346) {
            cout << "Login successful";
        }
        else {
            cout << "Incorrect PASSWORD, Try again.";
        }
    }
    else {
        cout << " Incorrect Login Details, Try again.";
    }
}
Program Output:
Enter the
name: GG
Enter your password: 1346
Login successful

Previous : C++ do...while loop Next : C++ switch
© Copyright 2018 W3Hello Publishing Limited. All rights reserved.
Contact us | Sitemap