Control Structures in C

Control structures are the fundamental building blocks of programming. They allow programmers to dictate how the code should execute based on conditions or repetitions.

Types of Control Structures

1. if Statement

Checks a condition. If it's true, the code inside the block runs.

int age = 20;
if (age >= 18) {
  printf("Eligible to vote");
}

2. if-else Statement

Provides an alternate path if the condition is false.

int age = 16;
if (age >= 18) {
  printf("Eligible to vote");
} else {
  printf("Not eligible to vote");
}

3. else-if Ladder

Used to check multiple conditions.

int score = 75;
if (score >= 90) {
  printf("A grade");
} else if (score >= 75) {
  printf("B grade");
} else {
  printf("C grade");
}

4. switch Statement

Checks a variable against a list of values.

int day = 2;
switch (day) {
  case 1:
    printf("Monday");
    break;
  case 2:
    printf("Tuesday");
    break;
  default:
    printf("Invalid day");
}

5. break Statement

Terminates loop or switch block prematurely.

for (int i = 1; i <= 5; i++) {
  if (i == 3) {
    break;
  }
  printf("%d ", i);
}

6. continue Statement

Skips the current iteration and moves to the next.

for (int i = 1; i <= 5; i++) {
  if (i == 3) {
    continue;
  }
  printf("%d ", i);
}

7. goto Statement

Transfers control to a labeled statement.

int num = 10;
if (num > 0)
  goto label;

printf("Negative");

label:
  printf("Positive");

C Programming - If Else Assignments

1. Check whether a number is positive, negative or zero.

Write a program to input a number and print whether it is positive, negative or zero using if else statements.

2. Find the largest of two numbers.

Take two integer inputs from the user and display the largest number using if else.

3. Check if a number is even or odd.

Write a C program to check whether a given number is even or odd using if else.

4. Find eligibility to vote.

Input the age of the user and determine whether the user is eligible to vote (age >= 18).

5. Check whether a character is a vowel or consonant.

Input a character from the user and determine whether it is a vowel or consonant using if else.

6. Check if a year is a leap year or not.

Input a year and determine whether it is a leap year using proper conditions and if else.

7. Determine grade from percentage.

Input percentage marks and determine grade based on the value (e.g., A, B, C, Fail) using nested if else.

8. Create a simple calculator (add, subtract, multiply, divide) based on user input.

Ask for two numbers and an operator. Use if else to perform the corresponding operation.

9. Check whether a number is divisible by 5 and 11.

Take input from the user and check whether the number is divisible by both 5 and 11 using if else.

10. Check if a number is positive, negative or zero using nested if.

Use nested if structure to determine the category of number input.

11. Find the greatest of three numbers.

Input three numbers and determine the greatest using multiple if else conditions.

12. Check whether a triangle is valid or not.

Input three angles of a triangle. A triangle is valid if the sum of all three angles is equal to 180 degrees.

13. Check if character is alphabet, digit or special character.

Take one character as input and categorize it using if else.

14. Menu-driven program for simple operations.

Create a menu for add, subtract, multiply, divide. Ask for user's choice and perform accordingly using if else if.