Skip to main content

Conditional Statements


Conditional STATEMENTS

Normally, the statements are executed sequentially as written in the program (i.e. Top to bottom). This is known as the normal flow of control. But sometimes the operational flow of control has to
be altered depending upon a condition. The statements that allow to do so, are known as selection statements. Most important selection statement that python offers is the If statement.
Selection statements are also known as conditional statements or decision statements.

THE IF STATEMENT

An if statement tests a particular condition; if the condition evaluates to true, a course of action is followed i.e. a statement of a set of statements is executed. If the condition is false the course of action is ignored.
The syntax of if statement is as shown below:
if (expression) :
Statement

THE ELIF STATEMENT

In some cases the program requires you to write multiple if statements. This results in many condition evaluations thus slowing up the program. So to avoid this elif statements can be used.
Unlike if statement elif statements stops further evaluation of conditional statement once a condition is true. The keyword ‘elif‘ is short for ‘else if’, and is useful to avoid excessive indentation.
An if ... elif ... elif ... sequence is a substitute for
the switch or case statements found in other languages.
USING ELSE
If all the conditions inside conditional statements are false then
the body of the else loop will be executed.
If (expression) :
Statement1
Elif(expression):
Statement2
Else:
Statement3
If , elif and else this way, together form the if-elif-else ladder

Comments

Popular posts from this blog

Factorial Program in Python

What is Factorial Program? Factorial Program in C: Factorial of n is   the product of all positive descending integers . Factorial of n is denoted by n!. For example: 5! = 5*4*3*2*1 = 120. How do you code a factorial for a loop in Python? num = int(input("enter a number: ")) fac = 1. for i in range(1, num + 1): fac = fac * i. print("factorial of ", num, " is ", fac) Factorial Program in Python # Python program to find the factorial of a number provided by the user. # change the value for a different result num = 7 # To take input from the user #num = int(input("Enter a number: ")) factorial = 1 # check if the number is negative, positive or zero if num < 0 : print ( "Sorry, factorial does not exist for negative numbers" ) elif num == 0 : print ( "The factorial of 0 is 1" ) else : for i in range( 1 ,num + 1 ): factorial = factorial*i print ( "The factorial of" ,num, "is" ...

Using input command in python

Today i will show you that how can you use input command in python language, so we will write a simple program that will ask for any name or value and then print that name or value. So here is our simple program. a= input ( "Enter your name" ) print (a) as you can see we have declared "a" a variable that will ask for input "Enter your name" and then print command will print the variable "a". So anything we will type will be printed. As show in the picture below. But here you have to remember, if you are working with integers for some mathematical work than you have to declare input type as integer because then python will take input type as integer and not as word. As show in the picture below. As you can see the syntax that how can we declaring the input type,,int, we have to write int then in brackets we have to enter the input command, int stands for integer and it will declare our input as integer, we have and in print command we have instruc...

Writing first python program

  We are here to write our first program,it will  be an easy program just printing the entered word or value. The syntax of python language is very simple and easy and you dont have to confuse your mind with any complexity. So here is our first program print ( "Hello my name is abc" ) print command is used to display the output written in brackets with inverted commas. As we have typed the sentence "Hello my name is abc". So this command will simply print this sentence as show above in the picture.