Learn C# - Learn C# - Logic and Conditionals Cheatsheet - Codecademy
Learn C# - Learn C# - Logic and Conditionals Cheatsheet - Codecademy
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
10-02-2024 16:03 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy
Break Keyword
One of the uses of the break keyword in C# is to exit string color = "blue";
out of switch / case blocks and resume program
execution after the switch code block. In C#, each
case code block inside a switch statement needs to switch (color) {
be exited with the break keyword (or some other case "red":
jump statement), otherwise the program will not Console.WriteLine("I don't like that
compile. It should be called once all of the instructions
color.");
specific to that particular case have been executed.
break;
case "blue":
Console.WriteLine("I like that
color.");
break;
default:
Console.WriteLine("I feel ambivalent
about that color.");
break;
}
// Regardless of where the break
statement is in the above switch
statement,
// breaking will resume the program
execution here.
Console.WriteLine("- Steve");
Comparison Operators
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 2/7
10-02-2024 16:03 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 often
will run.
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
10-02-2024 16:03 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy
Boolean Type
The bool data type can be either true or false and bool skyIsBlue = true;
is based on the concept that the validity of all logical
bool penguinsCanFly = false;
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
10-02-2024 16:03 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
A common pattern when writing multiple if and else int x = 100, y = 80;
statements is to have an else block that contains
another nested if statement, which can contain
another else , etc. A better way to express this pattern if (x > y)
in C# is with else if statements. The first condition {
that evaluates to true will run its associated code Console.WriteLine("x is greater than
block. If none are true , then the optional else block
y");
will run if it exists.
}
else if (x < y)
{
Console.WriteLine("x is less than y");
}
else
{
Console.WriteLine("x is equal to y");
}
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 5/7
10-02-2024 16:03 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 : expression2 .
// This sets umbrellaOrNot to "Umbrella"
It takes one boolean condition and two expressions as
inputs. Unlike an if statement, the ternary operator is if isRaining is true,
an expression itself. It evaluates to either its first input // and "No Umbrella" if isRaining is
expression or its second input expression depending on
false.
whether the condition is true or false , respectively.
string umbrellaOrNot = isRaining ?
"Umbrella" : "No Umbrella";
// "Umbrella"
Console.WriteLine(umbrellaOrNot);
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-logic-conditionals/cheatsheet 6/7
10-02-2024 16:03 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