0% found this document useful (0 votes)
14 views

Logic and Conditionals

The document provides a cheatsheet on logic and conditionals in C#, including if statements, the break keyword, comparison operators, switch statements, boolean expressions, and the boolean type. If statements execute code blocks based on boolean expressions. The break keyword exits switch blocks. Comparison operators compare values and return true or false. Switch statements evaluate expressions and run matching code blocks. Boolean expressions return true or false values. The bool data type can be true or false.

Uploaded by

cf8qrn9q4r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Logic and Conditionals

The document provides a cheatsheet on logic and conditionals in C#, including if statements, the break keyword, comparison operators, switch statements, boolean expressions, and the boolean type. If statements execute code blocks based on boolean expressions. The break keyword exits switch blocks. Comparison operators compare values and return true or false. Switch statements evaluate expressions and run matching code blocks. Boolean expressions return true or false values. The bool data type can be true or false.

Uploaded by

cf8qrn9q4r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

19/01/2024, 13:13 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy

Cheatsheets / Learn C#

Learn C#: Logic and Conditionals

If Statements

In C#, an if statement executes a block of code based if (true) {


on whether or not the boolean expression provided in
// This code is executed.
the parentheses is true or false .
If the expression is true then the block of code Console.WriteLine("Hello User!");
inside the braces, {} , is executed. Otherwise, the }
block is skipped over.

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

One of the uses of the break keyword in C# is to string color = "blue";


exit out of switch / case blocks and resume
program execution after the switch code block. In
switch (color) {
C#, each case code block inside a switch
statement needs to be exited with the break case "red":
keyword (or some other jump statement), otherwise the Console.WriteLine("I don't like that
program will not compile. It should be called once all of
color.");
the instructions specific to that particular case have
break;
been executed.
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

A comparison operator, as the name implies, compares int x = 5;


two expressions and returns either true or
Console.WriteLine(x < 6); // Prints
false depending on the result of the comparison.
For example, if we compared two int values, we
"True" because 5 is less than 6.
could test to see if one number is greater than the Console.WriteLine(x > 8); // Prints
other, or if both numbers are equal. Similarly, we can "False" because 5 is not greater than 8.
test one string for equality against another
string .
string foo = "foo";
Console.WriteLine(foo == "bar"); //
Prints "False" because "foo" does not
equal "bar".

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;
}

// The switch statement above is


equivalent to this:
if (fruit == "Banana") {
Console.WriteLine("Peel first.");
} else if (fruit == "Durian") {
Console.WriteLine("Strong smell.");
} else {
Console.WriteLine("Nothing to say.");
}

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

A truth table is a way to visualize boolean logic. Since


booleans only have two possible values, that means that
we can compactly list out in a table all the possible
input and output pairs for unary and binary boolean
operators.
The image below gives the truth tables for the AND, OR,
and NOT operators. For each row, the last column
represents the output given that the other columns
were fed as input to the corresponding operator.

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

An else followed by braces, {} , containing a code if (true) {


block, is called an else clause. else clauses must
// This block will run.
always be preceded by an if statement.
The block inside the braces will only run if the
Console.WriteLine("Seen!");
expression in the accompanying if condition is } else {
false . It is useful for writing code that runs only if // This will not run.
the code inside the if statement is not executed. Console.WriteLine("Not seen!");
}

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 int x = 100, y = 80;


else statements is to have an else block that
contains another nested if statement, which can
if (x > y)
contain another else , etc. A better way to express
this pattern in C# is with else if statements. The {
first condition that evaluates to true will run its Console.WriteLine("x is greater than
associated code block. If none are true , then the y");
optional else block 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
19/01/2024, 13:13 Learn C#: Learn C#: Logic and Conditionals Cheatsheet | Codecademy

Conditional Control

Conditional statements or conditional control


structures allow a program to have different behaviors
depending on certain conditions being met.
Intuitively, this mimics the way humans make simple
decisions and act upon them. For example, reasoning
about whether to go outside might look like:
Condition: Is it raining outside?
If it is raining outside, then bring an
umbrella.
Otherwise, do not bring an umbrella.
We could keep adding clauses to make our reasoning
more sophisticated, such as “If it is sunny, then wear
sunscreen”.

Control Flow

In programming, control flow is the order in which


statements and instructions are executed.
Programmers are able to change a program’s control
flow using control structures such as conditionals.
Being able to alter a program’s control flow is powerful,
as it lets us adapt a running program’s behavior
depending on the state of the program. For example,
suppose a user is using a banking application and wants
to withdraw $500. We certainly want the application to
behave differently depending on whether the user has
$20 or $1000 in their bank account!

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

You might also like