FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses C Programming From Zero

First year

Progress0 / 6 lessons
  1. 1. What a program actually is
  2. 2. Variables, types and exact sizes
  3. 3. Input and output with printf and scanf
  4. 4. Control flow and loops
  5. 5. Functions and how the call stack works
  6. 6. Arrays and strings in C

Courses › C Programming From Zero

Control flow and loops

if, switch, the three loops, and the off-by-one errors that cost marks.

11 min read · Lesson 4 of 6 · Pro

Programs make decisions

Without branching, a program is a fixed list of steps. if is what makes it react to data.

C
#include <stdio.h>

int main(void)
{
    int marks;
    printf("Marks: ");
    if (scanf("%d", &marks) != 1) return 1;

    if (marks >= 90)
        printf("Grade O\n");
    else if (marks >= 75)
        printf("Grade A\n");
    else if (marks >= 40)
        printf("Grade B\n");
    else
        printf("Fail\n");

    return 0;
}

The order matters. Because the checks run top to bottom and stop at the first true one, marks = 95 never reaches the >= 75 test. Write the ranges from highest to lowest and the logic stays simple.

The rest of this lesson is Pro

The free lessons of C Programming From Zero finish what they start — read those first if you have not. This one goes further, and it is part of the paid half.

A pass opens the paid lessons of every course, the mock test papers and the company-wise series. It ends on its own date; nothing renews by itself.

Get a pass — from ₹29 for 7 days

Already bought one? Log in.