In C programming, conditions are used to make decisions and control the flow of execution based on whether a certain condition evaluates to true or false. Conditional statements allow a program to make decisions, execute code based on those decisions, and control the execution flow.
There are several types of conditionals in C:
if statement
if-else statement
else if ladder
switch statement
Ternary (Conditional) operator
Let's go through the syntax and uses of each of these conditionals:
The if statement is used to execute a block of code if a specified condition is true.
Syntax:
if (condition) {
// Code to be executed if condition is true
}
Explanation:
condition is an expression that is evaluated. If the condition evaluates to true (non-zero), the code block inside the if statement is executed.
If the condition is false (zero), the code inside the block is skipped.
Example:
int main() {
int a = 5;
if (a > 0) {
printf("a is positive\n");
}
return 0;
}
Output:
a is positive
The if-else statement provides an alternative code block that is executed when the condition is false.
Syntax:
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
Explanation:
If the condition is true, the code inside the first block is executed.
If the condition is false, the code inside the else block is executed.
Example:
int main() {
int a = -5;
if (a > 0) {
printf("a is positive\n");
} else {
printf("a is negative or zero\n");
}
return 0;
}
Output:
a is negative or zero
The else if ladder is used when there are multiple conditions to check. It allows for several conditions to be evaluated sequentially.
Syntax:
if (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition2 is true
} else if (condition3) {
// Code to be executed if condition3 is true
} else {
// Code to be executed if none of the above conditions are true
}
Explanation:
The if statement is checked first.
If the first condition is false, the next else if condition is checked.
If none of the if or else if conditions are true, the code inside the else block will be executed.
Example:
int main() {
int a = 0;
if (a > 0) {
printf("a is positive\n");
} else if (a < 0) {
printf("a is negative\n");
} else {
printf("a is zero\n");
}
return 0;
}
Output:
a is zero
The switch statement is used to select one of many code blocks to be executed based on the value of a variable. It is often used when a variable needs to be compared against several possible constant values.
Syntax:
switch (expression) {
case value1:
// Code to be executed if expression == value1
break;
case value2:
// Code to be executed if expression == value2
break;
case value3:
// Code to be executed if expression == value3
break;
default:
// Code to be executed if none of the above cases match
}
Explanation:
The expression is evaluated, and its value is compared against each case.
If a case matches the value of the expression, the corresponding code block is executed.
The break statement is used to exit the switch statement. Without break, the program would continue executing the next case statements (this is known as "fall through").
The default block is optional and is executed if none of the case values match the expression.
Example:
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Output:
mathematica
Copy code
Wednesday
The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values based on whether the condition is true or false.
Syntax:
c
Copy code
condition ? expression1 : expression2;
If the condition is true, expression1 is evaluated and returned.
If the condition is false, expression2 is evaluated and returned.
Example:
c
Copy code
#include <stdio.h>
int main() {
int a = 5, b = 10;
int max = (a > b) ? a : b; // Ternary operator
printf("The maximum number is: %d\n", max);
return 0;
}
Output:
csharp
Copy code
The maximum number is: 10
if statement: Executes a block of code if the condition is true.
if-else statement: Executes one block of code if the condition is true, and another block if it is false.
else if ladder: Allows checking multiple conditions in sequence.
switch statement: A multi-way branch statement for comparing a variable to constant values.
Ternary (Conditional) Operator: A compact form of if-else to return one of two values based on a condition.
Conditionals are used to control the flow of the program based on boolean conditions.
if, else if, and switch statements help in making decisions.
The ternary operator is a concise way to handle simple if-else conditions.
This program uses the if statement to check if a number is positive or negative.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is positive.\n");
}
return 0;
}
Output (for input 5):
csharp code
The number is positive.
This program uses if-else to determine if a number is even or odd.
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
Output (for input 4):
csharp code
The number is even.
This program compares two numbers and prints the largest.
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
printf("%d is the largest number.\n", num1);
} else {
printf("%d is the largest number.\n", num2);
}
return 0;
}
Output (for input 5 and 3):
csharp code
5 is the largest number.
This program uses an if-else-if ladder to assign a grade based on marks.
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 75) {
printf("Grade: B\n");
} else if (marks >= 50) {
printf("Grade: C\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Output (for input 85):
makefile code
Grade: B
This program uses the switch statement to print the name of the day based on input.
int main() {
int day;
printf("Enter a number between 1 and 7: ");
scanf("%d", &day);
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
Output (for input 3):
mathematica code
Wednesday
This program uses if to check whether a year is a leap year or not.
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Output (for input 2020):
csharp code
2020 is a leap year.
This program uses else if to check if a person is eligible to vote based on their age.
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote.\n");
} else if (age >= 16) {
printf("You are eligible for a learner's permit.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0;
}
Output (for input 20):
css code
You are eligible to vote.
This program implements a basic calculator using a switch statement.
int main() {
int num1, num2, result, choice;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Enter your choice (1 for addition, 2 for subtraction, 3 for multiplication, 4 for division): ");
scanf("%d", &choice);
switch (choice) {
case 1: result = num1 + num2; printf("Result: %d\n", result); break;
case 2: result = num1 - num2; printf("Result: %d\n", result); break;
case 3: result = num1 * num2; printf("Result: %d\n", result); break;
case 4: if (num2 != 0) {
result = num1 / num2;
printf("Result: %d\n", result);
} else {
printf("Cannot divide by zero.\n");
}
break;
default: printf("Invalid choice.\n");
}
return 0;
}
Output (for input 5, 3, 1):
makefile code
Result: 8
This program checks whether a number is a palindrome.
int main() {
int num, reversed = 0, original, remainder;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed) {
printf("The number is a palindrome.\n");
} else {
printf("The number is not a palindrome.\n");
}
return 0;
}
Output (for input 121):
csharp code
The number is a palindrome.
This program classifies a person’s age group.
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age <= 12) {
printf("You are a child.\n");
} else if (age <= 19) {
printf("You are a teenager.\n");
} else if (age <= 60) {
printf("You are an adult.\n");
} else {
printf("You are a senior.\n");
}
return 0;
}
Output (for input 25):
sql code
You are an adult.
if: Used to execute a block of code if a condition is true.
else: Used in conjunction with if to execute a block of code if the if condition is false.
else if: Used to check multiple conditions in a sequence.
switch: Used for executing one out of many possible blocks of code based on the value of a variable.
In C programming, loops are used to execute a block of code repeatedly until a specified condition is met. There are three main types of loops in C:
for loop
while loop
do-while loop
Each of these loops has a specific use case, and understanding when to use each is key to writing efficient code.
The for loop is typically used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and increment/decrement.
Syntax:
c
Copy code
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
Explanation:
Initialization: Sets the initial value of the loop variable.
Condition: The loop continues executing as long as this condition evaluates to true (non-zero).
Increment/Decrement: Updates the loop variable after each iteration.
Example:
c
Copy code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("Iteration: %d\n", i);
}
return 0;
}
Output:
makefile
Copy code
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Use Case:
The for loop is ideal when you know how many times you want to iterate.
The while loop is used when the number of iterations is not known and depends on a condition that is evaluated before each iteration. The loop continues to run as long as the condition is true.
Syntax:
c
Copy code
while (condition) {
// Code to be executed repeatedly
}
Explanation:
Condition: The condition is checked before each iteration. If the condition evaluates to true, the code inside the loop is executed.
The loop terminates when the condition evaluates to false.
Example:
c
Copy code
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("Iteration: %d\n", i);
i++; // Increment to avoid infinite loop
}
return 0;
}
Output:
makefile
Copy code
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Use Case:
The while loop is used when the number of iterations is not predetermined, and the loop continues as long as a specific condition is met.
The do-while loop is similar to the while loop, but with a key difference: the condition is evaluated after each iteration. This ensures that the code inside the loop is executed at least once, regardless of the condition.
Syntax:
c
Copy code
do {
// Code to be executed at least once
} while (condition);
Explanation:
Condition: After each iteration, the condition is evaluated. If it evaluates to true, the loop continues.
The loop will always execute the code inside the block at least once, even if the condition is false initially.
Example:
c
Copy code
#include <stdio.h>
int main() {
int i = 1;
do {
printf("Iteration: %d\n", i);
i++; // Increment to avoid infinite loop
} while (i <= 5);
return 0;
}
Output:
makefile
Copy code
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Use Case:
The do-while loop is useful when you want the loop to run at least once, such as in menu-driven programs or user-input validation, where the program should prompt the user for input at least once.
break:
The break statement is used to exit the loop immediately, regardless of the condition. It can be used in for, while, and do-while loops.
Example:
c
Copy code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("Iteration: %d\n", i);
}
return 0;
}
Output:
makefile
Copy code
Iteration: 1
Iteration: 2
continue:
The continue statement is used to skip the remaining code in the current iteration and proceed to the next iteration of the loop.
Example:
c
Copy code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip iteration when i is 3
}
printf("Iteration: %d\n", i);
}
return 0;
}
Output:
makefile
Copy code
Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
goto:
The goto statement is used to transfer control to another part of the program, generally labeled as a specific point. It's usually discouraged, but can be used for skipping out of nested loops or breaking out of deeply nested conditions.
Example:
c
Copy code
#include <stdio.h>
int main() {
int i = 0;
start:
i++;
if (i <= 5) {
printf("Iteration: %d\n", i);
goto start; // Jump back to start
}
return 0;
}
Output:
makefile
Copy code
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
for Loop:
Best for when the number of iterations is known beforehand.
while Loop:
Best for when the condition is evaluated before the loop body is executed, and the number of iterations is unknown.
do-while Loop:
Best when the loop body needs to execute at least once, regardless of the condition.
break Statement:
Exits the loop immediately, skipping all remaining iterations.
continue Statement:
Skips the rest of the current loop iteration and moves to the next iteration.
goto Statement:
Jumps to a specific point in the program. It is usually discouraged due to its potential to create unreadable and difficult-to-debug code.
By understanding and using these loops and control statements effectively, you can write flexible, efficient, and readable C programs.
Here are 10 live C programs demonstrating the use of different types of loops (for, while, and do-while) in various scenarios.
This program uses a while loop to print all even numbers between 2 and 20.
c
Copy code
This program demonstrates how a do-while loop can be used to print odd numbers from 1 to 19.
c
Copy code
This program calculates the sum of the first 10 natural numbers using a for loop.
c
Copy code
This program uses a while loop to count down from 10 to 1.
c
Copy code
This program demonstrates how to print the multiplication table of 5 using a do-while loop.
c
Copy code
This program uses a for loop to print prime numbers between 1 and 50.
c
Copy code
This program calculates the factorial of a given number using a while loop.
c
Copy code
Input: 5
This program finds the sum of digits of a number using a do-while loop.
c
Copy code
Input: 123
This program prints the Fibonacci series up to the 10th term using a for loop.
c
Copy code
for loop: Best used when the number of iterations is known beforehand (e.g., iterating over a range or array).
while loop: Suitable for situations where the loop continues as long as a condition is true, but the number of iterations may not be known in advance.
do-while loop: Similar to while, but ensures that the loop runs at least once, even if the condition is false initially.
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("Iteration: %d\n", i);
}
printf("Loop exited\n");
return 0;
}
Output:
Iteration: 1
Iteration: 2
Loop exited
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip when i is 3
}
printf("Iteration: %d\n", i);
}
return 0;
}
Output:
Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
int main() {
int i = 1;
start:
if (i <= 5) {
printf("Iteration: %d\n", i);
i++;
goto start; // Jump back to the start label
}
return 0;
}
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
void printMessage() {
printf("This is the first message.\n");
return; // Exit the function
printf("This message will not be printed.\n");
}
int main() {
printMessage();
return 0;
}
Output:
This is the first message.
int main() {
int i = 1;
while (i <= 5) {
if (i == 4) {
break; // Exit the loop when i is 4
}
printf("Iteration: %d\n", i);
i++;
}
return 0;
}
Output:
Iteration: 1
Iteration: 2
Iteration: 3
int main() {
int i = 1;
while (i <= 5) {
if (i == 3) {
i++;
continue; // Skip when i is 3
}
printf("Iteration: %d\n", i);
i++;
}
return 0;
}
Output:
Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
int main() {
int i, j;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
goto endLoop; // Jump to endLoop label
}
printf("i = %d, j = %d\n", i, j);
}
}
endLoop:
printf("Exited nested loops\n");
return 0;
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
Exited nested loops
int main() {
int choice = 2;
switch (choice) {
case 1:
printf("Choice 1 selected\n");
break; // Exit the switch statement
case 2:
printf("Choice 2 selected\n");
break;
case 3:
printf("Choice 3 selected\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Output:
Copy code
Choice 2 selected
int checkEvenOdd(int num) {
if (num % 2 == 0) {
return 1; // Return 1 if the number is even
}
return 0; // Return 0 if the number is odd
}
int main() {
int result = checkEvenOdd(4);
if (result == 1) {
printf("The number is even\n");
} else {
printf("The number is odd\n");
}
return 0;
}
Output:
The number is even
main() {
int choice;
start:
printf("Menu:\n");
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You selected Option 1\n");
goto start; // Return to the menu
case 2:
printf("You selected Option 2\n");
goto start; // Return to the menu
case 3:
printf("Exiting program\n");
break;
default:
printf("Invalid choice, please try again\n");
goto start; // Return to the menu
}
return 0;
}
Output:
Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 1
You selected Option 1
Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 3
Exiting program
break: Exits a loop or switch statement immediately.
continue: Skips the remaining code in the current iteration of a loop.
goto: Transfers control to a specific point in the program.
return: Exits from a function, optionally returning a value.
In C programming, jump statements are used to transfer control to a different part of the program. These statements allow you to skip certain parts of code, loop back, or even exit loops or functions under specific conditions. There are four primary jump statements in C:
break
continue
goto
return
Let's go through the uses and syntax of each of these jump statements.
The break statement is used to terminate the execution of a loop or switch statement. It immediately exits the current loop or switch, and the program continues with the next statement after the loop or switch.
Syntax:
c
Copy code
break;
Explanation:
When break is encountered, control is transferred outside the loop or switch block, terminating the loop or switch execution.
break is commonly used in loops to stop execution when a certain condition is met (e.g., search loops).
Example (in a for loop):
c
Copy code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("Iteration: %d\n", i);
}
printf("Loop exited\n");
return 0;
}
Output:
vbnet
Copy code
Iteration: 1
Iteration: 2
Loop exited
Use Case:
The break statement is commonly used to exit loops early based on a condition (e.g., when a desired value is found).
The continue statement is used to skip the remaining code in the current iteration of a loop and proceed with the next iteration. It only affects the current iteration and does not terminate the loop.
Syntax:
c
Copy code
continue;
Explanation:
When continue is encountered inside a loop, it skips the rest of the current iteration and moves to the next iteration of the loop.
It is useful when you want to skip some part of the loop under certain conditions but still continue with further iterations.
Example (in a for loop):
c
Copy code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip when i is 3
}
printf("Iteration: %d\n", i);
}
return 0;
}
Output:
makefile
Copy code
Iteration: 1
Iteration: 2
Iteration: 4
Iteration: 5
Use Case:
The continue statement is used when certain iterations of a loop need to be skipped based on a condition, but the loop should continue.
The goto statement is used to transfer control to a specific labeled point in the program. This allows the program to jump directly to another part of the code, bypassing the normal flow of control.
Syntax:
c
Copy code
goto label;
...
label:
// Code to jump to
Explanation:
goto transfers control to the label defined elsewhere in the program.
The label is defined by placing a name followed by a colon (:) at the target location.
goto is often discouraged because it can make the code less readable and harder to maintain. However, it can be useful in certain situations, such as breaking out of deeply nested loops.
Example:
c
Copy code
#include <stdio.h>
int main() {
int i = 0;
start:
i++;
printf("Iteration: %d\n", i);
if (i < 3) {
goto start; // Jump back to the start label
}
return 0;
}
Output:
makefile
Copy code
Iteration: 1
Iteration: 2
Iteration: 3
Use Case:
goto can be used to exit from deeply nested loops or functions quickly (but use it sparingly to avoid making the code hard to follow).
The return statement is used to exit a function and optionally return a value to the caller. It can be used to terminate a function's execution and return control to the calling function.
Syntax:
c
Copy code
return; // Used in functions with void return type
return expression; // Used in functions with non-void return type
Explanation:
Without value: If the function has a void return type, you can use return; to exit the function.
With value: If the function has a non-void return type (e.g., int, float), you can use return expression; to return a value to the caller.
Example (without return value):
c
Copy code
#include <stdio.h>
void printMessage() {
printf("Hello, World!\n");
return; // Exit the function
}
int main() {
printMessage();
return 0;
}
Output:
Copy code
Hello, World!
Example (with return value):
c
Copy code
#include <stdio.h>
int add(int a, int b) {
return a + b; // Return the sum of a and b
}
int main() {
int result = add(3, 4);
printf("Sum: %d\n", result);
return 0;
}
Output:
makefile
Copy code
Sum: 7
Use Case:
The return statement is essential for ending the execution of functions and returning control (and possibly a value) to the calling function.
It can also be used to exit early from functions if a certain condition is met.
break:
Exits the loop or switch statement immediately, transferring control to the statement after the loop or switch.
Use: When you need to exit a loop or switch based on a condition.
continue:
Skips the remaining code in the current iteration and proceeds to the next iteration of the loop.
Use: When you want to skip the rest of the loop's body for the current iteration.
goto:
Transfers control to a labeled statement in the program.
Use: Best used for exiting deeply nested loops or handling special cases, though generally discouraged due to potential to reduce code readability.
return:
Exits the current function and optionally returns a value to the calling function.
Use: To exit a function and, if applicable, return a result to the caller.
break and continue are mostly used in loops.
goto is used for jumping to specific points in the program but should be avoided to maintain code readability.
return is used for ending the function and optionally passing a value back to the caller.
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit loop when i is 5
}
printf("%d ", i);
}
return 0;
}
Explanation: The loop terminates when i equals 5, skipping numbers after that.
int main() {
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
return 0;
}
Explanation: The continue statement skips the rest of the loop's body when i is even.
int main() {
int num = 10;
if (num > 0) {
goto positive; // Jump to label if condition is true
}
printf("The number is negative.\n");
return 0;
positive:
printf("The number is positive.\n");
return 0;
}
Explanation: The goto statement skips directly to the labeled block.
int main() {
int day = 3;
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");
}
return 0;
}
Explanation: The break prevents fall-through in the switch case.
int main() {
int i = 0;
while (i < 10) {
i++;
if (i % 3 == 0) {
continue; // Skip multiples of 3
}
printf("%d ", i);
}
return 0;
}
Explanation: Numbers divisible by 3 are skipped using continue.
int main() {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 3) {
break; // Exit the inner loop when j is 3
}
printf("i=%d, j=%d\n", i, j);
}
}
return 0;
}
Explanation: The break exits only the inner loop when j == 3.
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == j) {
goto end; // Jump out of both loops when i equals j
}
printf("i=%d, j=%d\n", i, j);
}
}
end:
printf("Exited the loops.\n");
return 0;
}
Explanation: The goto jumps out of nested loops when a condition is met.
int main() {
int count = 0;
while (1) { // Infinite loop
printf("%d ", count);
count++;
if (count == 5) {
break; // Exit the loop when count reaches 5
}
}
return 0;
}
Explanation: The loop is infinite but terminates when the condition inside the loop is met.
int main() {
int i = 0;
do {
i++;
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
} while (i < 10);
return 0;
}
Explanation: The do-while loop continues even when the continue statement is used.
int main() {
int num = 29, isPrime = 1;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = 0; // Not a prime number
break;
}
}
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}
Explanation: The break exits the loop as soon as a divisor is found, optimizing the prime-checking process.
break: Used to exit loops or switch cases based on conditions.
continue: Skips the current iteration and proceeds to the next one.
goto: Jumps to a labeled block of code.