{"id":6355,"date":"2023-10-30T11:29:24","date_gmt":"2023-10-30T05:59:24","guid":{"rendered":"https:\/\/python-programs.com\/?p=6355"},"modified":"2023-11-10T12:07:41","modified_gmt":"2023-11-10T06:37:41","slug":"python-ifelse-statement","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-ifelse-statement\/","title":{"rendered":"Python \u2013 If\u2026Else Statement"},"content":{"rendered":"
In real life, there are times when we must make choices and decide what to do next based on those decisions. Similar scenarios occur in programming where we must make decisions and then execute the next block of code based on those decisions.<\/p>\n
In programming languages, decision making statements determine the flow of program\u00a0execution.<\/p>\n
The if statement is the simplest decision-making statement. It is used to determine whether or not a specific statement or block of statements will be executed, i.e. if a certain condition is valid, then a block of statements is executed, otherwise not.<\/p>\n
Syntax:<\/strong><\/p>\n In this case, the condition will be either true or false after evaluation. The if statement accepts boolean values \u2013 if the value is valid, the block of statements below it will be executed; otherwise, it will not. We may also use condition with bracket \u2018(\u2018 \u2018)’.<\/p>\n Python, as we know, uses indentation to define a block. As a result, the block inside an if statement will be found, as seen in the example below.<\/p>\n <\/p>\n Example:<\/strong><\/p>\n Output:<\/strong><\/p>\n When the interpreter encounters an if argument, it evaluates the condition in the if-statement, and if that condition evaluates to True, it executes the suite in the if-block, i.e., the statements in the if-block and skips the statements in the else portion.<\/p>\n If the condition in the if-statement evaluates to False, the suite in the else block is executed directly, i.e., the statements in the else block are executed instead of the statements in the if block.<\/p>\n Syntax:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Here age is less than 15 so if condition is false and else statement is executed<\/p>\n In all of the preceding instances, we provide a single condition with the if-statement, but we can also provide multiple conditions.<\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n The if-statement in this case checks several conditions, and if all of them satisfy and evaluate to True, the interpreter executes the code in the if-block. In real life, there are times when we must make choices and decide what to do next based on those decisions. Similar scenarios occur in programming where we must make decisions and then execute the next block of code based on those decisions. In programming languages, decision making statements determine the flow of program\u00a0execution. If..Else …<\/p>\nif condition:\r\n # statements which should be executed<\/pre>\n
if condition:\r\n statement1\r\nstatement2\r\n\r\n#If the condition is valid, the if block will only consider\r\n# statement1 to be within its block.\r\n<\/pre>\n
age = 18\r\nif (age > 15):\r\n print(\"age is greater than 15\")\r\nprint(\"Statement after if block\")\r\n<\/pre>\n
age is greater than 15\r\nStatement after if block<\/pre>\n
2)If-else statement<\/h3>\n
if condition:\r\n statement_1\r\n statement_2\r\n statement_3\r\nelse:\r\n statement_4\r\n statement_5\r\n statement_6<\/pre>\n
3)Examples of If else statement<\/h3>\n
age = 13\r\nif (age > 15):\r\n print(\"age is greater than 15\")\r\nelse:\r\n print(\"age is less than 15\")\r\n<\/pre>\n
age is less than 15<\/pre>\n
4)If statement with multiple conditions<\/h3>\n
age = 13\r\n# checking if the age is between 10 to 20\r\nif (age > 10 and age < 20):\r\n print(\"ageis between 10 to 20\")\r\nelse:\r\n print(\"age is less than 15\")\r\n<\/pre>\n
age is between 10 to 20<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n