Key Concepts

Review core concepts you need to learn to master this subject

Boolean Expressions

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

A boolean expression is any expression that evaluates to, or returns, a boolean value.

Boolean Type

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

The bool data type can be either true or false and is based on the concept that the validity of all logical statements must be either true or false.

Booleans encode the science of logic into computers, allowing for logical reasoning in programs. In a broad sense, the computer can encode the truthfulness or falseness of certain statements, and based on that information, completely alter the behavior of the program.

Logical Operators

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

Logical operators receive boolean expressions as input and return a boolean value.

The && operator takes two boolean expressions and returns true only if they both evaluate to true.

The || operator takes two boolean expressions and returns true if either one evaluates to true.

The ! operator takes one boolean expression and returns the opposite value.

Comparison Operators

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

A comparison operator, as the name implies, compares two expressions and returns either true or false depending on the result of the comparison. For example, if we compared two int values, we could test to see if one number is greater than the other, or if both numbers are equal. Similarly, we can test one string for equality against another string.

Truth Tables

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

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.

Conditional Control

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

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

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

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!

If Statements

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

In C#, an if statement executes a block of code based on whether or not the boolean expression provided in the parentheses is true or false.

If the expression is true then the block of code inside the braces, {}, is executed. Otherwise, the block is skipped over.

Break Keyword

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

One of the uses of the break keyword in C# is to exit 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 be exited with the break keyword (or some other jump statement), otherwise the program will not compile. It should be called once all of the instructions specific to that particular case have been executed.

Ternary Operator

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

In C#, the ternary operator is a special syntax of the form: condition ? expression1 : expression2.

It takes one boolean condition and two expressions as inputs. Unlike an if statement, the ternary operator is an expression itself. It evaluates to either its first input expression or its second input expression depending on whether the condition is true or false, respectively.

Else Clause

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

An else followed by braces, {}, containing a code block, is called an else clause. else clauses must always be preceded by an if statement.

The block inside the braces will only run if the expression in the accompanying if condition is false. It is useful for writing code that runs only if the code inside the if statement is not executed.

If and Else If

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

A common pattern when writing multiple if and else 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 in C# is with else if statements. The first condition that evaluates to true will run its associated code block. If none are true, then the optional else block will run if it exists.

Switch Statements

// These expressions all evaluate to a boolean value. // Therefore their values can be stored in boolean variables. bool a = (2 > 1); bool b = a && true; bool c = !false || (7 < 8);

A switch statement is a control flow structure that evaluates one expression and decides which code block to run by trying to match the result of the expression to each case. In general, a code block is executed when the value given for a case equals the evaluated expression, i.e, when == between the two values returns true. switch statements are often used to replace if else structures when all conditions test for equality on one value.

Arrow Chevron Left Icon
Understanding Logic in C#
Lesson 1 of 2
Arrow Chevron Right Icon
  1. 1
    Computers are constantly checking the state of something. Is this program running or not? Does this variable exist or not? Is this value equal to that value? These yes or no questions demonstrate…
  2. 2
    In C#, we can represent Boolean values using the bool data type. Booleans, unlike numbers or strings, only have two values: true and false. To define a variable as a boolean, you define the data t…
  3. 3
    When writing a program, we often need to check if a value is correct or compare two values. Comparison operators allow us to compare values and evaluate their relationship. Rather than evaluating t…
  4. 4
    We can also use operators that use Boolean values as inputs and output. Logical operators, also known as Boolean operators, can be used to create Boolean expressions. Logical operators include: -…
  5. 5
    As we saw in the truth table, a Boolean expression that uses logical operators can be as simple as evaluating two boolean values: bool answer = true && false; // evaluates to False In this case, …
  6. 6
    Great job! You just learned about logic and boolean values, including: - How to define variables with a bool data type - How to use comparison operators with different data types to return boolean …
  1. 1
    We make decisions all the time in our life based on different conditions. Are you going to drink tea or coffee? Study history or biology? Buy a new shirt or save your money? We can program comput…
  2. 2
    Conditional statements are the most commonly used control structures in programming. They rely on the computer being able to reason whether conditions are true or false. The most basic conditiona…
  3. 3
    What if we want another set of instructions to execute if the condition is false? An else clause can be added to an if statement to provide code that will only be executed if the if condition is fa…
  4. 4
    What if we want to handle multiple conditions and have a different thing happen each time? Conditional statements can be chained by combining if and else statements into else if. After an initial i…
  5. 5
    Using multiple else if statements can get unwieldy pretty quickly. Imagine writing an else if statement for every possible number of guests. And you invited 20 people. You have to write a lot of re…
  6. 6
    The ternary operator allows for a compact syntax in the case of binary decisions. Like an if…else statement, it evaluates a single condition and executes one expression if the condition is true…
  7. 7
    Great job! You just learned about how to create programs that use control flow. Here’s a few of the things we covered: - Using if, else if, and else keywords to write conditional statements - Writi…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo