Header Ads

While loop:C Programming Language - Part 4.2

While



While the normal form of the loop is:

While (condition) statement

A condition is given as a condition. As long as the condition is true, the while loop will continue. Write a small program

1
2
3
4
5
6
7
8
9
#include <stdio.h>
Int main () {
  
Int number = 0;
While (number <= 9) {
        Printf ("% d \ n", number);
        Number ++;
    }
}
In the above program we have got an integer variables called number. The primary value of which is 0. Then we're writing while loop. (Number <= 9) This is the condition. As long as the condition is true, the {{...} code inside the bracket will be executed. In the bracket we first printed the number. Next line number ++; We have increased the number one by one.


First, the value of the variable variable was 0 ... first when entering the inside of the loop printed the number. Then number ++; Nambarar's value added one with. Now the value of the number variable is 1 again and again the first of the loop returned. Going and checking the condition (number <= 9). What is the value of variable number 9 or less? Since the number of the number of variables is 1, and smaller than 9. So again inside the loop. And print again. Now print again 1. number ++; Value will increase by one, the value will be 2 And again check the loop first. When you see it is smaller than 9, enter the loop. And again print the number. 10 times into the loop.

The last time when the number ++ increases one value, the value of the number variable is 10 and when the condition is checked, then the value of the variable number 9 is greater than 9. And while the while loop will not work. The program will end.


We can write another program. At times, sometimes mischievously, the teacher or the house would have written 100 times, "I will not do any other thing." Then if we knew programming, we would not have had much trouble. Writing a few lines of code would have been written 100 times. It could be written 1,000 times or 1 million times. Enter this program with the while loop. I wrote in the program "ami r dustumi korbo na"

1
2
3
4
5
6
7
8
9
#include <stdio.h>
Int main () {
  
Int number = 1;
While (number <= 100) {
        Printf ("ami r dustumi korbo na. \ N");
        Number ++;
    }
}

When I run the program, the console can be written 100 times, I will not do the same thing.

Here's the same thing as the previous program. Number variables printed in the previous program. Now a sting has been printed.

After printing the sting every time the number variable has been incremented one by one. And while the first of the while loop, the condition has been checked. As long as the value of the number is less than 100 or less, the loop is going on. And once the value of the number is 101, the work of the loop is over.


We can write another program with the while loop. From 1 to 100 we can print odd numbers. See the following program

1
2
3
4
5
6
7
8
9
#include <stdio.h>
Int main ()
{
Int number = 0;
While (number <= 100) {
    Printf ("% d \ n", number);
    Number = number + 2;
    }
}
One will add two from one and the other will be three. An odd number. Together with the three, we will add two to five, another odd number. Thus, if we increase the value of variable every time, we can get odd numbers. It has been done in the above program. Number = number + 2; Each time the number of variable variables has been increased by two.

First, the value of the variable was 1, int number = 1; I'm talking about it. Then enter the loop inside. And the condition is checked. Since the conditions are true, so the code inside the loop is run. Number printed. In the next line, the value of the number variable is increased by two. Thus, once the value of the number variable was greater than 100, the loop was finished.


Can we find out the number of numbers by modifying the above program? Then do it. You can then let me know in the comments.


No comments

Theme images by sebastian-julian. Powered by Blogger.