Introduction to Control Structures in Python

A program statement that causes a jump of control from one part of the program to another based on conditions is called a control structure or control statement. In Python, There are three important control structures,

  • Sequential
  • Alternating or Conditional Branching
  • Iteration or Looping

Sequential Statement

A sequential statement is a sequence of statements that are executed one after another (one by one). In a sequential statement, you know that python is an interpreter language so it compiles line by line.

In Real life example, In the morning, you are wake up and your activity is like brushing, taking tea, exercise, bathing, and so on. we know all that process, But python what you print first that will execute first it doesn’t know any process.

python virtual image

In the mean time, We want to know about one of the important concept of python

  • Indentation

In other languages like C, C++, java use curly braces { } to indicate blocks of codes for class, functions, or body of the loops and block of selection command. But in python there are no curly braces for indicating blocks instead of curly braces, Python uses Indentation (White spaces or tabs) to separate blocks.

Most of the indentation comes under after finished with the (:) symbol. Some categories are if, if-else, if elif, for, while, def, class, etc., Indentation has 1 tab space or 4 space bars. If you had confused let move on we will see many times in coming blogs.

Alternating or Conditional Branching

In our daily life, we takes many decisions for various activity. For example, which color dress I will wear tomorrow to impress my girlfriend. if your girlfriend likes red color, you will wear red color. if suppose you wear any other color your love definitely break up. Just for fun example, Cool.

In Python, They are three decision-making statements. To check whether the give the number is positive or negative, even or odd, etc.,

  • Simple if Statement
  • if-else Statement
  • if elif Statement

Simple if Statement

Simple if is the simplest of all decision-making statements. The condition should be in the form of relational or logical expression. If the condition is true, the statement of if the class is executed, otherwise nothing is happening. It directly exits from the loop.

Syntax of if statement

  • if <condition>:
    • statements-block1

In the above example, I run two times the first time I give Red that condition is true so, it goes inside an if statement blog and gives a result as “You will go to Dating with your girl friend” and the second time I give Green but the condition is false it comes out from the if statement.

if-else Statement

if .. else statement check the true block as well as the false block. If the condition is true, it executes if statement blog otherwise it executes an else blog.

Syntax of if else Statement

  • if :
    • statements-block 1
  • else:
    • statements-block 2
python virtual image

you can compare the if statement example and the if-else statement. In the example of the if-else statement the condition is false in the second time so, it executes the else blog but not in the if statement.

if elif Statement

When we need to construct a many if statement(s) then ‘elif ’ clause can be used instead of ‘else’. It likes the same as the if-else statement but we have more if clause we can add elif instead of many if clause.

Syntax of if elif Statement:

  • if :
    • statements-block 1
  • elif :
    • statements-block 2
  • else:
    • statements-block n
python virtual image

You can compare all previous examples for more clarification. These topics come under Alternating and Conditional branching. The next control structure we want to see is Looping or Iteration. It is a very important and huge concept so, we will see it in the next fresh blog.

My Previous Post

Different types of Operators in python: https://iterathon.tech//different-types-operators-in-python/

LEARN LITTLE BY LITTLE TO CHANGE THE TECH WORLD

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *