In C, a for loop is a control flow statement that allows you to repeatedly execute a block of code based on a specified condition. The syntax for a for loop in C is as follows:
for (initialization; condition; increment/decrement) { // code to be executed repeatedly }
Here’s an explanation of the different parts of the for loop:
- Initialization: This part is executed only once before the loop starts. It is typically used to initialize a loop control variable.
- Condition: The condition is evaluated before each iteration of the loop. If the condition is true, the loop continues executing; otherwise, the loop is terminated.
- Increment/Decrement: After each iteration of the loop, the increment or decrement statement is executed. It is used to modify the loop control variable.
- Code to be executed repeatedly: This is the block of code that will be executed repeatedly as long as the condition is true.
Here’s an example of a for loop that prints the numbers from 1 to 5:
#include <stdio.h> int main() { int i; for (i = 1; i <= 5; i++) { printf("%d\n", i); } return 0; }
Output:
1 2 3 4 5
In this example, the loop control variable i
is initialized to 1. The loop continues as long as i
is less than or equal to 5. After each iteration, i
is incremented by 1. The printf
statement inside the loop prints the value of i
, and the loop continues until the condition becomes false.
Syntax of for loop in C:
Certainly! Here’s the syntax of a for loop in C:
for (initialization; condition; increment/decrement) { // code to be executed }
Certainly! Here’s the syntax of a for loop in C:
for (initialization; condition; increment/decrement) {
// code to be executed
}
Let’s break down each part of the syntax:
- Initialization: It is an optional part where you can initialize variables or set an initial value. It typically declares a loop control variable and assigns an initial value to it. This part is executed only once at the beginning of the loop.
- Condition: It specifies the condition that is checked before each iteration of the loop. If the condition evaluates to true (non-zero), the loop body is executed. If the condition evaluates to false (zero), the loop is terminated, and control moves to the next statement after the loop.
- Increment/Decrement: It is used to modify the loop control variable after each iteration. It can be an increment (
i++
ori += 1
) or a decrement (i--
ori -= 1
) operation or any other valid expression. - Code to be executed: It represents the block of code that will be executed repeatedly as long as the condition is true. It can be a single statement or a compound statement (a group of statements enclosed in curly braces).
The for loop follows the sequential flow: initialization -> condition -> code execution -> increment/decrement -> condition -> code execution -> increment/decrement -> … and so on, until the condition becomes false.
It’s worth noting that all parts of the for loop are optional, but the semicolons (;) are required.
Here’s an example that demonstrates the syntax of a for loop:
#include <stdio.h> int main() { int i; for (i = 0; i < 5; i++) { printf("Iteration %d\n", i); } return 0; }
Output:
Iteration 0 Iteration 1 Iteration 2 Iteration 3 Iteration 4
In this example, i
is initialized to 0, the condition i < 5
is checked before each iteration, and i
is incremented by 1 after each iteration. The loop body prints the current value of i
until the condition becomes false.
Flowchart of for loop in C:
Sure! Here’s a simple flowchart representation of a for loop in C:
+------------------------------------+ | Initialization | | | | +------------------+ | | | Condition | | | | (True) | | | | | | | +------------------+ | | | | | v | | +------------------+ | | | Code Block | | | | | | | +------------------+ | | | | | v | | +------------------+ | | | Increment/ | | | | Decrement | | | +------------------+ | | | | | v | | +------------------+ | | | Condition | | | | (Re-evaluate) | | | | | | | +------------------+ | | | | | v | +-------<---(Condition False)--------+ | v +----------+ | Exit | +----------+
The flowchart represents the sequential flow of a for loop in C. It starts with the initialization step, followed by the evaluation of the condition. If the condition is true, the code block is executed. After executing the code block, the increment or decrement step is performed, and then the condition is evaluated again. This process continues until the condition becomes false. Once the condition is false, the loop is exited, and the control moves to the next statement after the loop.
Note that the flowchart assumes a standard for loop structure with initialization, condition, and increment/decrement steps. However, in C, each part of the for loop is optional, and you can have different variations based on your requirements.
C for loop Examples:
Certainly! Here are a few examples of for loops in C:
- Printing numbers from 1 to 10:
#include <stdio.h> int main() { int i; for (i = 1; i <= 10; i++) { printf("%d ", i); } return 0; }
Output:
1 2 3 4 5 6 7 8 9 10
- Computing the factorial of a number:
#include <stdio.h> int main() { int n, factorial = 1; printf("Enter a positive integer: "); scanf("%d", &n); for (int i = 1; i <= n; i++) { factorial *= i; } printf("Factorial of %d = %d\n", n, factorial); return 0; }
Sample Output:
Enter a positive integer: 5 Factorial of 5 = 120
- Printing a triangle pattern:
#include <stdio.h> int main() { int rows = 5; for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf("* "); } printf("\n"); } return 0; }
Output:
* * * * * * * * * * * * * * *
- Summing up the elements of an array:
#include <stdio.h> int main() { int numbers[] = {5, 2, 7, 9, 1}; int size = sizeof(numbers) / sizeof(numbers[0]); int sum = 0; for (int i = 0; i < size; i++) { sum += numbers[i]; } printf("Sum = %d\n", sum); return 0; }
Output:
Sum = 24
These examples demonstrate different use cases of the for loop in C, such as printing numbers, performing calculations, creating patterns, and iterating over array elements. Feel free to modify and experiment with these examples to further understand the behavior of for loops in C.
C Program: Print table for the given number using C for loop
Certainly! Here’s a C program that uses a for loop to print the table for a given number:
#include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); printf("Table for %d:\n", number); for (int i = 1; i <= 10; i++) { printf("%d x %d = %d\n", number, i, number * i); } return 0; }
In this program, the user is prompted to enter a number. The for loop iterates from 1 to 10, and for each iteration, it multiplies the given number with the loop variable i
to compute the product. The result is printed in the format “number x i = product”.
Sample Output:
Enter a number: 7 Table for 7: 7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
You can modify the program to compute the table for any number by changing the value of the number
variable or allowing the user to input the number.
Loop body:
In the context of a for loop in C, the loop body refers to the block of code that is executed repeatedly as long as the loop condition is true. It is the portion of the code that lies within the curly braces {}
following the for loop’s opening parenthesis.
The loop body can consist of a single statement or a compound statement (a group of statements enclosed within curly braces). You can have multiple statements within the loop body, and they will be executed sequentially each time the loop iterates.
Here’s an example demonstrating the loop body within a for loop:
#include <stdio.h> int main() { for (int i = 0; i < 5; i++) { printf("Iteration %d\n", i); printf("Loop body statement 1\n"); printf("Loop body statement 2\n"); } return 0; }
In this example, the loop body consists of three statements: two printf
statements and an increment statement (i++
). These statements are executed repeatedly as long as the loop condition i < 5
is true. Each time the loop iterates, the statements within the loop body are executed sequentially.
Output:
Iteration 0 Loop body statement 1 Loop body statement 2 Iteration 1 Loop body statement 1 Loop body statement 2 Iteration 2 Loop body statement 1 Loop body statement 2 Iteration 3 Loop body statement 1 Loop body statement 2 Iteration 4 Loop body statement 1 Loop body statement 2
Feel free to modify the loop body to include the desired statements or code logic that you want to execute repeatedly within the for loop.
Infinitive for loop in C:
An infinite for loop is a loop that continues executing indefinitely, as there is no condition that can become false to terminate the loop. In C, you can create an infinite for loop by omitting the loop condition entirely or by providing a condition that always evaluates to true.
Here’s an example of an infinite for loop in C:
#include <stdio.h> int main() { for (;;) { printf("This is an infinite loop.\n"); } return 0; }
In this example, the for loop does not have any initialization, condition, or increment/decrement expressions. The absence of the condition expression results in an infinite loop. The loop body simply prints the statement “This is an infinite loop.” It will continue executing indefinitely until the program is terminated externally, such as by pressing Ctrl+C.
When running this program, you will see the message “This is an infinite loop.” repeatedly until you forcefully terminate the program.
It’s important to note that infinite loops should be used with caution, as they can lead to programs that do not terminate naturally. They are typically used in specific scenarios where the program’s execution is intentionally meant to be continuous or until a specific termination condition is encountered.