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 Examples

Real-Life Examples

This example illustrates how you can employ if…else to “unlock a door” when the user inputs the correct code:

Example

int doorCode = 1337;

if (doorCode == 1337) {
  printf(“Correct code.\nThe door is now open.”);
} else {
  printf(“Wrong code.\nThe door remains closed.”);

This example demonstrates how you can utilize if…else to determine whether a number is positive or negative:

Example

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:

Example

int myAge = 25;
int votingAge = 18;

if (myAge >= votingAge) {
  printf(“Old enough to vote!”);
} else {
  printf(“Not old enough to vote.”);

Determine whether a number is even or odd.

 

Example

int myNum = 5;

if (myNum % 2 == 0) {
  printf(“%d is even.\n”, myNum);
} else {
  printf(“%d is odd.\n”, myNum);
}