Basics of Programming through Python Conditional Statements

Basics of Programming through Python Conditional Statements

Introduction

  • Course: COMP105

  • Term: 2025-2026

  • University: Imam Abdulrahman Bin Faisal University

  • Department: Deanship of Preparatory Year and Supporting Studies

Learning Objectives

  • Understand the concept and usage of selection structure and conditional statements.

  • Know various types of conditional statements available in Python:

    • if

    • elif

    • Nested if

  • Handling exceptions using:

    • try

    • except

    • else

    • finally

  • Analyze problems, decide, and evaluate conditions.

Structured Programming

  • Structured programming is a fundamental feature of programming languages consisting of:

    1. SEQUENCE

    2. SELECTION

    3. ITERATION OR LOOPING

Selection Structure

  • A selection statement causes the program control to transfer to a specific flow based on whether a certain condition is true.

  • Conditional Statements:

    • Used to control the flow of the program.

    • Perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false.

    • Types of conditional statements in Python:

    • if

    • elif

    • else

Conditional Execution: The if Statement

  • The simplest form of a conditional statement is the if statement, which is a one-selection statement.

    • Example:
      if x > 0: print('x is positive')

  • Logic of if statement:

Usage of if Statements

  • An if statement in Python is used for decision-making operations and has the following structure:

    • Syntax:
      if condition: statement_1_True else: statement_False

Alternative Execution: if-else Statement

  • In an if-else structure, there are two possibilities:

    • The condition determines which block of code gets executed.

    • Example:
      if x % 2 == 0: print('x is even') else: print('x is odd')

Important Notes on if Statements

  • Important syntax rules:

    • The colon (:) after the if statement is essential.

    • The header of the if statement is isolated from the body.

    • All lines indented after the colon are executed when the Boolean expression is valid.

Code Blocks in Python

  • Code blocks are sets of statements executed together, one after another.

  • Includes:

    • if statements

    • for loops

    • while loops

    • Functions

  • Example of a code block:

if-else Examples

Example 1

  • Code:

  age = 15
  if age >= 18:
      print("Eligible for Voting")
  else:
      print("Not Eligible for Voting")
  print("Statement after if statement")
  • Output:

  Not Eligible for Voting
  Statement after if statement

Example 2

  • Question: Find the output of this code:

  A = 15
  B = 20
  if B > A:
      print("B is greater than A")
  else:
      print("A is greater than B")
  • Output:
    B is greater than A

Using elif Statements

  • The elif keyword allows additional conditions to be checked if the previous conditions were not true.

    • The keyword stands for "else if."

    • There is no limit on the number of elif statements, but only one final else clause is permitted, and it must be the last branch.

Chained Conditionals Example

  • Example Structure:

  if x < y:
      print('x is less than y')
  elif x > y:
      print('x is greater than y')
  else:
      print('x and y are equal')

Example 1: Output

  • Code:

  Age = 27
  if Age >= 60:
      print('Senior Discount')
  elif Age <= 18:
      print('No Discount')
  else:
      print('Junior Discount')
  • Output: Junior Discount

Example 2: Output

  • Code:

  A = 33
  B = 33
  if B > A:
      print("B is greater than A")
  elif A > B:
      print("A is greater than B")
  else:
      print("A and B are equal")
  • Output: A and B are equal

Nested Conditionals

  • A conditional can be nested within another.

    • Example:
      if x == y: print('x and y are equal') else: if x < y: print('x is less than y') else: print('x is greater than y')

Syntax of Nested If Statements

  • Syntax:

  if EXPRESSION 1:
      STATEMENT(S)
      if EXPRESSION 2:
          STATEMENT(S)
      elif EXPRESSION 3:
          STATEMENT(S)
      else:
          STATEMENT(S)

Knowledge on Comparison Operators

  • Definition: Boolean expressions ask a question that produce a "Yes" or "No" result, controlling program flow directly.

  • Comparison Operators in Python:

    • <: Less than

    • <=: Less than or equal to

    • ==: Equal to

    • >=: Greater than or equal to

    • >: Greater than

    • !=: Not equal

    • Note: Remember `