
OOP Lesson 3
Presentation
•
Computers
•
University
•
Practice Problem
•
Hard
Karen Ahmed
Used 2+ times
FREE Resource
14 Slides • 5 Questions
1
Object Oriented Programming
Lesson 3: Logic & Conditionals
2
Boolean Logic
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
Boolean Data Type
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
Comparison Operators
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
Logical Operators
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
If Statements
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
If...Else... Statements
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
Else If Statements
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
Switch Statements
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.
Object Oriented Programming
Lesson 3: Logic & Conditionals
Show answer
Auto Play
Slide 1 / 19
SLIDE
Similar Resources on Wayground
15 questions
Social network
Presentation
•
University
13 questions
La Evolución de las Tecnologías de la Información y Comunicación
Presentation
•
University
17 questions
Genetics Review Part 2 (Practice)
Presentation
•
6th - 9th Grade
14 questions
PRESENT SIMPLE
Presentation
•
University
13 questions
too, too much/many, enough
Presentation
•
University
13 questions
Present simple vs present continuous
Presentation
•
University
13 questions
Relative Pronouns
Presentation
•
University
12 questions
Anne Frank Character Change
Presentation
•
6th - 9th Grade
Popular Resources on Wayground
15 questions
Grade 3 Simulation Assessment 1
Quiz
•
3rd Grade
22 questions
HCS Grade 4 Simulation Assessment_1 2526sy
Quiz
•
4th Grade
16 questions
Grade 3 Simulation Assessment 2
Quiz
•
3rd Grade
19 questions
HCS Grade 5 Simulation Assessment_1 2526sy
Quiz
•
5th Grade
17 questions
HCS Grade 4 Simulation Assessment_2 2526sy
Quiz
•
4th Grade
20 questions
Equivalent Fractions
Quiz
•
3rd Grade
24 questions
HCS Grade 5 Simulation Assessment_2 2526sy
Quiz
•
5th Grade
20 questions
Math Review
Quiz
•
3rd Grade
Discover more resources for Computers
36 questions
8th Grade US History STAAR Review
Quiz
•
KG - University
25 questions
Spanish future tense
Quiz
•
10th Grade - University
55 questions
Post Malone Addtion (Tres)
Quiz
•
12th Grade - University
15 questions
Quotation Marks vs. Italics for MLA
Quiz
•
9th Grade - University
20 questions
Disney Trivia
Quiz
•
University
50 questions
AP Biology Exam Review 2017
Quiz
•
11th Grade - University
215 questions
8th Physical Science GA Milestones Review
Quiz
•
KG - University
20 questions
Ch15_review_TEACHER
Quiz
•
University