Study guide

8.1 Sequence and Selection: if, else, Chained and Nested Conditions

Praxis Computer Science 5652

Track your progress as you read

Finished sections get a check mark.

1 Chapter 1: Praxis 5652 Exam Overview and ETS Pseudocode

1.1 Praxis 5652 Test Facts, Scoring, and Registration 1.2 Official ETS Pseudocode Notation and Tracing Strategy

2 Chapter 2: Impacts of Computing: Innovation, Access, and Effects

2.1 Computing for Creativity, Problem Solving, Communication, and Innovation 2.2 Equal Access, the Digital Divide, and Accessibility 2.3 Beneficial and Harmful Effects of Computing Innovations

3 Chapter 3: Impacts of Computing: Intellectual Property, Ethics, Privacy, and Security

3.1 Intellectual Property: Copyright, Open Source, and Creative Commons 3.2 Ethical and Unethical Computing Practices and Their Implications 3.3 Privacy, Security, Data Collection, and Local vs. Cloud Storage

4 Chapter 4: Algorithms and Computational Thinking: Abstraction, Decomposition, Number Bases, and Representation

4.1 Abstraction and Abstraction Hierarchies 4.2 Decomposition, Pattern Recognition, and Developing Algorithms 4.3 Number Bases: Binary, Octal, Decimal, and Hexadecimal 4.4 Algorithm Formats: Natural Language, Flowcharts, Pseudocode, and Tracing

5 Chapter 5: Algorithms: Efficiency, Limits of Computing, and Randomization

5.1 Algorithm Efficiency: Linear, Quadratic, Exponential, and Logarithmic Growth 5.2 Limits of Computing: Unsolvable Problems, Intractability, and Heuristics 5.3 Randomization, Random vs. Pseudorandom Numbers, and Scaling to a Range

6 Chapter 6: Algorithms: Searching, Sorting, and Recursion

6.1 Searching Algorithms: Linear Search and Binary Search 6.2 Sorting Algorithms: Tracing, Comparisons, Correctness, and Efficiency 6.3 Recursion: Base Cases, Tracing, Errors, and Iterative Equivalents

7 Chapter 7: Programming: Variables, Data Types, Strings, and Expressions

7.1 Variables, Data Types, and Type Conversion 7.2 Strings and String Operations 7.3 Operators, Precedence, Expressions, and Formulas 7.4 Boolean Expressions, Truth Tables, and Boolean Algebra

8 Chapter 8: Programming: Selection, Iteration, and Writing Code

8.1 Sequence and Selection: if, else, Chained and Nested Conditions 8.2 Iteration: for, while, do … while, and repeat … until Loops 8.3 Reading, Completing, and Modifying Code

9 Chapter 9: Programming: Procedures, Arrays, and Data Structures

9.1 Procedures, Parameters, Return Values, Nested Calls, and Scope 9.2 Arrays, Lists, and Two-Dimensional Arrays 9.3 Stacks, Queues, and Dictionaries (Maps)

10 Chapter 10: Programming: Correctness, Errors, Testing, and Code Quality

10.1 Program Correctness: Preconditions, Postconditions, Invariants, and Testing vs. Proof 10.2 Error Types and Debugging Techniques 10.3 Choosing Useful Test Cases: Boundaries, Partitions, Coverage, and Regression 10.4 Extensibility, Modifiability, and Reusability: Constants and Parameterization 10.5 Documentation, Code Reviews, Peer Feedback, and End-User Feedback

11 Chapter 11: Programming: Events, Usability, Input Validation, and APIs

11.1 Event-Driven Programming, Asynchronous Events, and Concurrency 11.2 Usability, Meaningful Error Messages, and Input Validation vs. Verification 11.3 Libraries and APIs: Reading Definitions, Making Calls, and Weighing Trade-offs

12 Chapter 12: Programming: Languages, Tools, Paradigms, and Object-Oriented Design

12.1 Low- and High-Level Languages, Compilers and Interpreters, and IDEs 12.2 Programming Paradigms: Procedural, Object-Oriented, and Others 12.3 Classes, Objects, Instance Variables, Methods, and Encapsulation 12.4 Inheritance, Polymorphism, and Overloading vs. Overriding

13 Chapter 13: Data: Bits, Encoding, Compression, and Encryption

13.1 Bits and Bytes: Why Binary, Storage Calculations, and Signed Integers 13.2 Encoding Text, Images, and Sound 13.3 Lossless and Lossy Compression: Run-Length Encoding, Huffman Coding, and Media Formats 13.4 Encoding vs. Encryption, Symmetric and Public-Key Encryption, and Their Trade-offs

14 Chapter 14: Data: Analysis Tools, Simulation, Storage, and Collection

14.1 Analyzing Data with Spreadsheets: Cleaning, Transforming, and Visualizing 14.2 Simulation and Modeling 14.3 Files, Databases, and Storage Requirements: Scale, Redundancy, and Backup 14.4 Collecting, Aggregating, and Generating Data: Public Data Sets, Surveys, Sensors, and Crowdsourcing

15 Chapter 15: Computing Systems: Hardware, Operating Systems, and Computing Environments

15.1 Hardware Components and the Fetch–Decode–Execute Cycle 15.2 Operating Systems, Software Components, and Resource Issues 15.3 Embedded Systems, the Internet of Things, and Types of Computing Systems 15.4 Local, Network, and Cloud Computing and Storage; How Binary Data Are Stored

16 Chapter 16: Networks, the Internet, the Web, and Cybersecurity

16.1 Network Types and Wired and Wireless Communication 16.2 Network Hardware and Layered Network Models (OSI and TCP/IP) 16.3 Network Performance: Bandwidth, Latency, Load, and Estimating Needs 16.4 How the Internet Works: Protocols, IP Addressing, DNS, URLs, and Reliability 16.5 The Web: HTTP, HTML and Markup Languages, Browsers, Servers, and Clients 16.6 Cybersecurity: The Five Pillars, Strong Passwords, Security Strategies, and Trade-offs

8.1 Sequence and Selection: if, else, Chained and Nested Conditions

Key Takeaways Explain This Topic AI

Every algorithm can be built from three constructs: sequence (steps in order), selection (choosing a path with if/else), and iteration (repeating with loops).

In an if / else if chain, conditions are tested from the top, and only the first true branch runs; checking score ≥ 70 before score ≥ 90 makes the "A" branch unreachable.

A nested if runs its inner test only when the outer test is true, which is equivalent to joining the two conditions with and.

ETS pseudocode closes each selection with end if, and its indentation is significant, so an else belongs to the if it lines up with.

In C-family languages, a switch case without break falls through into the next case.

Last updated: September 2026

What this competency asks

ETS asks you to understand the three basic constructs used in programming: sequence, selection, and iteration . Besides tracing code, finding inputs for given outputs, describing a segment's purpose, and supplying missing code, you should be able to:

Identify the three constructs when used in code.

Identify which constructs are needed to implement given functionality.

Convert code that does not use iteration into equivalent code that does (Section 8.2).

The discussion questions add: trace code that uses selection (if, if/else, switch, case) and convert from one type of selection to another .

The three constructs

  • Construct Meaning Recognize it by
  • Sequence Statements run one after another, in order Consecutive statements with no branching
  • Selection The program chooses which statements to run based on a condition if , else , switch / case
  • Iteration Statements repeat while or until a condition holds for , while , do … while , repeat … until

Which constructs does a task need? Ask two questions:

Does the program make a decision? If so, it needs selection .

Does it repeat an action an unknown or large number of times? If so, it needs iteration .

Every program uses sequence. "Print the larger of two numbers" needs sequence and selection. "Print the numbers 1 to 100" needs iteration. "Count the passing scores in a list" needs all three: a loop over the list and an if for each score.

  • One-way and two-way selection if ( temperature > 100 )
  • print "Warning: overheating"
  • end if
  • if ( balance ≥ amount )
  • balance ← balance - amount
  • else
  • print "Insufficient funds"
  • end if

A one-way if may do nothing. An if … else always runs exactly one of its two blocks.

  • Chained selection: order matters
  • When there are several mutually exclusive cases, nest the next if inside the else : if ( score ≥ 90 )
  • grade ← "A"
  • else
  • if ( score ≥ 80 )
  • grade ← "B"
  • else
  • if ( score ≥ 70 )
  • grade ← "C"
  • else
  • grade ← "F"
  • end if
  • end if
  • end if

The tests run top to bottom , and the first true test wins. With score ← 85 , the first test fails, the second succeeds, and grade becomes "B"; the remaining tests never run.

  • Shadowed conditions. Reverse the order and the logic breaks: if ( score ≥ 70 )
  • grade ← "C" // 85 and 95 both land here
  • else
  • if ( score ≥ 80 )
  • grade ← "B" // unreachable: any score ≥ 80 was already ≥ 70
  • end if
  • end if

For lower-bound tests (≥), check the largest threshold first. For upper-bound tests (<), check the smallest threshold first. Alternatively, use explicit ranges, as in ( score ≥ 80 ) and ( score < 90 ) .

  • Nested selection
  • A nested if is tested only when the enclosing condition is true: int x ← 7
  • String msg ← ""
  • if ( x % 2 == 0 )
  • msg ← "even"
  • else
  • if ( x > 5 )
  • msg ← "big odd"
  • end if
  • msg ← msg + "!"
  • end if
  • print msg

x is odd, so the else block runs. Inside it, 7 > 5 is true, so msg becomes "big odd". Then msg ← msg + "!" runs regardless of that inner test, because it sits in the else block, not inside the inner if . The output is big odd! . Indentation and end if tell you exactly where each statement belongs.

Why end if matters: the dangling else

In languages that allow an if without braces, such as C and Java, an else binds to the nearest unmatched if , whatever the indentation suggests: if (x > 0)

  • if (y > 0)
  • System.out.println("both");
  • else
  • System.out.println("x not positive"); // actually pairs with if (y > 0)

With x = −3, nothing prints. The else belongs to the inner if , which is never reached. ETS pseudocode avoids this ambiguity: indentation and end if are significant , so an else belongs to the if it lines up with. When you trace code on the test, follow the end if markers. In Java or C, use braces.

Converting between forms of selection

Nested ifs to a compound condition. An outer test and an inner test that must both pass are equivalent to and : if ( ( loggedIn ) and ( isAdmin ) )

  • showAdminPanel ( )
  • end if

Guard clauses. A deeply nested chain of checks can often be flattened by handling failure cases first and returning early. This makes the main path easier to read.

switch/case to if/else. Many languages offer a switch statement that compares one value against constants: switch (day) {

  • case 1: label = "Mon"; break;
  • case 2: label = "Tue"; break;
  • default: label = "Other";
  • }
  • Equivalent ETS-style selection: if ( day == 1 )
  • label ← "Mon"
  • else
  • if ( day == 2 )
  • label ← "Tue"
  • else
  • label ← "Other"
  • end if
  • end if

In C, C++, and Java, leaving out break makes execution fall through into the next case. Sometimes this is intentional (grouping cases), but often it is a bug.

Common selection pitfalls

Assignment instead of comparison: use == to compare. In C, if (x = 5) assigns 5 and is always true.

Boundary errors: age < 18 versus age ≤ 18 changes the result for exactly one value, 18. Test values at, just below, and just above each boundary (Section 10.3).

Floating-point equality: avoid == with computed doubles; compare within a tolerance.

Impossible conditions: ( x < 10 ) and ( x > 20 ) can never be true.

Missing else : a variable assigned in only some branches may keep an old value. ETS's sample test-case question turns on exactly this: a code segment meant to store the largest of three numbers assigns nothing in one branch.