Logic and Conditionals
Logic and Conditionals
Cheatsheets / Learn C#
If Statements
if (false) {
// This code is skipped.
Console.WriteLine("This won't be seen :
(");
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 1/7
19/01/2024, 13:13 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy
Break Keyword
Comparison Operators
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 2/7
19/01/2024, 13:13 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy
Switch Statements
A switch statement is a control flow structure that // The expression to match goes in
evaluates one expression and decides which code
parentheses.
block to run by trying to match the result of the
expression to each case . In general, a code block is switch (fruit) {
executed when the value given for a case equals the case "Banana":
evaluated expression, i.e, when == between the two // If fruit == "Banana", this block
values returns true . switch statements are
will run.
often used to replace if else structures when all
conditions test for equality on one value.
Console.WriteLine("Peel first.");
break;
case "Durian":
Console.WriteLine("Strong smell.");
break;
default:
// The default block will catch
expressions that did not match any above.
Console.WriteLine("Nothing to say.");
break;
}
Boolean Expressions
A boolean expression is any expression that evaluates // These expressions all evaluate to a
to, or returns, a boolean value.
boolean value.
// Therefore their values can be stored
in boolean variables.
bool a = (2 > 1);
bool b = a && true;
bool c = !false || (7 < 8);
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 3/7
19/01/2024, 13:13 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy
Boolean Type
The bool data type can be either true or bool skyIsBlue = true;
false and is based on the concept that the validity
bool penguinsCanFly = false;
of all logical statements must be either true or false.
Booleans encode the science of logic into computers, Console.WriteLine($"True or false, is the
allowing for logical reasoning in programs. In a broad sky blue? {skyIsBlue}.");
sense, the computer can encode the truthfulness or
// This simple program illustrates how
falseness of certain statements, and based on that
information, completely alter the behavior of the booleans are declared. However, the real
program. power of booleans requires additional
programming constructs such as
conditionals.
Logical Operators
Logical operators receive boolean expressions as input // These variables equal true.
and return a boolean value.
bool a = true && true;
The && operator takes two boolean expressions and
returns true only if they both evaluate to true . bool b = false || true;
The || operator takes two boolean expressions and bool c = !false;
returns true if either one evaluates to true .
The ! operator takes one boolean expression and
// These variables equal false.
returns the opposite value.
bool d = true && false;
bool e = false || false;
bool f = !true;
Truth Tables
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 4/7
19/01/2024, 13:13 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy
Else Clause
if (false) {
// Conversely, this will not run.
Console.WriteLine("Not seen!");
} else {
// Instead, this will run.
Console.WriteLine("Seen!");
}
If and Else If
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 5/7
19/01/2024, 13:13 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy
Conditional Control
Control Flow
Ternary Operator
In C#, the ternary operator is a special syntax of the bool isRaining = true;
form: condition ? expression1 :
// This sets umbrellaOrNot to "Umbrella"
expression2 .
It takes one boolean condition and two expressions as if isRaining is true,
inputs. Unlike an if statement, the ternary operator // and "No Umbrella" if isRaining is
is an expression itself. It evaluates to either its first false.
input expression or its second input expression
string umbrellaOrNot = isRaining ?
depending on whether the condition is true or
false , respectively. "Umbrella" : "No Umbrella";
// "Umbrella"
Console.WriteLine(umbrellaOrNot);
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 6/7
19/01/2024, 13:13 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 7/7