This example illustrates how you can employ if…else to “unlock a door” when the user inputs the correct code:
int doorCode = 1337;
if (doorCode == 1337) { |
This example demonstrates how you can utilize if…else to determine whether a number is positive or negative:
int myNum = 10; // Is this a positive or negative number? if (myNum > 0) { printf(“The value is a positive number.”); } else if (myNum < 0) { printf(“The value is a negative number.”); } else { printf(“The value is 0.”); } |
Determine if an individual meets the legal age requirement to vote:
int myAge = 25; int votingAge = 18; if (myAge >= votingAge) { |
Determine whether a number is even or odd.
int myNum = 5;
if (myNum % 2 == 0) { |