When you write an if then else statement Under what circumstances do the statements that appear between the else clause and the end if clause execute?

If...Then...Else Statement

The If...Then...Else statement allows conditional execution based on the evaluation of an expression.

There are two ways to write an If...Then...Else statement:

Single-Line

Block

Single-Line

Description

The single-line form of If...Then...Else is often useful for short, simple conditional tests.

Syntax

If condition Then thenpart [Else elsepart]

Parts:

condition - A numeric or string expression that evaluates true (nonzero) or false (0 or Null).

thenpart, elsepart - Statements or branches performed when condition is True (thenpart) or False (elsepart).

The thenpart is executed if condition is true; if condition is false, then elsepart is executed. If the Else clause is not present, control passes to the next statement in the program.

You can have multiple statements within a condition, but they must be on the same line and separated by colons.

Example

If A > 10 Then A = A + 1 : B = B + A : C = C + B Else A = A + 2

Block

Description

The block form of If...Then...Else provides more structure and flexibility than the single-line form and is usually easier to read, maintain, and debug.

Syntax

If condition1 Then
[statementblock1]
[ElseIf condition2 Then
[statementblock2] ]
[Else
[statementblock3] ]
End If

Parts:

condition1, condition2 - The same conditions as used in the single-line form.

statementblock1 to 3 - One or more statements on one or more lines.

Remarks

In executing a block If, the map tests condition1, the first numeric expression. If the expression is true, the statements following Then are executed. If the first expression is false, the map begins evaluating each ElseIf condition in turn. When the map finds a true condition, the statements following the corresponding Then are executed. If none of the ElseIf conditions is true, the statements following Else are executed. After executing the statements following Then or Else, the program continues with the statement following End If.

The Else and ElseIf blocks are both optional. You can have as many ElseIf clauses as you like in a block If, but none can appear after an Else clause. Any of the statement blocks can contain nested block If statements.

The map looks at what appears after the Then reserved word to determine whether or not an If statement is a block If. If anything other than a comment appears after Then, the statement is treated as a single-line If statement.

A block If statement must be the first statement on a line. The Else, ElseIf, and End If parts of the statement can have only a line number or line label in front of them. The block must end with an End If statement.

Use the If… Then… Else statement to define two blocks of statements. One of the statements runs when the specified condition is True, and the other one runs when the condition is False. When you want to define more than two blocks of statements, use the ElseIf Statement.

You can nest up to ten levels of If... Then... Else statements. If you need to create an expression with more than ten levels, you must redefine it using the ElseIf statement or the Select Case...End Case Statement.

Example

This example tests the length of a field after spaces have been removed to determine if the field contains data. If the source field contains no data, a string is placed in the target field that says No Data Available. If there is data in the source field, the data is placed in the target field.

If Len(Trim(FieldAt("/SOURCE/R1/Field1")))=0 Then
  "No Data Available"
Else
  FieldAt("/SOURCE/R1/Field1")
End If

The if statement is used to create a(n) ________ _________, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a Boolean expression is true.

The if statement is used to create a decision structure, which allows a program to have more than one ___________________. The if statement causes one or more statements to execute only when a Boolean expression is true.

The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a Boolean expression is _____.

The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a(n) ________ expression is true.

A(n) ________ _________ is a logical design that controls the order in which a set of statements execute.

What is a control structure?

A control structure is a logical design that controls the order in which a set of statements execute.

A(n) __________ __________ is a set of statements that execute in the order that they appear.

What is a sequence structure?

A sequence structure is a set of statements that execute in the order that they appear.

Decision structures are also known as __________ ___________.

In a(n) _________ __________’s simplest form, a specific action is performed only if a certain condition exists. If the condition does not exist, the action is not performed.

A(n) _______ ___________ _________ __________ provides only one alternative path of execution.

single alternative decision structure

The first line of an if statement is known as the _____________.

The if clause begins with the word if, followed by a condition, which is an expression that will be evaluated as either true or false. A(n) ______ appears after the condition. Beginning at the next line is a block of statements. All of the statements in a block must be consistently indented. This indentation is required because the Python interpreter uses it to tell where the block begins and ends.

When the if statement executes, the _________ is tested. If the _________ is true, the statements that appear in the block following the if clause are executed. If the _________ is false, the statements in the block are skipped.

When the ___ __________ executes, the condition is tested. If the condition is true, the statements that appear in the block following the if clause are executed. If the condition is false, the statements in the block are skipped.

When the if statement executes, the condition is tested. If the condition is true, the statements that appear in the ______ following the if clause are executed. If the condition is false, the statements in the ______ are skipped.

The if statement tests an expression to determine whether it is true or false. The expressions that are tested by the if statement are called Boolean expressions, named in honor of the English mathematician _______ ______.

In the 1800s _______ ______ invented a system of mathematics in which the abstract concepts of true and false can be used in computations.

Typically, the Boolean expression that is tested by an if statement is formed with a(n) __________ ________. A(n) __________ ________ determines whether a specific relationship exists between two values.

A(n) _______ statement will execute one block of statements if its condition is true, or another block if its condition is false.

A(n) _____ ___________ _________ _________ has two possible paths of execution—one path is taken if a condition is true, and the other path is taken if the condition is false. 

dual alternative decision structure

The if-else statement forms a(n) ________________.

dual alternative decision structure

Python allows you to ________ strings. This allows you to create decision structures that test the value of a string.

String comparisons are _____ __________. For example, the strings 'saturday' and 'Saturday' are not equal because the "s" is lowercase in the first string, but uppercase in the second string.

In addition to determining whether strings are equal or not equal, you can also determine whether one string is _________________________ another string. This is a useful capability because programmers commonly need to design programs that sort strings in some order.

greater than or less than

The uppercase ASCII characters A through Z are represented by the numbers (a) through (b).

The lowercase ASCII characters a through z are represented by the numbers (a) through (b).

When the digits 0 through 9 are stored in memory as characters, they are represented

by the numbers (a) through (b).

A blank space is represented by the number ___.

In addition to establishing a set of numeric codes to represent characters in memory, ASCII also establishes a(n) ______ for characters. The character “A” comes before the character “B”, which comes before the character “C”, and so on.

When a program compares characters, it actually compares the ______ ______ for the characters.

When you use relational operators to compare strings, the strings are compared ________________________, beginning with the first, or leftmost, characters.

When you use relational operators to compare strings, the strings are compared character-by-character, beginning with the _

(a)

__, or ___

(b)

___, characters.

If one of the strings in a comparison is shorter than the other, only the _____________ characters will be compared. If the _____________ characters are identical, then the shorter string is considered less than the longer string.

If one of the strings in a(n) ____________ is shorter than the other, only the corresponding characters will be compared. If the corresponding characters are identical, then the shorter string is considered less than the longer string.

To test more than one condition, a decision structure can be _______ inside another decision structure.

The logic of the nested decision structure is fairly complex. Python provides a special version of the decision structure known as the ___________ statement, which makes this type of logic simpler to write.

The logic of an if-elif-else statement is usually easier to follow than a long series of nested if-else statements. And, because all of the clauses are ________ in an if-elif-else statement, the lengths of the lines in the statement tend to be shorter.

The logic of an if-elif-else statement is usually easier to follow than a long series of nested if-else statements. And, because all of the ________ are aligned in an if-elif-else statement, the lengths of the lines in the statement tend to be shorter.

The logical _(a)_ operator and the logical _(b)_ operator allow you to connect multiple Boolean expressions to create a compound expression.

The logical and operator and the logical or operator allow you to connect multiple Boolean expressions to create a __________ Boolean expression.

The logical ____ operator reverses the truth of a Boolean expression.

Python provides a set of operators known as _________ operators, which you can use to create complex Boolean expressions.

Python provides a set of operators known as logical operators, which you can use to create complex ________ expressions.

This operator is a unary operator, meaning it works with only one operand. The operand must be a Boolean expression. This operator reverses the truth of its operand. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true.

This operator connects two Boolean expressions into one compound expression. Both subexpressions must be true for the compound expression to be true.

This operator connects two Boolean expressions into one compound expression. One or both subexpressions must be true for the compound expression to be true. It is only necessary for one of the subexpressions to be true, and it does not matter which.

Both the and and or operators perform ______-_______ evaluation.

If the expression on the left side of the _____ operator is false, the expression on the right side will not be checked. Because the compound expression will be false if only one of the subexpressions is false, it would waste CPU time to check the remaining expression. So, when the _____ operator finds that the expression on its left is false, it short-circuits and does not evaluate the expression on its right.

If the expression on the left side of the ___ operator is true, the expression on the right side will not be checked. Because it is only necessary for one of the expressions to be true, it would waste CPU time to check the remaining expression.

When determining whether a number is inside a range, it is best to use the ____ operator.

A Boolean variable can reference one of two values: _(a)__ or __(b)__. Boolean variables are commonly used as flags, which indicate whether specific conditions exist.

In addition to the int, float, and str (string) data types, Python also provides a(n) _____ data type. The _____ data type allows you to create variables that may reference one of two possible values: True or False.

Boolean variables are most commonly used as ______.

A(n) _____ is a variable that signals when some condition exists in the program. When the _____ variable is set to False, it indicates the condition does not exist. When the _____ variable is set to True, it means the condition does exist.

What is a control structure?

A logical design that controls the order in which a set of statements execute

What is a decision structure?

It is a program structure that can execute a set of statements only under certain circumstances.

What is a single alternative decision structure?

A decision structure that provides a single alternative path of execution. If the condition that is being tested is true, the program takes the alternative path.

What is a Boolean expression?

An expression that can be evaluated as either true or false

What types of relationships between values can you test with relational operators?

You can determine whether one value is greater than, less than, greater than or equal to, less than or equal to, equal to, or not equal to another value.

Write an if statement that assigns 0 to x if y is equal to 20.

Write an if statement that assigns 0.2 to commissionRate if sales is greater than or equal to 10000.

if sales >= 10000:

    commissionRate = 0.2

How does a dual alternative decision structure work?

A dual alternative decision structure has two possible paths of execution; one path is taken if a condition is true, and the other path is taken if the condition is false.

What statement do you use in Python to write a dual alternative decision structure?

When you write an if-else statement, under what circumstances do the statements that appear after the else clause execute?

When the condition is false

What is a compound Boolean expression?

It is an expression that is created by using a logical operator to combine two Boolean subexpressions.

Explain how short-circuit evaluation works with the and and or operators.

The and operator: If the expression on the left side of the and operator is false, the expression on the right side will not be checked. The or operator: If the expression on the left side of the or operator is true, the expression on the right side will not be checked.

Write an if statement that displays the message “The number is valid” if the value

referenced by speed is within the range 0 through 200.

if speed >= 0 and speed <= 200     print('The number is valid')

Write an if statement that displays the message “The number is not valid” if the value referenced by speed is outside the range 0 through 200.

if speed < 0 or speed > 200     print('The number is not valid')

What values can you assign to a bool variable?

A variable that signals when some condition exists in the program

A(n) __________ structure can execute a set of statements only under certain circumstances.

A(n) ________ ___________ _________ structure provides one alternative path of execution.

single alternative decision

A(n) __________ expression has a value of either true or false.

The symbols >, < and == are all ___________ operators.

A(n) _____ ___________ _________ structure tests a condition and then takes one path if the condition is true, or another path if the condition is false.

dual alternative decision

You use a(n) _____ statement to write a single alternative decision structure.

You use a(n) ________ statement to write a dual alternative decision structure.

and, or, and not are ________ operators.

A compound Boolean expression created with the _____ operator is true only if both of its subexpressions are true.

A compound Boolean expression created with the ___ operator is true if either of its subexpressions is true.

The ____ operator takes a Boolean expression as its operand and reverses its logical value.

A(n) ______ is a Boolean variable that signals when some condition exists in the program.

True or false?

You can write any program using only sequence structures.


True or false?

A program can be made of only one type of control structure. You cannot combine structures.

True or false?

A single alternative decision structure tests a condition and then takes one path if the condition is true, or another path if the condition is false.

True or false?

A decision structure can be nested inside another decision structure.

True or false?

A compound Boolean expression created with the and operator is true only when both subexpressions are true.

How is if statement different from if else statement?

The if/else statement With the if statement, a program will execute the true code block or do nothing. With the if/else statement, the program will execute either the true code block or the false code block so something is always executed with an if/else statement.

What is the function of Else clause in an if statement?

The else clause is the inline clause for the if statement to execute when it evaluates to false. Expression to be tested. If it evaluates to nonzero, it is considered true. If it evaluates to 0, it is false.

What is an example of an if/then else statement?

The if / then statement is a conditional statement that executes its sub-statement, which follows the then keyword, only if the provided condition evaluates to true: if x < 10 then x := x+1; In the above example, the condition is x < 10 , and the statement to execute is x := x+1 .

How many statements can be in the true branch of an if/then else statement?

Use the If… Then… Else statement to define two blocks of statements. One of the statements runs when the specified condition is True, and the other one runs when the condition is False. When you want to define more than two blocks of statements, use the ElseIf Statement.