Hello everyone, in this article, you are going to learn about the "for" loop in "c" programming language with easy and simple examples. The table of contents given below.
What is the "for" loop in "c" programming language?
The for loop is used to execute a section of code or sequence of statements repeatedly as long as the given condition is true. A for loop in c is divided into two parts, the first one is the control statement and the second one is the body of the loop. The control statement is a combination of initialization, condition, and increment or decrement. It directs the body of the loop to execute until the condition becomes false. The for loop in c programming is an entry control loop. In an entry control loop, the condition is checked, first and then the body of the loop is executed.
here the init-statement is the initialization part and the expression is the increment or decrement part.
for loop flowchart:
Let's understand the above flow chart:
step 1: Start
step 2: initialize the counter variable, suppose we have an integer variable named "i" then we can write i=any integer value. For example, i=0;
step 3: Test condition can be a specific value or can be another value. For example "i<10" is a specific value while "i<a" is another value.
step 4: If the condition is true then it will execute the loop body
step 5: After executing the loop body, the counter will be updated, for example, "++i" or "--i". Then it will again go to test condition.
step 6: If the condition becomes false the loop will be stopped.
Basic things to remember when using for loop:
When you are using for loop in c programming, some basic things need to be remembered.
The control statement of the for loop is must be well defined otherwise it will execute infinite times. This situation occurs when either the termination condition is not defined or the termination condition is never meet.
The following Examples illustrates the for loop in C programming examples:
Example 1: Write a program in C to display the first 10 natural numbers using for loop
Output:
Example 2: Write a C program to find the sum of the first 10 natural numbers using for loop
Output:
1. Here we have declared two int variables among them we initialize value 0 to one variable.Example 3: Write a program in C to display n terms of natural number and their sum. using for loop
Output:
Example 4: Write a program in C to read 10 numbers from the keyboard and find their sum and average using for loop
Output:
1. Here we have declared three int variables and one float variable among them we initialize value 0 to one variable.
Example 5: Write a program in C to display the cube of the number up to give an integer by the user using for loop
Output:
1. Here we have declared three int variables and one float variable among them we initialize value 0 to one variable.
Explanation:
The value entered by the user is stored in the input variable. Suppose the user entered 3.The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we perform multiplication three times between the n and stored it in the result variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 4. When the n is 4, the test expression is evaluated to false, and the loop terminates.
The value entered by the user is stored in the input variable. Suppose the user entered 3.The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we perform multiplication three times between the n and stored it in the result variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 4. When the n is 4, the test expression is evaluated to false, and the loop terminates.
Example 6: Write a program in C to display the multiplication a given integer, using for loop
Output:
1. Here we have declared three int variables and one float variable among them we initialize value 0 to one variable.
2. We take the input Table number from the user and stored it into an integer variable.
3. In for loop, we initialize 'n' with value 1. In the condition part, we specified our condition and then the incremental part.
4. In the body of the loop we specified our condition to calculate the Table of a given number and print the values one by one.
Explanation:
The value entered by the user is stored in the input variable. Suppose the user entered 12.The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we perform multiplication between the result and n and stored it in the result variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 11. When the n is 11, the test expression is evaluated to false, and the loop terminates.
The value entered by the user is stored in the input variable. Suppose the user entered 12.The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we perform multiplication between the result and n and stored it in the result variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 11. When the n is 11, the test expression is evaluated to false, and the loop terminates.
Example 7: Write a program in C to display the n terms of odd natural number and their sum using for loop
Output:
1. Here we have declared four int variables and one float variable among them we initialize value 0 to one variable.
2. We take input the nth term from the user and stored it into an integer variable.
3. In for loop, we initialize 'n' with value 1. In the condition part, we specified our condition and then the incremental part.
4. In the body of the loop we specified our condition to calculate the Odd natural numbers and print them one by one.
Explanation:
The value entered by the user is stored in the input
variable. Suppose the user entered 5.
The n
is initialized to 1 and test expression is evaluated. Since the test expression is true the body of the for loop executed, in the body first we calculate the odd natural number and then stored it in the result variable after that we print the result in the console then we perform addition between the result and sum and stored in the sum variable. After executing the body of the loop the update statement is executed and n will equal 2. Again the test expression is evaluated.
This process goes on until the n reaches 6. When the n is 6, the test expression is evaluated to false, and the loop terminates.
0 Comments