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

Else

The else Statement

Utilize the else statement to designate a block of code for execution if the condition evaluates to false.

Syntax

if (condition) {
  // block of code to be executed if the condition is true
} else {
  // block of code to be executed if the condition is false
}

Example

int time = 20;
if (time < 18) {
  printf(“Good day.”);
} else {
  printf(“Good evening.”);
}
// Outputs “Good evening.” 

Example explained

In the provided example, since the time (20) exceeds 18, the condition is false. Consequently, the program proceeds to the else condition and outputs “Good evening” to the screen. Had the time been less than 18, the program would have printed “Good day”.