Skip to main content
Indietro

Decision and Repetition Structures in Programming

Guida di studio - Note intelligenti

Appunti personalizzati basati sui tuoi materiali, ampliati con definizioni chiave, esempi e contesto.

Decision Structures and Boolean Logic

The if Statement

The if statement is a fundamental control structure in programming that allows a program to execute certain statements only when a specified condition is true. This enables the creation of multiple paths of execution within a program, enhancing its flexibility and decision-making capabilities.

  • Control structure: Logical design that determines the order in which statements execute.

  • Sequence structure: Statements execute in the order they appear, from top to bottom.

  • Decision structure: Executes specific actions only if a condition exists (also called selection structure).

  • In flowcharts, a diamond shape represents a true/false condition that must be tested.

A simple decision structure flowchart

Python Syntax Example:

if condition: statement statement

The condition is evaluated; if true, the indented block executes. Otherwise, it is skipped.

Boolean Expressions and Relational Operators

A Boolean expression is an expression that evaluates to either True or False. These are commonly used in decision structures to determine which path of execution to follow. Relational operators are used to compare values within Boolean expressions.

  • Relational operators: >, <, >=, <=, ==, !=

  • Example: a > b is true if a is greater than b.

Expression

Meaning

x > y

Is x greater than y?

x < y

Is x less than y?

x >= y

Is x greater than or equal to y?

x <= y

Is x less than or equal to y?

x == y

Is x equal to y?

x != y

Is x not equal to y?

Boolean expressions using relational operators

Example Decision Structures

Decision structures can be visualized using flowcharts. For example, a program may award a bonus if sales exceed a certain threshold.

Example decision structure flowchart

Conditional Execution: if-else Statement

The if-else statement provides two alternative paths of execution. If the condition is true, one block of statements executes; if false, another block executes.

if condition: statements else: other statements

Conditional execution in an if-else statement

Comparing Strings

Strings can be compared using relational operators. Comparisons are case-sensitive and are performed character by character based on ASCII values.

  • == and != compare equality and inequality.

  • >, <, >=, <= compare lexicographical order.

  • ASCII values determine the order: e.g., 'y' (121) > 'k' (107).

ASCII comparison of strings 'Mary' and 'Mark'

Repetition Structures

Introduction to Repetition Structures

Repetition structures, or loops, allow a program to execute a block of code multiple times. This avoids code duplication and makes programs more efficient and easier to maintain.

  • Condition-controlled loops: Repeat as long as a condition is true (e.g., while loop).

  • Count-controlled loops: Repeat a specific number of times (e.g., for loop).

The while Loop: Condition-Controlled Loop

The while loop repeats a block of statements as long as a specified condition remains true. It is known as a pretest loop because the condition is evaluated before each iteration.

while condition: statements

The logic of a while loop

Flowchart Example: while Loop

Flowchart for a while loop program

Calculating a Running Total

Programs often need to calculate the sum of a series of numbers. This is typically done using an accumulator variable within a loop.

  • Initialize the accumulator to 0.

  • For each number, add it to the accumulator.

  • At the end, the accumulator holds the total.

Logic for calculating a running total

Augmented Assignment Operators

Augmented assignment operators provide a shorthand way to update the value of a variable.

Operator

Example Usage

Equivalent To

+=

x += 5

x = x + 5

-=

y -= 2

y = y - 2

*=

z *= 10

z = z * 10

/=

a /= b

a = a / b

%=

c %= 3

c = c % 3

Augmented assignment operators table

Input Validation Loops

Input validation ensures that only valid data is processed by the program. A priming read is often used to get the first input before entering the validation loop.

  • If input is invalid, display an error and prompt again.

  • If input is valid, continue processing.

Logic containing an input validation loop

Nested Loops

A nested loop is a loop inside another loop. The inner loop completes all its iterations for each iteration of the outer loop. This is useful for tasks such as simulating clocks or processing multi-dimensional data.

Flowchart for a clock simulation

Summary

  • Decision structures allow programs to choose between alternative paths based on conditions.

  • Boolean expressions and relational operators are essential for making decisions in code.

  • Repetition structures (loops) enable efficient repetition of code blocks.

  • Input validation and accumulator patterns are common in robust programs.

  • Understanding and using these structures is fundamental to programming logic and algorithm design.

Pearson Logo

Study Prep