0% found this document useful (0 votes)
19 views6 pages

New - 11 - Selection Statement

Uploaded by

jayrathore130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

New - 11 - Selection Statement

Uploaded by

jayrathore130
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
SSi| Degezat Python If-else statements Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making. Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise. Following is the general form of a typical decision making structure found in most of the programming languages — If condition If condition is true is false Pern y cry Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value. ° SSi| Degital 1 ion in P For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block. Generally, four spaces are given to indent the statements which are a typical amount of indentation in python. Indentation is the most used part of the python language since it declares the block of code. All the statements of one block are intended at the same level indentation. Python has the following decision-making statements: if statements if-else statements if-elif ladder Nested statements Python if statement if statement is the most simple form of decision-making statement. It takes an expression and checks if the expression evaluates to True then the block of code in if statement will be executed. If the expression evaluates to False, then the block of code is skipped. ‘Syntax: if (expression ): Statement 1 Statement 2 Statement n SSi | Degecat Python if statement Body of if Statement just below Python if-else statement From the name itself, we get the clue that the if-else statement checks the expression and executes the if block when the expression is True otherwise it will execute the else block of code. The else block should be right after if block and it is executed when the expression is False. ‘Syntax: if( expression ): Statement else: Statement SSi| Degezat Python if-else statement False Body of Statement just belo Note: Only one else statement is followed by an if statement. If you use two else statements after an if statement, then you get the following error. Python if-elif ladder You might have heard of the else-if statements in other languages like C/C++ or Java. In Python, we have an elif keyword to chain multiple conditions one after another. With elif ladder, we can make complex decision-making statements. The elif statement helps you to check multiple expressions and it executes the code as soon as one of the conditions evaluates to True. SSii| Degezat if( expression1 ): statement elif (expression2 ) : statement elif(expression3 ): statement else: statement Python if-elif ladder Statement 1 }—> Statement 2 }-—> Statement 3 be Body of else = : SSi | Decal Python Nested if statement In very simple words, Nested if statements is an if statement inside another if statement. Python allows us to stack any number of if statements inside the block of another if statements. They are useful when we need to make a series of decisions. ‘Syntax: if (expression): if(expression): Statement of nested if else: Statement of nested if else Statement of outer if Statement outside if block Python Nested if statement ‘Nested test expression True False | statament | Body ot nested Body of nested else Statement just below if

You might also like