if StatementAn if statement is used to test an expression for truth.
true, then the code within the block is executed; otherwise, it will be skipped.if (a == 10) {// Code goes here}
else ClauseAn else clause can be added to an if statement.
true, code in the if part is executed. false, code in the else part is executed.if (year == 1991) {// This runs if it is true}else {// This runs if it is false}
Relational operators are used to compare two values and return true or false depending on the comparison:
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
if (a > 10) {// ☝️ means greater than}
else if StatementOne or more else if statements can be added in between the if and else to provide additional condition(s) to check.
if (apple > 8) {// Some code here}else if (apple > 6) {// Some code here}else {// Some code here}
switch StatementA switch statement provides a means of checking an expression against various cases. If there is a match, the code within starts to execute. The break keyword can be used to terminate a case.
default is executed when no case matches.
switch (grade) {case 9:std::cout << "Freshman\n";break;case 10:std::cout << "Sophomore\n";break;case 11:std::cout << "Junior\n";break;case 12:std::cout << "Senior\n";break;default:std::cout << "Invalid\n";break;}
Logical operators can be used to combine two different conditions.
&& requires both to be true (and)|| requires either to be true (or)! negates the result (not)if (coffee > 0 && donut > 1) {// Code runs if both are true}if (coffee > 0 || donut > 1) {// Code runs if either is true}if (!tired) {// Code runs if tired is false}