0% found this document useful (0 votes)
68 views3 pages

Learn C++ - Conditionals & Logic Cheatsheet - Codecademy

The document discusses various conditionals and logical operators in C++ including if statements, relational operators, else clauses, switch statements, else if statements, and logical operators. Key points covered are: 1) An if statement executes code if a condition is true, else clauses execute code if the condition is false. 2) Relational operators like ==, >, < compare values and return true or false. 3) switch statements check an expression against cases and execute matching code blocks. 4) else if statements provide additional conditions between if and else. 5) Logical operators like &&, ||, and ! combine or negate conditions.

Uploaded by

hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views3 pages

Learn C++ - Conditionals & Logic Cheatsheet - Codecademy

The document discusses various conditionals and logical operators in C++ including if statements, relational operators, else clauses, switch statements, else if statements, and logical operators. Key points covered are: 1) An if statement executes code if a condition is true, else clauses execute code if the condition is false. 2) Relational operators like ==, >, < compare values and return true or false. 3) switch statements check an expression against cases and execute matching code blocks. 4) else if statements provide additional conditions between if and else. 5) Logical operators like &&, ||, and ! combine or negate conditions.

Uploaded by

hamza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Cheatsheets / Learn C++

Conditionals & Logic


if Statement
An if statement is used to test an expression for truth.
if (a == 10) {
● If the condition evaluates to true , then the code   // Code goes here
within the block is executed; otherwise, it will be
}
skipped.

Relational Operators
Relational operators are used to compare two values and
return true or false depending on the comparison: if (a > 10) {
   // ☝️ means greater than
● == equal to
}
● != not equal to

● > greater than

● < less than

● >= greater than or equal to

● <= less than or equal to

else Clause
An else clause can be added to an if statement.
if (year == 1991) {
● If the condition evaluates to true , code in the if   // This runs if it is true
part is executed.
}
● If the condition evaluates to false , code in the else {
else part is executed.   // This runs if it is false
}
switch Statement
A switch statement provides a means of checking an
expression against various case s. If there is a match, the switch (grade) {
code within starts to execute. The break keyword can be   case 9:
used to terminate a case.     std::cout << "Freshman\n";
default is executed when no case matches.
    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;
}

else if Statement
One or more else if statements can be added in
between the if and else to provide additional if (apple > 8) {
condition(s) to check.   // Some code here
}
else if (apple > 6) {
  // Some code here
}
else {
  // Some code here
}
Logical Operators
Logical operators can be used to combine two different
conditions. if (coffee > 0 && donut > 1) {
  // Code runs if both are true
● && requires both to be true ( and )
}
● || requires either to be true ( or )

● ! negates the result ( not ) if (coffee > 0 || donut > 1) {


  // Code runs if either is true
}

if (!tired) {
  // Code runs if tired is false
}

You might also like