If Statements

C conditional execution

Basic if statement

Simple C if statement

#include <stdio.h>

int main() {
    int x = 10;
    
    if (x > 5) {
        printf("x is greater than 5\n");
    }
    
    return 0;
}
If-else statement

C if-else statement

#include <stdio.h>

int main() {
    int age = 18;
    
    if (age >= 18) {
        printf("You are an adult\n");
    } else {
        printf("You are a minor\n");
    }
    
    return 0;
}
If-else if-else chain

Multiple conditions with else-if

#include <stdio.h>

int main() {
    int score = 85;
    
    if (score >= 90) {
        printf("Grade: A\n");
    } else if (score >= 80) {
        printf("Grade: B\n");
    } else if (score >= 70) {
        printf("Grade: C\n");
    } else {
        printf("Grade: F\n");
    }
    
    return 0;
}
C if with logical operators

C if statement with logical AND operator

#include <stdio.h>

int main() {
    int age = 25;
    char status = 'A';
    
    if (age >= 18 && status == 'A') {
        printf("Eligible for premium service\n");
    } else if (age >= 18 && status == 'B') {
        printf("Eligible for standard service\n");
    } else {
        printf("Not eligible\n");
    }
    
    return 0;
}
C if with comparison operators

C if statement with multiple comparison operators

#include <stdio.h>

int main() {
    int a = 10, b = 20;
    
    if (a > b) {
        printf("a is greater than b\n");
    } else if (a < b) {
        printf("a is less than b\n");
    } else {
        printf("a is equal to b\n");
    }
    
    return 0;
}
C if with character operations

C if statement with character classification functions

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'A';
    
    if (isupper(ch)) {
        printf("Uppercase letter\n");
    } else if (islower(ch)) {
        printf("Lowercase letter\n");
    } else if (isdigit(ch)) {
        printf("Digit\n");
    } else {
        printf("Other character\n");
    }
    
    return 0;
}
C if with string comparison

C if statement with string comparison using strcmp

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "hello";
    char str2[] = "world";
    
    if (strcmp(str1, str2) == 0) {
        printf("Strings are equal\n");
    } else if (strcmp(str1, str2) < 0) {
        printf("str1 comes before str2\n");
    } else {
        printf("str1 comes after str2\n");
    }
    
    return 0;
}
C if with pointer operations

C if statement with pointer operations

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *ptr = arr;
    
    if (ptr != NULL) {
        printf("Pointer is not NULL\n");
        printf("First element: %d\n", *ptr);
    }
    
    if (*ptr > 0) {
        printf("First element is positive\n");
    }
    
    return 0;
}
C if with ternary operator

C if statement using ternary operator

#include <stdio.h>

int main() {
    int a = 10, b = 20;
    int max = (a > b) ? a : b;
    
    printf("Maximum: %d\n", max);
    
    // Nested ternary
    int result = (a > b) ? 1 : (a < b) ? -1 : 0;
    printf("Comparison result: %d\n", result);
    
    return 0;
}
C if with bitwise operations

C if statement with bitwise operations

#include <stdio.h>

int main() {
    int num = 5;
    
    if (num & 1) {
        printf("Number is odd\n");
    } else {
        printf("Number is even\n");
    }
    
    if (num & (1 << 2)) {
        printf("Bit 2 is set\n");
    }
    
    return 0;
}