Conditionals Flashcards

1
Q

If Statements

A
  • Used to make decisions in our program.
  • If Condition: the if statement is used to execute a block of code if a specified condition is true.
  • An if statement includes a condition and one or more statements that form the body of the if statement.
    ex.) if (conditional expression):
    body
  • The conditional expression is evaluated, if it evaluates to True then the body of the if statement executes, followed by the rest of the statements in the program.
  • If the if statement’s condition evaluates to False then the body of the if statement is skipped and execution continues at the first line after the body of the if statement.
  • An if statement’s condition often includes a relational operator that compares two values, variables, or complex expressions.
  • The body must be indented
  • Semicolon after condition
  • Number of indented spaces must be consistent in a block.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Mutually exclusive

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

If-Else Statements

A
  • The if-else statement executes one block of code if a condition is true, and another block if it’s false.
  • Consists of an if part with a condition and a body, and an else part with a body (but no condition).

Format:
if (conditional expression):
body of ‘if’ - when expression is True
else:
body of ‘else’ - when expression is False

remainder of program

  • When the statement executes its condition is evaluated.
    If the condition evaluates to True then the body of the if part executes and the body of the else part is skipped.
  • If the condition evaluates to False the body of the if part is skipped and the body of the else part executes.
  • Impossible for both bodies to execute.
  • Impossible to skip both bodies.
    When to use if-else statement: when only one condition needs to be written and evaluated.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If-Elif-Else Statements

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If-Elif Statements

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Nested If Statements

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Boolean Expression

A
  • Conditionals are questions with answers that are either True or False.
  • Variable with only values of True or False, is called a Boolean type.
  • Known as bool (short for boolean).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Logical (Boolean) Operators

A

Python includes three Boolean operators: not, and, and or.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

And Operator

A

a and b: Output is True only when a and b are both True.

  • If a is False, or b is False, or both a and b are False then a and b evaluates to False.
  • Two statements are True.

ex.) not snowing today, temperature is not freezing.

  • Same thing as asking: a == true and b === true.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Or Operator

A
  • a or b: Output True when at least one of a and b are True.
  • Evaluates to True if a is True, or if b is True, or if both a and b are True.
  • Evaluates to False if both a and b are False.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Not Operator

A
  • The not operator reverses the truth of a Boolean expression. If the expression a, evaluates to True, then not a evaluates to False.
  • If a evaluates to False then not a evaluates to True.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Truth Tables

A
  • A truth table is a mathematical table used in logic and computer science to represent the possible truth values of logical expressions.
  • The behavior of any Boolean expression can be described by a truth table.
  • Has one column for each distinct variable in the Boolean expression, and a column for the expression itself.
  • Each row in the truth table represents one combination of True and False values for the variables in the expression.

A truth table for an expression having n distinct variables has 2^n rows, each of which shows the result computed by the expression for a different combination of values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly