TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

C++

C++ if-else statement

C++

Introduction

In this post, we will discuss conditional execution using the if statement in C++ programs. C++ if statement in its simplest form and has the following syntax as below:

if(expression){
     // statements to be executed when the expression is true

}

Flowchart

If the expression is true the action is executed, otherwise the action is not executed. Basic flowchart of the if statement is shown in the picture.

Sample Code

/*************************
* C++ if statement demo
*************************/

#include <iostream>
using namespace std;

int main()
{
 int n;
 cout << "Please enter a number : " ;
 cin >> n;
 if(n > 0)
 cout << "Number is positive." << endl;
 return 0;
}

Sample run:

Please enter a number : 5
Number is positive.

Process returned 0 (0x0) execution time : 3.236 s
Press any key to continue.

*******************

Please enter a number : -5

Process returned 0 (0x0) execution time : 6.017 s
Press any key to continue.

*******************

 

 

if-else statement

In situations where different actions are to be executed based on the expression.

if(expression){
      action1

}
else {
      action2

}

 

 

/*************************
* C++ if statement demo
*************************/

#include <iostream>
using namespace std;

int main()
{
 int n;
 cout << "Please enter a number : " ;
 cin >> n;
 // if-else statement
 if(n > 0)
 cout << "Number is positive." << endl;
 else
 cout << "Number is negative." << endl;

 return 0;
}

Sample run

Please enter a number : 5
Number is positive.

Process returned 0 (0x0) execution time : 10.715 s
Press any key to continue.

**********************

Please enter a number : 5
Number is positive.

Process returned 0 (0x0) execution time : 10.715 s
Press any key to continue.

**********************

 

—

C++ Tutorials

C++ Tutorials on this website:

https://www.testingdocs.com/c-coding-tutorials/

For more information on the current ISO C++ standard

https://isocpp.org/std/the-standard

Related Posts

g not found OpenSuse

C++ /

Install GCC C++ Compiler on OpenSuse

sum of digits c program

C++ /

Sum of Digits of a Number C++ Program

C++ Hello World Program

C++ /

C++ Hello World Program

C++ /

C++ Program to Find Quadratic Equation Roots

C Class Constructor

C++ /

Define C++ Class Constructor

‹ C++ iteration using for loop› C++ Basic Input/Output

Recent Posts

  • Update draw.io on Windows
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com