Control structures are the fundamental building blocks of programming. They allow programmers to dictate how the code should execute based on conditions or repetitions.
break,
continue, goto).Checks a condition. If it's true, the code inside the block runs.
int age = 20;
if (age >= 18) {
printf("Eligible to vote");
}
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");
}
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");
}
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");
}
Terminates loop or switch block prematurely.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
printf("%d ", i);
}
Skips the current iteration and moves to the next.
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
Transfers control to a labeled statement.
int num = 10;
if (num > 0)
goto label;
printf("Negative");
label:
printf("Positive");
Write a program to input a number and print whether it is positive, negative or zero using if else
statements.
Take two integer inputs from the user and display the largest number using if else.
Write a C program to check whether a given number is even or odd using if else.
Input the age of the user and determine whether the user is eligible to vote (age >= 18).
Input a character from the user and determine whether it is a vowel or consonant using if else.
Input a year and determine whether it is a leap year using proper conditions and if else.
Input percentage marks and determine grade based on the value (e.g., A, B, C, Fail) using nested
if else.
Ask for two numbers and an operator. Use if else to perform the corresponding operation.
Take input from the user and check whether the number is divisible by both 5 and 11 using if else.
Use nested if structure to determine the category of number input.
Input three numbers and determine the greatest using multiple if else conditions.
Input three angles of a triangle. A triangle is valid if the sum of all three angles is equal to 180 degrees.
Take one character as input and categorize it using if else.
Create a menu for add, subtract, multiply, divide. Ask for user's choice and perform accordingly using if else if.