top of page
  • Megan Silvey

If Else Statements


Many programmers utilize the technique of if else statements in their code. An if else statement is used to evaluate a condition and execute certain action(s) if it is true and potentially a different action if it is not. The if checks the condition, and the else is a catch all for when the if is not true. For example, a program can use an if else statement to determine if a name field is properly filled in on a website. If it is filled in correctly, then it will let the user continue on. If not, then it will provide a prompt notifying the user to properly fill out the field as instructed.


If there are multiple conditions to check instead of one condition, then programmers use an expanded version of the if else statement. In Python, this is called an if elif else statement where the elif portion covers all the other conditions. The else still works the same where it triggers if none of the if or elif conditions end up being true. One great thing about an if elif else statement is that the code stops once it meets the condition that is true. This makes it faster to run and easier to code with having the code complete execution once it finds the first true statement. For example, you can use a grading scale to assign letter grades from numerical grade values by using an if elif else statement. This is great if you have many grades to evaluate from a large list of students.


8 views0 comments

Recent Posts

See All

MATLAB

bottom of page