C Tutorial

C If Else

if runs a block when a condition is true. else and else if cover the other paths.

if runs a block

Write if, a condition in parentheses, then a brace-wrapped block. If the condition is true, the block runs. If it is false, C skips the block and continues after the closing brace.

Always use braces, even for one statement. C lets you omit them; this tutorial never does. A later edit that adds a second line without braces will not do what it looks like.

Example

#include <stdio.h>

int main(void) {
  int score = 72;
  if (score >= 50) {
    printf("pass\n");
  }
  return 0;
}

Change score and compile in /c/try. At 49 the program prints nothing. That is a reason to add else.

else covers the rest

Pair else with an if when you need exactly one of two paths. One block runs. The other does not. There is no third option in a plain if/else.

Example

#include <stdio.h>

int main(void) {
  int score = 41;
  if (score >= 50) {
    printf("pass\n");
  } else {
    printf("fail\n");
  }
  return 0;
}

else if chains more tests

else if means: the previous tests were false, so try this one. C walks the chain from the top and runs the first block whose condition is true. A final else catches everything that did not match.

Order matters. Put the strictest test first. If you check score >= 50 beforescore >= 90, a 95 never reaches the A branch.

Example

#include <stdio.h>

int main(void) {
  int score = 82;
  if (score >= 90) {
    printf("A\n");
  } else if (score >= 80) {
    printf("B\n");
  } else if (score >= 70) {
    printf("C\n");
  } else {
    printf("below C\n");
  }
  return 0;
}

There is no elif keyword. The spelling is two words: else if.

Nested if

An if inside another if is a nested if. Use it when the second question only makes sense after the first one is true. Indent the inner block so a person can see the shape.

Example

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int age = 20;
  bool has_ticket = true;
  if (age >= 18) {
    if (has_ticket) {
      printf("enter\n");
    } else {
      printf("buy a ticket\n");
    }
  } else {
    printf("too young\n");
  }
  return 0;
}

Two independent facts can often be one condition:if (age >= 18 && has_ticket) { ... }. Nest when the inner else needs a different message.

Conditions you will write

NeedWrite
Equalif (a == b) { ... }
Not equalif (a != b) { ... }
Both trueif (a && b) { ... }
Either trueif (a || b) { ... }

Do not write if (x = 1). That assigns 1 to x and then treats it as true. Compare with ==. Always wrap the body in { }.

When if is the wrong tool

If you are picking among many integer labels — a menu choice, a day number — a switch is often clearer. That is the next chapter. For ranges such as scores, stay with else if.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Biology

Fever as a threshold

Many clinics treat 38.0 °C oral as fever. 37.5–38.0 is a grey band they may call raised. 36.8 °C is ordinary. These cutoffs are policy, not a law of nature. Ear thermometers and core probes disagree by a few tenths.

else if chains the bands so a temperature is classified once. Order matters: test the strict fever first, or 38.4 would also match a looser “raised” test if you wrote that first.

Example

#include <stdio.h>

int main(void) {
  double temp_c = 38.4;
  if (temp_c >= 38.0) {
    printf("fever\n");
  } else if (temp_c >= 37.5) {
    printf("raised\n");
  } else {
    printf("normal\n");
  }
  return 0;
}

Physics

Compared with the speed of sound

In dry air near 20 °C, sound travels at about 343 m/s. School problems often round to 340. A craft at 310 m/s is subsonic. At 340 it is Mach 1 in that model. Above, supersonic.

Mach number is v / v_sound. Temperature changes v_sound: colder air is slower. Using 340 for a high-altitude jet is the wrong table.

Mach = v / v_sound

Example

#include <stdio.h>

int main(void) {
  double v = 310.0;
  if (v > 340.0) {
    printf("supersonic\n");
  } else if (v == 340.0) {
    printf("Mach 1\n");
  } else {
    printf("subsonic\n");
  }
  return 0;
}

FAQ: C If Else

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c if else examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c if else in this C C lesson (C If Else).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.