Consider a “real-life example” scenario where we need to determine if a person is eligible to vote.
In the following example, we utilize the >= comparison operator to ascertain whether the age ( 25 ) exceeds or equals the voting age threshold, which is established at 18:
int myAge = 25; int votingAge = 18; printf(“%d”, myAge >= votingAge); // Returns 1 (true), meaning 25 year olds are allowed to vote! |
Isn’t that neat? An even more effective approach (since we’re making progress) would be to enclose the aforementioned code within an if…else statement, enabling us to execute different actions based on the outcome:
If myAge is 18 or older, output “Old enough to vote!” Otherwise, output “Not old enough to vote.”
int myAge = 25; int votingAge = 18; if (myAge >= votingAge) { |
Booleans serve as the foundation for all comparisons and conditions. Further details on conditions (if…else statements) will be covered in the upcoming chapter. |