1. Decision-Making Structures
if Statement:
Used to execute a block of code only if the condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
int x = 5;
if (x > 0) {
printf("Positive\n");
}
if-else Statement:
Executes one block of code if the condition is true, and another block if the condition is false.
Syntax:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
Example:
int x = -5;
if (x > 0) {
printf("Positive\n");
} else {
printf("Negative\n");
}
else if Ladder:
Used to check multiple conditions sequentially.
Syntax:
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if no condition is true
}
Example:
int x = 10;
if (x < 0) {
printf("Negative\n");
} else if (x == 0) {
printf("Zero\n");
} else {
printf("Positive\n");
}
switch Statement:
Used to test the value of a variable against multiple possible values (cases).
Syntax:
switch (variable) {
case value1:
// Code if variable == value1
break;
case value2:
// Code if variable == value2
break;
default:
// Code if none of the cases match
}
Example:
int day = 2;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
2. Loop Control Structures
while Loop:
Executes a block of code as long as the given condition is true.
Syntax:
while (condition) {
// Code to execute
}
Example:
int x = 0;
while (x < 5) {
printf("%d ", x);
x++;
}
do-while Loop:
Executes a block of code once, and then continues executing it while the condition is true.
Syntax:
do {
// Code to execute
} while (condition);
Example:
int x = 0;
do {
printf("%d ", x);
x++;
} while (x < 5);
for Loop:
Used when the number of iterations is known in advance.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to execute
}
Example:
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
Nested for Loop:
A for loop inside another for loop, often used for 2D array traversal or matrix operations.
Syntax:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Code to execute
}
}
Example:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", i * j);
}
printf("\n");
}
3. Other Control Statements
break Statement:
Terminates the current loop or switch statement immediately.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
continue Statement:
Skips the current iteration of the loop and proceeds with the next iteration.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
printf("%d ", i);
}
goto Statement:
Transfers control to a specific labeled statement within the program.
Syntax:
goto label;
label:
// Code to jump to
Example:
int x = 0;
label:
if (x < 5) {
printf("%d ", x);
x++;
goto label;
}
exit() Function:
Terminates the program immediately.
Syntax: exit(status); (where status is the exit status, typically 0 for successful execution).
Example:
if (some_condition) {
exit(0);
}
Decision Making:
if, if-else, else if, and switch are used to make decisions based on conditions.
Loops:
while, do-while, for, and nested for loops are used for repetitive tasks.
Control Flow:
break, continue, goto, and exit are used to control the flow of execution inside loops and functions.
These control structures are essential for building logic and controlling the flow of execution in a C program.
Here's a comprehensive example combining decision-making structures, loops, and other control statements in C:
#include <stdio.h>
#include <stdbool.h>
// Function to check if a number is prime
bool isPrime(int num) {
if (num <= 1) {
return false; // Numbers less than or equal to 1 are not prime
}
// Check divisibility from 2 to sqrt(num)
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false; // num is divisible by i, so it's not prime
}
}
return true; // num is prime
}
int main() {
int start, end;
// Input the range of numbers to check
printf("Enter the range (start and end) to find prime numbers: ");
scanf("%d %d", &start, &end);
// Check if the input is valid
if (start > end) {
printf("Invalid range! Start should be less than or equal to end.\n");
return 1; // Exit the program with an error code
}
printf("Prime numbers between %d and %d are:\n", start, end);
// Loop through the range to find prime numbers
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
printf("%d ", num); // Print the prime number
}
}
printf("\n");
return 0;
}
Decision-Making:
The function isPrime uses an if-else structure to check if a number is prime:
If the number is less than or equal to 1, it's not prime.
A for loop checks divisibility starting from 2 up to the square root of the number.
Loops:
The for loop in the main function iterates through the range of numbers entered by the user, checking each one to see if it's prime using the isPrime function.
The isPrime function itself uses a for loop to check divisibility of the number by every integer from 2 up to the square root of the number.
Control Flow:
The if statement in the main function checks if the entered range is valid (i.e., start is less than or equal to end).
If the input is invalid, the program exits with an error code using return 1.
Decision-making: if, else, else if
Loops: for, while
Other control statements: return, printf, scanf
Enter the range (start and end) to find prime numbers: 10 50
Prime numbers between 10 and 50 are:
11 13 17 19 23 29 31 37 41 43 47
This example demonstrates how to combine different control structures to solve a problem in C. The program finds prime numbers in a given range and illustrates the use of decision-making, loops, and control flow statements like return and printf.
Here are short notes on Control Structures in C:
if statement: Executes a block of code if a condition is true.
if (condition) {
// code to be executed if condition is true
}
if-else statement: Executes one block if the condition is true, and another if it is false.
if (condition) {
// code if true
} else {
// code if false
}
if-else if-else ladder: Used when there are multiple conditions to check.
if (condition1) {
// code if condition1 is true
} else if (condition2) {
// code if condition2 is true
} else {
// code if no conditions are true
}
switch statement: Selects one of many code blocks to execute based on the value of an expression.
switch (expression) {
case value1:
// code for value1
break;
case value2:
// code for value2
break;
default:
// code if no case matches
}
while loop: Repeats a block of code while a condition is true.
while (condition) {
// code to be executed
}
do-while loop: Similar to while, but it guarantees at least one execution of the loop body.
do {
// code to be executed
} while (condition);
for loop: Used when the number of iterations is known beforehand.
for (initialization; condition; increment/decrement) {
// code to be executed
}
Nested loops: Loops inside other loops.
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
// code to be executed
}
}
break statement: Exits from the current loop or switch case immediately.
break;
continue statement: Skips the remaining code in the current iteration and proceeds to the next iteration.
continue;
goto statement: Transfers control to a specific label.
goto label;
exit() function: Terminates the program immediately.
exit(0); // Terminate with success
These are the core control structures in C used for decision-making, looping, and other control flow operations.
Here are some questions based on Control Structures in C:
What will be the output of the following code?
int x = 10;
if (x > 5) {
printf("Greater than 5\n");
} else {
printf("Less than or equal to 5\n");
}
Write a C program that uses an if-else if ladder to check if a number is positive, negative, or zero.
Explain how the switch statement works. Write a program that takes an integer input and prints the corresponding day of the week (1 = Monday, 2 = Tuesday, etc.).
What is the difference between a while loop and a do-while loop in C? Provide an example of each.
Write a C program using a for loop to print the first 10 natural numbers.
Write a program that uses nested for loops to print a multiplication table from 1 to 5.
What will the following code output?
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue;
}
printf("%d ", i);
}
Write a C program using the break statement to stop the loop when a number greater than 50 is encountered in a sequence of numbers from 1 to 100.
Explain the goto statement and write a program that demonstrates its usage to jump to a specific label.
Write a C program to find the factorial of a number using a for loop.
Write a program that uses both if and for to check whether a number is prime.
These questions will help you practice and solidify your understanding of decision-making structures, loops, and other control flow statements in C.