A while loop in C programming repeatedly executes a target statement as long as a given
condition is true. if our given condition is true it will print what we are given
inside the loop. if our given condition is not true it will come outside of the loop. mainly for
the loop, it's used to reduce the code. when we are running 10 set of print of statement its
take more time to write when thinking in micro sec or non0 sec time, not a minute. In while
loop its provide the option so ,you can write the code statement inside of the
loop and give the range how much of the numbers you want to print . it's simple set its take
few seconds to execute. Once your are going to big level of industry or any competitive
exam all the code system made by some loops . better be familiar with loops .
syntax
while(condition) {
statement(s);
}
flow chart:
this the flow chart for while loop.
program:
#include<stdio.h> # include<conio.h>
void main () {
int a = 10;
while( a < 20 ) {
printf("value of a:
%d\n", a);
a++;
}
getch();
}
|
output:
value of a :10 11 12 13 14 15 16 17 18 19
|
this our output .now we see how it will execute, first we should give one condition for
while our entireprocess of execution is based on your given condition. when you give the
compiling your program first check your given condition. In this program, our given
condition is (a<20), we have already givena=10, then our compiler check our given
condition is it true it will print the inside loop what operationwe have given that will
executed , then it will print the out put.
A do-while loop is similar to the while loop except for the fact that it is guaranteed to execute a
fast one time. The flow of execution is also similar to the while loop.If our condition is true ,it will go to the
inside loop. If our condition is false, it will go to the outside of the loop.
But the stracture of the do-while is totally different from normal while loop.
syntax
see this syntax, In while loop our given condition should be present in the first. But in do-while, our condition value should be present at the end of the loop. In do while do is a keyword it contains only statements. then, one important thing is after do -keyword while condition should be present with a semicolon.
flow chart
this the flowchart of do-while.
program:
#include<stdio.h> #include<conio.h> void main() { int a=10; do{ print("value of a:%d\n',a); a++; }while(a<20); getch(); } |
output:
value of a:11 12 13 14 15 16 17 18 19 |
this the output of the our program. Now we see how it will execute. we have already know the do-while flow of compiling similar to the while loop. The structure only changed. Then first, while loop checking our condition , in this program our given condition is (a<20) ,our given integer is a=10;, then our condition will be checked if it is True it will go to the inside loop. In the inside loop, our given condition is to print the values of a. When the value is reached 20 our condition is going to false , so it will come the outside of the loop.
A for loop is a repetition control structure. that allows you to efficiently write a loop that needs to execute a specific number of times. It is used to execute a set of instructions repeatedly until the given condition becomes false.
The for loop has three parts. they are,
-
initialization: it is used to initialize a counter variable.
-
condition: it is used to test the condition every time.
-
increment/decrement: it is used to alter increment or decrement of the counter
variable.
syntax:
for(initialization;condition;increment/decrement)
{
statements;
}
flow chart:
program:
#include<stdio.h> #include<conio.h> void main() { int n=5,i ; for(i= 0; i<n ; i++ ) { print("%d\n",i); } getch(); }
|
output:
now see how it will execute. When we start compiling our program first it will start the compilation at the first part initialization(i=0) then it will go to the second part, in the second it is checking the condition. then, our compiler goes to check the condition, if our condition is true it will go to the inside of the loop. Then inside the loop, we have already given one statement it prints the value of the n numbers. Once our first iteration is done. Next, it will do increment operation(i++), then again it is beginning to check the condition, the condition is true it will print the value. in the same process will continue until the condition is false. for loop its reduce the time and make code simple and very unique. you wanna become a good developer, first, you should know how to write the big logic statement code into a unique small kinda code. usually most of the time for loop used for all the various kind of work in IT industry
No comments:
Post a Comment