C++: Logical operators

This entry is part 14 of 61 in the series C++

I have mentioned in an earlier post that you can stack up the many if statements to check for the different conditions.

This could make the code look messy.

The logical operators provide a neat and simple solution. Using logical operators, you can combine a series of comparisons into a single expression so that you need just one if, almost regardless of the complexity of the set of conditions.

The example below use the && (And) operator.  Other common operators are || (Or) and ! (Not).

#include <iostream>
using namespace std;

void main()
	char letter = 'C';

	if (letter >= 'A' && letter <= 'Z')
		cout << "This is an uppercase letter." << endl;




Series Navigation<< C++: Combining while loop and if statement
C++: The for loop >>

Leave a comment

Leave a Reply