
OOP Lesson 3
Presentation
•
Computers
•
University
•
Practice Problem
•
Hard
Karen Ahmed
Used 2+ times
FREE Resource
14 Slides • 5 Questions
1
Lesson 3: Logic & Conditionals
2
Boolean logic is based on the idea that all values are either true or false. Logic is important to computer science because it is an early attempt at translating the human capacity for reason to computers. If a computer can use logic to reason about certain situations, it can use that rationale to make decisions.
3
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 type as bool. Then write the variable name and set it equal to the value, either true or false:
4
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 to an integer, they evaluate to boolean values. Expressions that evaluate to boolean values are known as boolean expressions.
5
Comparison operators include:
Equals ==: returns true if the value to the left is equal to the value to the right.
Inequality operator !=: returns true if the two values are not equal.
Less than <: returns true if the value to the left is less than the value to the right.
Greater than >: returns true if the value to the left is more than the value to the right.
Less than or equal to <=: returns true if the value to the left is less than or equal to the value on the right.
Greater than or equal to >=: returns true if the value to the left is more than or equal to the value to the right.
6
7
bool andExample = ((4 > 1) && (2 < 7));
// (True AND True) evaluates to True
bool orExample = ((8 > 6) || (3 > 6));
// (True OR False) evaluates to True
bool notExample = !(1 < 3);
// NOT (True) evaluates to False
8
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, we’re saying that answer is equal to the evaluation of true AND false. According to the truth table, answer will return False.
bool answer = (9 < 3) || (100 < 45); // evaluates to Falsebool another = ((3439 > 40) && (1 < 3)) || answer; // evaluates to True
9
We can program computers to make decisions based on different conditions. We can specify to the computer the order in which it should execute certain instructions, or that it should only execute certain instructions in specific cases. That means that depending on the conditions, the instructions that our program executes can change.
The order that computer programs execute a set of instructions is known as control flow. We can use different control structures to alter the flow of our program. Control structures let us handle different situations that might arise and make our programs more flexible. In C#, these type of statements are known as conditional or selection statements.
10
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 conditional statement is an if statement. An if statement executes a block of code if specified condition is true.
In C#, we write an if statement using the following syntax:
string color = "blue"; if (color == "blue"){ // this code block will execute only if the value of color is // equivalent to "blue" Console.WriteLine("color is blue");}
11
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 false.
In C#, we write an if..else… statement using the following syntax:
string color = "red"; if (color == "blue"){ // this code block will execute only if the value of color is // equivalent to "blue" Console.WriteLine("color is blue");} else { // this code block will execute if the value of color is // NOT equivalent to "blue" Console.WriteLine("color is NOT blue");}
12
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 if statement, one or more else if blocks can check additional conditions. An optional else block can be added at the end to catch cases that do not match any of the conditions.
In C#, we write an if..else if... statement using the following syntax:
string color = "red"; if (color == "blue"){ // this code block will execute only if the value of color is // equivalent to "blue" Console.WriteLine("color is blue");} else if (color == "red"){ // this code block will execute if the value of color is // equivalent to "red" Console.WriteLine("color is NOT blue");} else // this is optional{ // this code block will execute if the value of color is // NOT equivalent to "blue" OR "red" Console.WriteLine("color is NOT blue OR red");}
13
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 repetitive code, which is hard to read and prone to errors.
If it’s necessary to evaluate several conditions with their own unique output, a switch statement is the way to go. Switch statements allow for compact control flow structures by evaluating a single expression and executing code blocks based on a matched case.
In C#, we write a switch statement using the following syntax:
string color; switch (color){ case "blue": // execute if the value of color is "blue" Console.WriteLine("color is blue"); break; case "red": // execute if the value of color is "red" Console.WriteLine("color is red"); break; case "green": // execute if the value of color is "green" Console.WriteLine("color is green"); break; default: // execute if none of the above conditions are met break;}
14
Multiple Choice
What will be printed to the console when this code is executed?
<(`^´)>
true
expr
¯\_(ツ)_/¯
15
Multiple Choice
What’s wrong with this code?
There are no switch statements in C#
There are not enough cases
There are no break statements
num should be type string
16
Multiple Choice
What possible values can a bool variable take?
on and off
right and wrong
"true" and "false"
true and false
17
Multiple Choice
What will happen when this code is executed?
"x is greater than zero" is printed
"x is equal to zero" is printed
"x is less than zero" is printed
There will be an error
18
Multiple Choice
What will be printed to the console when this code is executed?
x is less than zero
Nothing will be printed
x is greater than zero
x is equal to zero
19

GuessTheNumberGame-1 - Replit
You can open this webpage in a new tab.
Lesson 3: Logic & Conditionals
Show answer
Auto Play
Slide 1 / 19
SLIDE
Similar Resources on Wayground
13 questions
Tutorial 5 : Security Controls and Policies (Lecture 5)
Presentation
•
University
13 questions
WEEK 5 Review Quiz
Presentation
•
University
15 questions
Quizzizz for Engaging Synchronous Lessons
Presentation
•
University
11 questions
Teenager 5 Session 22: Pronunciation & Speaking
Presentation
•
KG
15 questions
Programación Orientada a Objetos
Presentation
•
University
16 questions
Unidad 3
Presentation
•
University
16 questions
Present Perfect
Presentation
•
University
14 questions
Mastering CCNA: Interfaces and Cables
Presentation
•
University
Popular Resources on Wayground
10 questions
How much do you know about our Portrait of an Eagle?
Quiz
•
10th Grade
10 questions
Fast Food Slogans
Quiz
•
6th - 8th Grade
21 questions
Continents and Oceans
Quiz
•
6th Grade
20 questions
Parts of Speech
Quiz
•
5th Grade
16 questions
Subject & Predicate
Quiz
•
5th Grade
25 questions
Multiplication Facts
Quiz
•
5th Grade
12 questions
Map Skills
Quiz
•
3rd Grade
22 questions
Continents and Oceans
Quiz
•
5th Grade
Discover more resources for Computers
11 questions
NFL Football logos
Quiz
•
KG - Professional Dev...
20 questions
Disney Trivia
Quiz
•
University
10 questions
Identifying equations
Quiz
•
KG - University
16 questions
Water Modeling Activity
Presentation
•
11th Grade - University
20 questions
Logos
Quiz
•
KG - University
21 questions
Spanish-speaking Countries
Quiz
•
KG - University
36 questions
Unit 5 Key Terms
Quiz
•
11th Grade - University
21 questions
Mapa países hispanohablantes
Quiz
•
1st Grade - University