Learn C#: Logic and Conditionals
Learn how to work with Boolean logic and conditional statements in C#.
StartKey Concepts
Review core concepts you need to learn to master this subject
Boolean Expressions
Boolean Type
Logical Operators
Comparison Operators
Truth Tables
Conditional Control
Control Flow
If Statements
Boolean Expressions
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.
Understanding Logic in C#
Lesson 1 of 2
- 1Computers 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…
- 2In 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…
- 3When 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…
- 4We 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: -…
- 5As 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, …
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory