Loading . . .

C Programming in VS Code

Printing a message in C

Beginner's Guide to Keywords, Identifiers, Constants, Variables, Datatypes in C

Operators & Expressions in C

Input & Output Operations in C

Control Statements in C

Loops in C

Array in C

Character Array & Strings in C

User Defined Functions in C

Structure and Union in C

Pointers in C


Disclaimer: Educational Purposes Only

The content on this website, including videos and code examples, is for educational purposes only. All demonstrations and designs are fictional and created to illustrate coding techniques. Any resemblance to existing websites or brands is purely coincidental.

The creators and administrators of this website do not claim ownership or affiliation with any existing websites or companies. Users are encouraged to use the information responsibly for learning purposes. Liability for any misuse of the content provided is not accepted.

Download Notes

Control Statements in C

Control statement: These statements control the flow of execution which are known as the control statements.

If statement: An if statement in C is used to make a decision in a program. It checks if a certain condition is true, and if it is, a block of code is executed.

Types of if statements:

1) Simple if statement: A simple if statement in C checks a condition and if it's true, runs a block of code.

2) If-else statement: An if-else statement in C checks a condition, if it's true it runs a block of code, if it's false it runs another block of code.

3) Nested if-else statement: A nested if-else statement in C is an if-else statement within another if-else statement. It allows for multiple conditions to be checked in a logical order.

4) If-else ladder: An if-else ladder statement in C is multiple if-else statements cascaded one after another, it checks multiple conditions and runs a different block of code based on the condition.

Switch statement: A switch statement in C is used to execute different blocks of code based on a specific value.

Control Statements in C

Simple if statement:

simple_if_program.c


#include <stdio.h>

void main() {
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);
    if(number > 10) {
        printf("\nThe number is greater than 10.");
    }
    printf("\nYou entered: %d", number);
}

If else:

if_else.c


#include <stdio.h>

void main() {
    int age;
    printf("Enter age: ");
    scanf("%d", &age);
    if(age >= 18) {
        printf("\nYou can vote.");
    } else {
        printf("\nYou can't vote.");
    }
    printf("\nAge you entered: %d", age);
}

Nested if else:

nested_if_else.c


#include <stdio.h>

void main() {
    int a, b, c;
    printf("Enter three numbers(press enter or space between numbers to make them separate): ");
    scanf("%d %d %d", &a, &b, &c);
    if(a > b) {
        if(a > c) {
            printf("\n%d (FIRST) is largest.", a);
        } else {
            printf("\n%d (THIRD) is largest.", c);
        }
    } else {
        if(c > b) {
            printf("\n%d (THIRD) is largest.", c);
        } else {
            printf("\n%d (SECOND) is largest.", b);
        }
    }
}

If else Ladder:

if_else_ladder.c


#include <stdio.h>

void main() {
    int marks;
    printf("Enter the marks: ");
    scanf("%d", &marks);
    if(marks >= 60) {
        printf("\nFirst Division.");
    } else if(marks >= 45) {
        printf("\nSecond Division.");
    } else if(marks >= 35) {
        printf("\nThird Division.");
    } else {
        printf("\nFail.");
    }
}

Switch statement:

switch_program.c


#include <stdio.h>

void main() {
    char text;
    printf("Choose an option\na)Subs\tb)Like\tc)Share\n");
    text = getchar();
    switch(text) {
        case 'a':
            printf("\nSubscribe to channel.");
            break;
        case 'b':
            printf("\nLike this video.");
            break;
        case 'c':
            printf("\nShare this video with family and friends.");
            break;
        default:
            printf("\nInvalid Option.");
            break;
    }
}