Conditional Execution

conditional statement in c++ with example

conditional statement in cpp language

Code Example

#include <iostream>
int main() {
// Declare some Boolean variables
bool a = true, b = false;
std::cout << "a = " << a << ", b = " << b << '\n';
// Reassign a
a = false;
std::cout << "a = " << a << ", b = " << b << '\n';
// Mix integers and Booleans
a = 1;
b = 1;
std::cout << "a = " << a << ", b = " << b << '\n';
// Assign Boolean value to an integer
int x = a, y = true;
std::cout << "a = " << a << ", b = " << b
<< ", x = " << x << ", y = " << y << '\n';
// More mixing
a = 1725; // Warning issued
b = -19; // Warning issued
std::cout << "a = " << a << ", b = " << b << '\n';
}

The simplest Boolean expressions are false and true, the Boolean literals. A Boolean variable is also a Boolean expression. An expression comparing numeric expressions for equality or inequality is also a Boolean expression. The simplest kinds of Boolean expressions use relational operators to compare two expressions.

The Simple if Statement

The Boolean expressions described in Section 5.2 at first may seem arcane and of little use in practical programs. In reality, Boolean expressions are essential for a program to be able to adapt its behavior at run time. Most truly useful and practical programs would be impossible without the availability of Boolean expressions.