Decision Structures and Boolean Logic

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which programming construct executes a set of statements in the order they appear?

  • Sequence structure (correct)
  • Selection structure
  • Decision structure
  • Loop structure

What is the primary purpose of a decision structure in programming?

  • To sequentially execute statements
  • To define data types
  • To repeat a block of code
  • To perform actions only if a condition is met (correct)

In the context of an if statement, what does a diamond represent in a flowchart?

  • A true/false condition (correct)
  • Input or output operation
  • An action to be performed
  • The start or end of the program

When does a single alternative decision structure provide a path of execution?

<p>When the condition is true (A)</p> Signup and view all the answers

What happens in an if statement when the condition being tested is not true?

<p>The program exits the structure (B)</p> Signup and view all the answers

Which keyword is used to begin an if clause in Python?

<p>if (B)</p> Signup and view all the answers

Which of the following best defines a Boolean expression?

<p>An expression that evaluates to either true or false (D)</p> Signup and view all the answers

What is the purpose of a relational operator?

<p>To determine the relationship between two values (B)</p> Signup and view all the answers

Which operator checks if two operands are equal to each other?

<p>== (C)</p> Signup and view all the answers

What is the key distinction between the == operator and the = operator in many programming languages?

<p><code>==</code> compares two values, <code>=</code> assigns a value (C)</p> Signup and view all the answers

If balance == 0 is part of an if statement, what is being checked?

<p>If balance is equivalent to 0 (C)</p> Signup and view all the answers

In a nested if statement, how should the inner block of code be formatted?

<p>Indented with respect to the outer block (C)</p> Signup and view all the answers

Consider the following Python code:

i = 10 if (i > 15): print("10 is less than 15") print("I am Not in if")

What will be the output?

<p><code>I am Not in if</code> (B)</p> Signup and view all the answers

Which of the following statements is true about dual alternative decision structures?

<p>They allow one of two possible paths of execution. (B)</p> Signup and view all the answers

In an if-else statement, if the condition is true, what happens?

<p>The <code>if</code> block is executed. (A)</p> Signup and view all the answers

What is a key requirement regarding the if and else clauses in an if-else statement?

<p>They must be aligned, and statements must be consistently indented. (A)</p> Signup and view all the answers

Consider this code snippet:

if temperature < 40: print("A little cold, isn't it?") else: print("Nice weather we're having.")

If temperature is 50, what will be printed?

<p><code>Nice weather we're having.</code> (B)</p> Signup and view all the answers

When is it necessary to use a nested decision structure?

<p>When multiple conditions need to be checked sequentially, and the outcome of one condition affects the subsequent conditions (C)</p> Signup and view all the answers

A customer is eligible for a premium membership if they've spent over $1000 and visited the store more than 10 times. How would you structure this in code?

<p>Using a nested decision structure where the second condition is checked only if the first is true (A)</p> Signup and view all the answers

What is the primary reason that proper indentation is important within nested decision structures in Python?

<p>It affects the execution of the code, as it determines the code block associated with each <code>if</code> or <code>else</code> clause (D)</p> Signup and view all the answers

In the context of nested if statements, what can the if-elif-else structure simplify?

<p>The logic of multiple nested conditions (A)</p> Signup and view all the answers

Which statement about the if-elif-else structure is correct?

<p>It can include multiple <code>elif</code> statements to check different conditions. (A)</p> Signup and view all the answers

What alignment is typically used with an if-elif-else statement?

<p><code>if</code>, <code>elif</code>, and <code>else</code> clauses are all aligned. (B)</p> Signup and view all the answers

Under what circumstance might using a nested if-else structure be preferred over an if-elif-else statement?

<p>When the indentation complexity of <code>if-elif-else</code> becomes problematic. (A)</p> Signup and view all the answers

Write a Python program that checks if a number stored in a variable is greater than 50. If true, print "Number is greater than 50". Otherwise, nothing should happen. Which code is correct?

<pre><code class="language-python">num = 55 if num &gt; 50: print(&quot;Number is greater than 50&quot;) ``` (B) </code></pre> Signup and view all the answers

Write a Python program that checks if a room's temperature is less than 40°F. If it is, display "A little cold, isn't it?". Otherwise, display "Nice weather we're having.". Which code block is correct?

<pre><code class="language-python">temperature = 30 if temperature &lt; 40: print(&quot;A little cold, isn't it?&quot;) else: print(&quot;Nice weather we're having.&quot;) ``` (C) </code></pre> Signup and view all the answers

Write a Python program that takes an integer input from the user and determines whether it is positive, negative, or zero. If the input is positive, the program should print "Positive number". If the input is negative, it should print "Negative number". If the input is zero, it should print "Zero". Which of the following is a correct solution?

<pre><code class="language-python">num = int(input(&quot;Enter a number: &quot;)) if num &gt; 0: print(&quot;Positive number&quot;) elif num &lt; 0: print(&quot;Negative number&quot;) else: print(&quot;Zero&quot;) ``` (B) </code></pre> Signup and view all the answers

Rewrite the eligibility criteria for premium membership using a nested if statement. A customer is eligible for a premium membership at a shopping mall only if they meet both conditions:

  • They must have spent at least $1,000 in total purchases.
  • They must have visited the store at least 10 times in the last year.

Which accurately implements these criteria?

<pre><code class="language-python">total_spent = 1200 visits = 12 if total_spent &gt;= 1000: if visits &gt;= 10: print(&quot;Eligible for premium membership&quot;) ``` (D) </code></pre> Signup and view all the answers

Convert the following nested if statement into an if-elif-else structure. Determine the grade based on the student score.

score = 85
if score >= 90:
 print("A")
else:
 if score >= 80:
 print("B")
 else:
 if score >= 70:
 print("C")
 else:
 print("D")

Which code block is a correct if-elif-else version of the above nested 'if' statement?

<pre><code class="language-python">score = 85 if score &gt;= 90: print(&quot;A&quot;) elif score &gt;= 80: print(&quot;B&quot;) elif score &gt;= 70: print(&quot;C&quot;) else: print(&quot;D&quot;) ``` (C) </code></pre> Signup and view all the answers

Write a Python program that checks whether a student has passed or failed a course based on their exam scores. The program should first ask the user to enter the exam score.

  • If the score is greater than or equal to 60, the program should check if it's greater than or equal to 90. If so, it should print "Excellent! You've passed with distinction!".
  • If the score is between 60 and 90 (inclusive), it should print "Congratulations! You've passed!".
  • If the score is less than 60, it should print "Sorry, you've failed. Better luck next time!".

Which of the following code is correct?

<pre><code class="language-python">score = int(input(&quot;Enter the exam score: &quot;)) if score &gt;= 60: if score &gt;= 90: print(&quot;Excellent! You've passed with distinction!&quot;) else: print(&quot;Congratulations! You've passed!&quot;) else: print(&quot;Sorry, you've failed. Better luck next time!&quot;) ``` (C) </code></pre> Signup and view all the answers

Write a Python program to determine the discount a customer receives based on their total purchase amount.

  • If the purchase amount is greater than or equal to $100, the customer receives a 10% discount.
  • If the purchase amount is between $50 (inclusive) and $100 (exclusive), the customer receives a 5% discount.
  • If the purchase amount is less than $50, the customer receives no discount.
  • After determining the discount, the program should calculate and display the discounted amount and the final amount the customer needs to pay.

Which of the following code is correct?

<pre><code class="language-python">purchase_amount = float(input(&quot;Enter the total purchase amount: $&quot;)) if purchase_amount &gt;= 100: discount_rate = 0.10 elif 50 &lt;= purchase_amount &lt; 100: discount_rate = 0.05 else: discount_rate = 0.00 discount_amount = purchase_amount * discount_rate final_amount = purchase_amount - discount_amount print(&quot;Discounted amount: $&quot;, discount_amount) print(&quot;Final amount: $&quot;, final_amount) ``` (D) </code></pre> Signup and view all the answers

Flashcards

Control Structure

Logical design that controls the order in which a set of statements execute.

Sequence Structure

Set of statements that execute in the order they appear.

Decision Structure

Specific action(s) performed only if a condition exists.

Diamond Symbol (Flowchart)

It represents a true or false condition that must be tested in a flowchart.

Signup and view all the flashcards

If Clause

First line of an if statement, including the 'if' keyword

Signup and view all the flashcards

Boolean Expression

Expression tested by an if statement to determine if it is true or false.

Signup and view all the flashcards

Relational Operator

Determines whether a specific relationship exists between two values.

Signup and view all the flashcards

Single Alternative Decision Structure

Provides only one alternative path of execution.

Signup and view all the flashcards

Dual Alternative Decision Structure

Two possible paths of execution; run one if the condition is true, otherwise run the other.

Signup and view all the flashcards

Nested Decision Structure

A decision structure nested inside another decision structure.

Signup and view all the flashcards

If-Elif-Else Statement

Special version of a decision structure, simplifies nested decision structures.

Signup and view all the flashcards

Study Notes

  • Study notes on Decision Structures and Boolean Logic

The if Statement

  • This statement uses a control structure, which controls the order of execution of statements.
  • Sequence structure executes statements linearly, in the order they appear.
  • Decision structures execute specific actions when a condition is met.
  • This statement is also known as a selection structure.
  • A flowchart's diamond shape indicates a true or false condition to be tested.
  • Actions can be conditionally executed.
  • Actions are only performed if a condition is True.
  • Single alternative decision structure provides only one alternative path of execution.
  • If the condition is not True, the structure exits.
  • The first line of an if statement is known as the if clause, which includes the keyword "if" followed by a condition.
  • If the condition is True, the block statements execute
  • If the condition is False, the block statement is skipped.
  • The condition that is tested can be either True or False

Boolean Expressions and Relational Operators

  • Boolean expressions are tested by the if statement to determine its truth value.
  • Relational operators determine the relationship between two values.
  • a > b is True when a is greater than b, and False otherwise.
  • Operators like >=, and <= test for more than one relationship and only require one to exist for the expression to be True.
  • The == operator checks if two operands are equal.
  • Confusing == with the assignment operator = should be avoided.
  • The != operator checks if two operands are not equal.
  • Any relational operator can be used in a decision block.
  • It is possible to have a block inside another block
  • Statements in the inner block must be indented with respect to the outer block

Example: Conditional Statement in Python

  • i = 10 is assigned.
  • The if statement checks the condition i > 15.
  • Because 10 > 15 is False, the print statement in the if block is skipped.
  • The succeeding print outside the if block executes, printing "I am Not in if."

The if-else Statement

  • This statement is a dual alternative decision structure.
  • It provides two possible execution paths depending on whether condition is True or False.
  • "if" and "else" clauses must be aligned, and statements must be consistently indented for code to function.
  • If the if condition is True block of statements is executed and then the control jumps to the statement following the if-else statement
  • If the if condition is False, else block of statement is executed and then the control jumps to the statement following the if-else statement

Nested Decision Structures and the if-else Statement

  • A nested decision structure is when a decision structure is placed inside another.
  • Proper indentation is important in nested structures.
  • Indentation is important for the Python interpreter
  • Code is more readable for the programmer following indentation
  • Rules for writing nested if statements
  • "else" clauses should align with matching "if" clauses
  • Statements in each block must be consistently indented

The if-elif-else Statement

  • The if-elif-else statement is a special version of a decision structure that simplifies nested decision structures.
  • It can include multiple elif statements.
  • In "if-elif-else", the "if", "elif", and "else" clauses must be aligned.
  • Conditionally executed blocks are consistently indented for readability.
  • The statement is not required, but makes the logic is easier to follow rather than nested statements.
  • Coding can become complex, and indentation can cause problematic long lines.

Exercises Recap

  • Practice problems include creating Python programs based on conditional logic:
  • Checking number ranges.
  • Evaluating temperatures.
  • Determining number signs.
  • Grading systems using exam scores.
  • Calculating discounts based on purchase amounts.

Summary of Decision structures

  • Single alternative decision structures
  • Dual alternative decision structure
  • Nested decision structures
  • Relational operators and logical operators as used in creating Boolean expression

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Python Programming Fundamentals Quiz
36 questions
Boolean Logic and Algebra Quiz
22 questions
Excel Functions and Boolean Logic Quiz
15 questions
Control Statements and Loops Quiz
17 questions
Use Quizgecko on...
Browser
Browser