UNIT 3.3

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/11

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

12 Terms

1
New cards

What if you want to pick between two possibilities? If you are trying to decide between a couple of things to do, you might flip a coin and do one thing if it lands as heads and another if it is tails. In programming, you can use the if keyword followed by a statement or block of statements and then the else keyword also followed by a statement or block of statements.

2
New cards

// A block if/else statementif (boolean expression) { statement1; statement2; } else{ do other statement; and another one; }

3
New cards

// A single if/else statement if (boolean expression) Do statement; else Do other statement;

4
New cards

The else will only execute if the condition is false.

5
New cards

If/else statements can also be used with relational operators and numbers like below. If your code has an if/else statement, you need to test it with 2 test-cases to make sure that both parts of the code work.

6
New cards

If statements can be nested inside other if statements. Sometimes with nested ifs we find a dangling else that could potentially belong to either if statement. The rule is that the else clause will always be a part of the closest unmatched if statement in the same block of code, regardless of indentation.

7
New cards

// Nested if with dangling elseif (boolean expression) if (boolean expression) Do statement; else // belongs to closest if Do other statement;

8
New cards
  • If statements can be followed by an associated else part to form a 2-way branch:

**if** (boolean expression) { Do statement; } **else**{ Do other statement; }

9
New cards

A two way selection (if/else) is written when there are two sets of statements: one to be executed when the Boolean condition is true, and another set for when the Boolean condition is false.

10
New cards

The body of the “if” statement is executed when the Boolean condition is true, and the body of the “else” is executed when the Boolean condition is false.

11
New cards

Use 2 test-cases to find errors or validate results to try both branches of an if/else statement.

12
New cards

The else statement attaches to the closest unmatched if statement in the same block of statements.