Skip to main content

Posts

Showing posts from July, 2024

Introduction to if...else Statements

  In Python, conditional statements allow us to execute certain code based on specific conditions. The if...else statement is one of the most commonly used control flow statements that helps in decision-making. It executes a block of code if a specified condition is true. Otherwise, it executes an alternative block of code. Syntax if condition: # code to execute if condition is true else : # code to execute if condition is false Basic Usage Simple if Statement A simple if statement checks a condition and executes the indented block of code only if the condition is true. if...else Statement An if...else statement allows us to define an alternative block of code that runs if the condition is false. Example Codes Example 1: Basic if Statement x = 10 if x > 5 : print ( "x is greater than 5" ) Example 2: if...else Statement x = 3 if x > 5 : print ( "x is greater than 5" ) else : print ( "x is not greater than 5" ) Example...