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

Conditionals & Logic Cheatsheet - Codecademy

The document discusses different conditionals and logical operators in C++ including if/else statements, else if statements, switch statements, and logical operators. If statements execute code if a condition is true, else clauses execute if false. Else if provides additional conditions. Switch checks an expression against cases. Logical operators combine conditions using &&, ||, and !.

Uploaded by

rwopara2007
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)
34 views3 pages

Conditionals & Logic Cheatsheet - Codecademy

The document discusses different conditionals and logical operators in C++ including if/else statements, else if statements, switch statements, and logical operators. If statements execute code if a condition is true, else clauses execute if false. Else if provides additional conditions. Switch checks an expression against cases. Logical operators combine conditions using &&, ||, and !.

Uploaded by

rwopara2007
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
if Statement

An if statement is used to test an expression if (a == 10) {


for truth.
// Code goes here
If the condition evaluates to true , then
the code within the block is executed; }
otherwise, it will be skipped.

else
else Clause

An else clause can be added to an if if (year == 1991) {


statement.
// This runs if it is true
If the condition evaluates to true , code
in the if part is executed. }
If the condition evaluates to false , else {
code in the else part is executed. // This runs if it is false
}

Relational Operators

Relational operators are used to compare two if (a > 10) {


values and return true or false depending
// means greater than
on the comparison:
== equal to }
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
else
else if
if Statement

One or more else if statements can be if (apple > 8) {


added in between the if and else to
// Some code here
provide additional condition(s) to check.
}
else if (apple > 6) {
// Some code here
}
else {
// Some code here
}

switch
switch Statement

A switch statement provides a means of switch (grade) {


checking an expression against various case s.
case 9:
If there is a match, the code within starts to
execute. The break keyword can be used to std::cout << "Freshman\n";
terminate a case. break;
default is executed when no case matches. 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

Logical operators can be used to combine two if (coffee > 0 && donut > 1) {
different conditions.
// 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
}

Print Share

You might also like