Header Ads

For-loop:C Programming Language - Part 4.4

 For-loop



Looping is used most commonly for loop There are three parts for a for loop. Before that we did not see for loop general usage rules.

For (exprission1; Exprission2; Expression3) Statement

Behind every expression here; (Semicolon).

The first exprission1 here is the first part of the for loop. It is provided by a primary value. Which is called the initial part. It controls the entire looping process.

The second part is given by a condition called Exprission2. It determines how long the loop will run. Exprission2 usually has a logical expression that can only understand the true falsity. If it is true then returns 1 and if it is a lie, then return 0. If it returns a value other than 0 then the loop will run, and if 0 returns, then the loop will not continue anymore.

Expression3 is the work that we initially assimilated with the initial value of what we want to modify. It works in the last step of each loop.

And before I say the loop will continue as long as Exprission2 does not lie or returns 0.

We know so much about the for loop, how to use it in the program. For this, write a program that prints the number from 1 to 10.
The following program is for you. Find out how its output will be.

1
2
3
4
5
6
7
#include <stdio.h>
Int main (void)
{
Int i;
For (i = 0; i <= 10; i ++)
    Printf ("% d \ n", i);
}
It's put out: 0 1 2 3 4 5 6 7 8 9 10 Here we have got an integer variable. For the first expression of loop we took the initial value of 0. The primary value is for the second part of the loop, ie the logical part is not validated and the statement inside the for loop will be executed. And the first Expression job will end. There is no other work.

After printing, the third part of the word "Exprission3" here will be modified by the i ++ portion. We know i ++ means i = i + 1 so here i will increase the value of i and will be 0 to 1. The second part of this expression will come here and i <= 10 will be logical verification by part. Here we will verify that the value of i is not less than 10 or 10. Since the value of i is less than 10, the loop will run again. And second time I will print 1. Again, Exprission3 will come to Modify. I got the value of i from the previous loop 1 now 1 will increase with 1 to one (by the part i ++).

Again the second part i <= 10 will be logical verification. Since 2 is less than 10, then print 2. Thus, it will end every step. When the value of i increases to 11, the loop will not work. And our program will end. Well, write another program. If you were punished in a short time, we would not have been punished for writing 100 times. We will write it now and write the program. And using the for loop.

1
2
3
4
5
6
7
8
#include <stdio.h>
Int main (void)
{
Int i;
For (i = 0; i <= 100; i ++)
    Printf ("ami r dustumi korbo na. \ N");
Return 0;
}
Here's a little different from the previous program, we've printed the value of i. Here we have printed a line "ami r dustumi korbo na." For loop inside Exprission2 i <= 100 So the value of i will be 100 until this loop will run. We have increased the value of i in Exprission3 to 1 per time.
Now we write another program. Now write a program to find out the number of odd numbers from one to fifty.

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
Int main (void)
{
Int i;
For (i = 0; i <= 50; i ++)
    {
    If (i% 2 == 1)
        Printf ("% d \ n", i);
    }
Return 0;
}
As we did earlier, we printed the value of i. But I have given a condition here. For loop enter every time inside the loop. After entering, check a condition with if. If (i% 2 == 1) means that if i divided by i, the share is the last one, then if the statement inside the condition is printf ("% dn", i); I will print the value of i. Otherwise nothing will happen. Here's what I'm doing, another loop inside a loop.

If we wish we could use one in another inside the loop, another one would like it. As we will now use another for loop inside of a for loop. But before that, let's see how the above program can be written more easily. If we wish, we can find out the odd numbers from 1 to 50 with the following:

1
2
3
4
5
6
7
8
9
#include <stdio.h>
Int main (void)
{
Int i;
For (i = 1; i <= 50; i = i + 2)
    Printf ("% d \ n", i);
Return 0;
}
Here I have assumed the first value of i. One odd number. It will print it. Then add 2 to 1 and then it will be 3, it's odd number. It will print it. Then 3 will be added to 3, the odd number is 5. It will print it. We are writing on the expression of loop i = i + 2 I have conditioned the expression in loop i <= 50 That means until the value of i is greater than 50, the loop will continue.


Now type a program using another for loop inside a for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
Int main (void) {
Int i, j;
For (i = 0; i <= 5; i ++) {
    For (j = 0; j <= i; j ++) {
        Printf ("% d", j);
    }
    Printf ("\ n");
  }
Return 0;
}
Here we have got two variables. I and j In the first for the inside of the loop, I gave an initial value of 0. I gave the condition i <= 5 and increased the value of i to 1. This means the first for loop will be executed 5 times. The program outputs Dibah



1
2
3
4
5
6
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
  In the second for the loop, I gave an initial value of 0 with j = 0, then the condition with j <= i. And at the end j ++ has increased the value of j. According to the condition, the second instrument will be executed depending on the first for loop. Like the first time i value 0 So for the first time the second for the loop will run. The second time i is the value of 1, so the second loop will run two times. For the third time the second for the loop will run 3 times. The fifth loop will run 5 times for the fifth time. The second loop will print for the first time 0, print the second time 0 1. Third time 0 1 2. This will print the fifth time 0 1 2 3 4 5. Now if we change the condition of the first for loop, then give the output:

Nested for demo

By doing do we have written a program to find out the average number of numbers. Now we will type it using the for loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
Int main ()
{
    Int total_no, count;
    Float number, average, sum = 0;
    Printf ("how many numbers?");
    Scanf ("% d", & total_no);
    For (count = 1; count <= total_no; count ++) {
        Printf ("Enter number% d:", count);
        Scanf ("% f", & number);
        Sum + = number;
    }
    Average = sum / total_no;
    Printf ("Average is:% f \ n", average);
Return 0;


No comments

Theme images by sebastian-julian. Powered by Blogger.