Javatpoint Logo
Javatpoint Logo

Control Structures in Python

Most programs don't operate by carrying out a straightforward sequence of statements. A code is written to allow making choices and several pathways through the program to be followed depending on shifts in variable values.

All programming languages contain a pre-included set of control structures that enable these control flows to execute, which makes it conceivable.

This tutorial will examine how to add loops and branches, i.e., conditions to our Python programs.

Types of Control Structures

Control flow refers to the sequence a program will follow during its execution.

Conditions, loops, and calling functions significantly influence how a Python program is controlled.

There are three types of control structures in Python:

  • Sequential - The default working of a program
  • Selection - This structure is used for making decisions by checking conditions and branching
  • Repetition - This structure is used for looping, i.e., repeatedly executing a certain piece of a code block.

Sequential

Sequential statements are a set of statements whose execution process happens in a sequence. The problem with sequential statements is that if the logic has broken in any one of the lines, then the complete source code execution will break.

Code

Output:

The result of the subtraction is:  10
The result of the addition is :
30
The result of the multiplication is:  200

Selection/Decision Control Statements

The statements used in selection control structures are also referred to as branching statements or, as their fundamental role is to make decisions, decision control statements.

A program can test many conditions using these selection statements, and depending on whether the given condition is true or not, it can execute different code blocks.

There can be many forms of decision control structures. Here are some most commonly used control structures:

  • Only if
  • if-else
  • The nested if
  • The complete if-elif-else

Simple if

If statements in Python are called control flow statements. The selection statements assist us in running a certain piece of code, but only in certain circumstances. There is only one condition to test in a basic if statement.

The if statement's fundamental structure is as follows:

Syntax

These statements will always be executed. They are part of the main code.

All the statements written indented after the if statement will run if the condition giver after the if the keyword is True. Only the code statement that will always be executed regardless of the if the condition is the statement written aligned to the main code. Python uses these types of indentations to identify a code block of a particular control flow statement. The specified control structure will alter the flow of only those indented statements.

Here are a few instances:

Code

Output:

The initial value of v is 5 and that of t is  4
5 is bigger than  4
The new value of v is 3 and the t is  4
3 is smaller than  4
the new value of v is
4
The value of v,
4  and t, 4, are equal

if-else

If the condition given in if is False, the if-else block will perform the code t=given in the else block.

Code

Output:

The value of v is  4 and that of t is  5
v is less than t

Repetition

To repeat a certain set of statements, we use the repetition structure.

There are generally two loop statements to implement the repetition structure:

  • The for loop
  • The while loop

For Loop

We use a for loop to iterate over an iterable Python sequence. Examples of these data structures are lists, strings, tuples, dictionaries, etc. Under the for loop code block, we write the commands we want to execute repeatedly for each sequence item.

Code

Output:

2, 4, 7, 1, 6, 4,

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

While Loop

While loops are also used to execute a certain code block repeatedly, the difference is that loops continue to work until a given precondition is satisfied. The expression is checked before each execution. Once the condition results in Boolean False, the loop stops the iteration.

Code

Output:

2 3 4 5 6 7 8 While loop is completed






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA