Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

Real Life Example

Real Life Example

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:

Example

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:

 

Example

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) {
  printf(“Old enough to vote!”);
} else {
  printf(“Not old enough to vote.”);

 

Booleans serve as the foundation for all comparisons and conditions.

Further details on conditions (if…else statements) will be covered in the upcoming chapter.