Skip to content

Control Statement

Control statements control the flow of execution of a program. This statements help to make decisions, repeat tasks or jump to specific part of code.

Conditional statement to execute a block of code if a condition is true.

if (condition) {
// code
}

It allows us to execute a block of code among multiple conditions. It evaluates conditions in sequence and executes the code block associated with the first true condition.

if (number > 0) {
// block1
} else if (number < 0) {
// block2
} else {
// default block
}

Extends if statement to execute one block if condition is true and another if false.

if (condition) {
// true block
} else {
// false block
}

Allows multi-way branching.

switch(expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
#include <stdio.h>
int main() {
int number = 0;
if (number > 0) {
printf("The number is positive.\n");
} else if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
int choice = 2;
switch (choice) {
case 1:
printf("You selected option 1.\n");
break;
case 2:
printf("You selected option 2.\n");
break;
case 3:
printf("You selected option 3.\n");
break;
default:
printf("Invalid option.\n");
}
return 0;
}