3. Selection: Comparisons
Other than for the most basic tasks, most programs involve having to make choices. Maybe you want to do one thing if a condition is true and another if it is not. The process of determining which choice to take is called 'selection'.
Selection means the CPU is given the choice of two or more possible actions and it decides which one to carry out depending on the result of a comparison.
Comparisons
Selection always involves a comparison operation, where you check whether certain conditions are met. These use relational operators, which we discussed in the operators section. But, to remind you, the operators you will need to know are:
Meaning | Symbol |
---|---|
Greater than | $>$ |
Less than | $<$ |
Greater than or equal to | $\ge$ |
Less than or equal to | $\le$ |
Not equal to | $\ne$ |
Are the same | $=$ |
Combine comparisons
You can use boolean operators to make two comparisons in the same statement, like this
$$a < 2\ AND\ b > 4$$The boolean AND operator means that both comparisons need to be true for the evaluation to be true. Other common boolean operators used are OR and NOT.
Challenge see if you can find out one extra fact on this topic that we haven't already told you
Click on this link: What is selection in programming?