In general, control statements help with the control flow of the program. But, do we mean by control flow? Show
Control flow is the order in which the statements execute. For example, the program’s flow is usually from top to bottom(Sequential flow), but what if we want a particular block of code to only be executed when it fulfills a specific condition or wants to run a block of the code a specified number of times? These statements will ensure a smooth and intentional flow of a program. There are three types of control statements:
Conditional/selection statementsThese control statements help with implementing decision-making in a program. A conditional statement will select the block of statements that will execute based on the given condition. The result of the program depends on the condition. If the condition provided proves to be true, the result is 1; if it proves to be false, the result is 0. When there is a truth value, the code will get executed; otherwise, it will not. Simple if-elseThis code shows a true block and a false block. If the condition is true, the control will pass to the statements written under the if block. If the condition is false, it will pass to statements under the else block.
Eligibility of vote program with simple if else Else-if statementIf we want to test a particular case through multiple conditions, we can do it using the else-if statement.
Grades of a student program using Elif statements The program above checks the marks according to the condition we have assigned to the value of variable grade. This is also called an Nested |