Conditions - sheerazwalid/COMP-I GitHub Wiki

Data input and output

cout << "What is the temperature? ";
int temperature;
cin >> temperature;

If statement

  • If statement would be evaluated, if the conditional expression is true
if (temperature <= 32) {
    A << endl;
}

if (temperature > 32) {
    cout << "Ice will melt." << endl;
}

If-else statement

cout << "What is the temperature? ";
int temperature;
cin >> temperature;

if (temperature <= 32) {
    cout << "Water will freeze." << endl;    //cout is an output stream. the expression cout << ____ evaluates to cout 


// sample. these are equalient 
cout << " Water will freeze.";
cout << endl;
 
} else {
    cout << "Ice will melt." << endl;
}
cout << "What is the temperature? ";
int temperature;
cin >> temperature;

if (temperature <= 32) {
    cout << "It's freezing." << endl;     
} else if (temperature <= 60) {
    cout << "It's cool." << endl;
} else {
    cout << "It's warm." << endl;
}

Precedence & Associativity Summary

Operators | Associativity | Precedence Order :-:|:-:|:-:|:-------------: ! | - | Highest
*,/,% | left to right | ↓
+,- | left to right | ↓
<,<=,>,>= | left to right | ↓
==, != | left to right | ↓
&& | left to right | ↓
|| | left to right | ↓
=, +=, –=, *=, /=, %= | right to left | Lowest

Boolean operators

  • The symbol && means "and".
  • The expression (a && b) is true if both operands a and b are true. Otherwise, it's false.
a b a && b
T T T
T F F
F T F
F F F
  • The symbol || means "or".
  • The expression a || b is true if at least one of the operands is true. Otherwise, its false.
a b a || b
T T T
T F T
F T T
F F F

The symbol ! means "not".

a !a
T F
F T
if (temperature > 32 && temperature < 212) {
    cout << "Water will appear as a liquid on this planet.";    
}

Additional reading

⚠️ **GitHub.com Fallback** ⚠️