Home » Python

Conditional statements (if, break and continue statements) in Python

Learn: What are Conditional Statements in Python? How their control flow and decision could be taken by these statements?
Submitted by Abhishek Jain, on September 27, 2017

Same as of other programming languages, python also uses conditional Statements like if-else, break, continue etc. While writing program(s), we almost always need the ability to check the condition and then change the course of program, the simplest way to do so is using if statement.

Code:

x = 10

if  x > 0: 
    print("x is positive")

Here, the Boolean expression written after if is known as condition, and if Condition is True, then the statement written after, is executed. Let's see the syntax of if statement.

Syntax - 1

if condition: 
	STATEMENTs- BLOCK 1
[ else:       #Statement with in [ ] bracket are optional.
	STATEMENTs- BLOCK 2]

Let us understand the syntax, in Syntax 1 - if the condition is True (i.e. satisfied), the statement(s) written after if (i.e. STATEMENT-BLOCK 1) is executed, and otherwise statement written after else (i.e. STATEMENT-BLOCK 2) is executed. Remember else clause is optional. If provided, in any situation, one of the two blocks get executed not both.

Syntax - 2

if condition-1: 
	STATEMENTs- BLOCK 1 
[elif condition-2:         #Statement with in [ ] bracket are optional. 
	STATEMENTs- BLOCK 2 
else: STATEMENTs- BLOCK N]

We can say that, "if" with "else" provides an alternative execution, as there are two possibilities and the condition determines which one gets executed. If there are more than two possibilities then we need to chain the if statement(s). This is done using the 2nd option of if statement. Here, we have used "elif" clause instead of "else". "elif" combines if else- if else statements to one if "elif" …else. You may consider "elif" to be an abbreviation of else if. There is no limit to the number of "elif" clause used.

In the chained conditions, each condition is checked in order if previous is False then next is checked, and so on. If one of them is True then corresponding block of statement(s) are executed and the statement ends i.e., control moves out of "if statement". If none is true, then else block gets executed if provided. If more than one condition is true, then only the first true option block gets executed.

It is possible to have a condition within another condition. Such conditions are known as Nested Condition.

Example:

x=5
y=10

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)

Output

5 is less than 10 

break Statement

break can be used to unconditionally jump out of the loop. It terminates the execution of the loop. break can be used in while loop and for loop. break is mostly required, when because of some external condition, we need to exit from a loop.

Example:

for letter in "Python":
  if letter =='h':
     break
  print (letter)

Output

P
y
t

continue Statement

This statement is used to tell Python to skip the rest of the statements of the current loop block and to move to next iteration, of the loop. continue will return back the control to the beginning of the loop. This can also be used with both while and for statement.

Example:

for letter in "Python":
   if letter == 'h':
       continue
   print(letter)

Output

P
y
t
o
n





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.