Conditionals in C practice — 3 real programs in gedit and Fedora
In the previous article we covered C conditionals theory. Now it’s time to write real programs. In this article we build three programs from scratch using gedit and gcc in Fedora — a grade classifier, a number guessing game and a calculator with a menu. Each one uses if/else if/else and switch in situations where the choice between them is intentional and clear.
Set up your workspace:
cd ~/GCID/IC2/Labs mkdir Lab_conditionals cd Lab_conditionals
Table of Contents
Conditionals in C practice — Program 1: Grade classifier
This program reads a grade and classifies it — showing how if/else if handles ranges that switch can’t, and how to validate input before processing it.
touch classifier.c gedit classifier.c &
#include <stdio.h>
int main() {
char name[50];
double grade;
char grade_letter;
const double PASS = 5.0;
printf("=== GRADE CLASSIFIER ===\n\n");
printf("Student name: ");
scanf("%s", name);
printf("Grade (0-10): ");
scanf("%lf", &grade);
/* Input validation */
if (grade < 0 || grade > 10) {
printf("Error: grade must be between 0 and 10\n");
return 1;
}
/* Determine letter grade */
if (grade >= 9.0) grade_letter = 'A';
else if (grade >= 7.0) grade_letter = 'B';
else if (grade >= 5.0) grade_letter = 'C';
else grade_letter = 'F';
/* Display results */
printf("\n--- Results for %s ---\n", name);
printf("Grade: %.2f\n", grade);
printf("Letter: %c\n", grade_letter);
printf("Status: %s\n", (grade >= PASS) ? "PASSED" : "FAILED");
if (grade < PASS) {
printf("Points needed: %.2f\n", PASS - grade);
} else {
printf("Points above: %.2f\n", grade - PASS);
}
/* Detailed classification with switch on letter */
printf("\nFeedback: ");
switch (grade_letter) {
case 'A':
printf("Outstanding — excellent work!\n");
break;
case 'B':
printf("Merit — keep pushing for that A\n");
break;
case 'C':
printf("Passed — there is room to improve\n");
break;
case 'F':
printf("Failed — keep working at it\n");
break;
}
return 0;
}
gcc classifier.c -o classifier -Wall ./classifier
Output with Sergio, grade 7.5:
=== GRADE CLASSIFIER === Student name: Sergio Grade (0-10): 7.5 --- Results for Sergio --- Grade: 7.50 Letter: B Status: PASSED Points above: 2.50 Feedback: Merit — keep pushing for that A
Notice the combination of if/else if for the grade ranges (because switch doesn’t work with doubles) and switch for the letter feedback (because the letter is a char with exact values). Each tool in its right place.
The return 1 in the validation block is important — in C return 0 means success and any non-zero value means error. When input is invalid it makes sense to signal an error to the operating system and stop immediately rather than continuing with garbage data.
Conditionals in C practice — Program 2: Number guessing game
This is the first program in C that uses a while loop — we’ll cover loops properly in the next topic, but the logic here is intuitive and it’s a perfect showcase of nested if inside a loop.
touch guessing.c gedit guessing.c &
#include <stdio.h>
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */
int main() {
int secret, guess, attempts;
int min = 1, max = 100;
/* Seed the random number generator with current time */
srand(time(NULL));
/* Generate random number between 1 and 100 */
secret = (rand() % 100) + 1;
attempts = 0;
printf("=== GUESS THE NUMBER ===\n");
printf("I am thinking of a number between %d and %d\n\n", min, max);
/* Game loop */
do {
printf("Attempt %d — Your guess: ", attempts + 1);
scanf("%d", &guess);
attempts++;
if (guess < min || guess > max) {
printf("Please guess between %d and %d\n", min, max);
attempts--; /* don't count invalid guesses */
} else if (guess < secret) {
printf("Too low\n\n");
} else if (guess > secret) {
printf("Too high\n\n");
} else {
printf("\nCorrect! The number was %d\n", secret);
printf("You got it in %d attempt(s)\n\n", attempts);
}
} while (guess != secret);
/* Rating based on attempts */
printf("Rating: ");
if (attempts <= 3) {
printf("Incredible! Are you psychic?\n");
} else if (attempts <= 6) {
printf("Great job!\n");
} else if (attempts <= 10) {
printf("Good — within a normal range\n");
} else {
printf("Keep practising!\n");
}
return 0;
}
gcc guessing.c -o guessing -Wall ./guessing
Output:
=== GUESS THE NUMBER === I am thinking of a number between 1 and 100 Attempt 1 — Your guess: 50 Too high Attempt 2 — Your guess: 25 Too low Attempt 3 — Your guess: 37 Correct! The number was 37 You got it in 3 attempts Rating: Incredible! Are you psychic?
Two new things in this program worth noting. srand(time(NULL)) seeds the random number generator with the current time — without it rand() would produce the same sequence every time you run the program. rand() % 100 + 1 generates a number between 1 and 100 — rand() gives a large random number, % 100 gives the remainder when dividing by 100 (0 to 99), then + 1 shifts it to 1-100.
The do { } while () loop guarantees the block runs at least once — perfect for games where you always need at least one guess. We’ll cover this properly in the loops article.
Conditionals in C practice — Program 3: Calculator with switch menu
This is the most complete program of the three — a full interactive calculator that keeps running until the user chooses to exit.
touch calc_menu.c gedit calc_menu.c &
#include <stdio.h>
int main() {
int option;
double a, b, result;
int running = 1; /* 1 = true, 0 = false — no bool in C */
printf("=== CALCULATOR ===\n");
while (running) {
/* Display menu */
printf("\n--- Menu ---\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Integer division and remainder\n");
printf("6. Power (a^b)\n");
printf("0. Exit\n");
printf("Option: ");
scanf("%d", &option);
/* Exit condition */
if (option == 0) {
printf("Goodbye!\n");
running = 0;
continue; /* skip to next loop check, which will be false */
}
/* Read operands for non-exit options */
printf("First number: ");
scanf("%lf", &a);
printf("Second number: ");
scanf("%lf", &b);
printf("\n--- Result ---\n");
switch (option) {
case 1:
result = a + b;
printf("%.4g + %.4g = %.4g\n", a, b, result);
break;
case 2:
result = a - b;
printf("%.4g - %.4g = %.4g\n", a, b, result);
break;
case 3:
result = a * b;
printf("%.4g * %.4g = %.4g\n", a, b, result);
break;
case 4:
if (b == 0) {
printf("Error: cannot divide by zero\n");
} else {
result = a / b;
printf("%.4g / %.4g = %.6g\n", a, b, result);
}
break;
case 5:
if (b == 0) {
printf("Error: cannot divide by zero\n");
} else {
int ia = (int)a, ib = (int)b;
printf("%d // %d = %d\n", ia, ib, ia / ib);
printf("%d %% %d = %d\n", ia, ib, ia % ib);
}
break;
case 6: {
int exp = (int)b;
result = 1;
int i;
for (i = 0; i < exp; i++) {
result *= a;
}
printf("%.4g ^ %d = %.4g\n", a, exp, result);
break;
}
default:
printf("Invalid option — choose between 0 and 6\n");
}
}
return 0;
}
gcc calc_menu.c -o calc_menu -Wall ./calc_menu
Output:
=== CALCULATOR === --- Menu --- 1. Addition ... Option: 4 First number: 10 Second number: 3 --- Result --- 10 / 3 = 3.33333 --- Menu --- ... Option: 0 Goodbye!
Three details worth understanding here. %.4g is a smart format specifier — it uses either fixed or scientific notation depending on which is shorter, with 4 significant figures. It avoids printing 10.0000 when 10 is cleaner. The case 6: block is wrapped in { } — this is required when you declare a variable inside a case because C’s scoping rules require it. The int running = 1 pattern is the classic C way of having a boolean flag — C89 has no bool type, so int with 0/1 values is the convention.
Visualise with Python Tutor
Select C from the dropdown and paste in pythontutor.com this minimal version to see how switch jumps directly to the matching case:
#include <stdio.h>
int main() {
int option = 2;
switch (option) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
default:
printf("Other\n");
}
return 0;
}
Step through it and watch how execution jumps directly to case 2 without evaluating cases 1 or 3. Then try removing the break after case 2 and step through again — you’ll see fall-through in action as execution continues into case 3 and default without any condition check.
Summary and next step
In this article you practised C conditionals with three real programs. You used if/else if for grade ranges and validation, switch for exact char values and menu options, the ternary operator for one-line decisions, and the = vs == trap in a real context.
In the next article you’ll find exercises to solve on your own in Fedora.

One Comment