Monday 24 June 2019

Conditional Statements

1.     Control statements

Control statements:
The Control Statements are used for controlling the Execution of the program there are the three control statements 

Divided into 3 parts:
1.      Conditional
2.    Looping or repetitive statements
3.    Unconditional statements

Conditional Statements:

Statements in a programs are executed sequentially. This happens when there is no condition around the statements.
If you put some condition for a statement(s) the flow of execution may change based on the result evaluated by the condition.

They are:
(1.)if (2.) if..else (3.)if…elif..else statements

The if statement:

This statement is used to execute one or more statement depending on whether a condition is True or False

a=1
if a==1:
     print(“one”)
first the condition is checked, if the condition gets True then the if statement is executed.

We can also write group of statements after colon.  The group of statements in Python is called a suite.
While writing a group of statements, we should write them all with proper indentation.


What is Python suite?
A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite.
Indentation means the spaces left before the statements. The default indentation used in Python is 4 spaces.

Header lines begin the statement (with the keyword) and terminate with a colon (: ) and are followed by one or more lines which make up the suite.


For example:
"""wap to accept empno,empname,empsal: Calculate the tax depending upon the
following condition: if salary is greater than 3000/-,
tax should be applied 10% of basic salary
"""
empno=int(input("enter emp no "))
empname=input("enter emp name ")
sal=float(input("enter emp basic salary "))
city=input("enter emp city ")
if sal>=3000:
    tax=0.10*sal
    print("emp no is: ",empno)
    print("emp Name : ",empname)
    print("emp salary :",sal)
    print("emp tax is :",tax)
""" output: case 1:
enter emp no  :007
enter emp name : Sitha
enter emp basic salary: 30000
enter emp city: hyd
emp no is:  7
emp Name :  Sitha
emp salary : 30000.0
emp tax is : 3000.0

Case 2:

enter eno   11
enter ename  krishna
enter salary  2500
enter emp city kadapa
>>> 
>>> 

"""
Observe that every print() function mentioned after colon is starting after 4 spaces only. When we write the statements with same indentation, then those statements are considered as a suite (or belonging to same group).

Indentation:
Indentation is very important in Python. Indentation refers to spaces that are used in the beginning of a statement.

if x==1:

….print(‘a’)

Related Videohttps://www.youtube.com/watch?v=_cS3R_6SbRc&feature=youtu.be

Next Topic:if else statement
Previous TopicControl statements (Related Video: https://www.youtube.com/watch?v=c2AzRkJRruY&feature=youtu.be )

No comments:

Post a Comment

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...