Search Header Logo
OOP Lesson 3

OOP Lesson 3

Assessment

Presentation

Computers

University

Practice Problem

Hard

Created by

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.

media

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:

media

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.

media

6

media

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.

media

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

Question image

What will be printed to the console when this code is executed?

1

<(`^´)>

2

true

3

expr

4

¯\_(ツ)_/¯

15

Multiple Choice

Question image

What’s wrong with this code?

1

There are no switch statements in C#

2

There are not enough cases

3

There are no break statements

4

num should be type string

16

Multiple Choice

What possible values can a bool variable take?

1

on and off

2

right and wrong

3

"true" and "false"

4

true and false

17

Multiple Choice

Question image

What will happen when this code is executed?

1

"x is greater than zero" is printed

2

"x is equal to zero" is printed

3

"x is less than zero" is printed

4

There will be an error

18

Multiple Choice

Question image

What will be printed to the console when this code is executed?

1

x is less than zero

2

Nothing will be printed

3

x is greater than zero

4

x is equal to zero

19

web page not embeddable

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