Do while loop in c
Hello friends,
Today we will read what is ‘for loop and do-while loop in c programming’, how to use it in c programming, and will also see it's an excellent example. In the previous class, we had learned about the while-loop. If you have not read it yet, then please first read it, then read this post.
Let’s start
What is Do While Loop in C Language?
Do While Loop in C Language: This is the second Loop used in "C" language. Like all other Loop, three basic statements are required, the initial value, final value, and step size of Loop's Variable. The feature of this Loop is that it checks at the end of the Condition Loop to be checked.
That is, when we need to execute the statement or Statement Block of Loop in the program at least once, then we use this Loop. Syntax of this Loop is as follows:
Variable Declaration;
Value Initialization;
do
{
Statement Block;
Step Size;
} while (Condition);
Other code;
How do while works in c
This Loop starts with do Keyword and are given a while condition after closing the middle bracket after the Statement Block. Also, this is the Loop, with the condition of while after the bracket; (Semi-Colon) is used. No Colon or Semi-Colon is used after do.
In this Loop, as soon as the do keyword is received to the program control, the program control goes directly into the statement block of do and executes the statements are written in it. Then the step size of Variable running Loop does as per Increase or Decrease program.
When the Program Control comes out of this block, it gets the while condition. Here if Condition is true then Program Control executes the Statements back from do and if Condition is false then Program Control does not iterate back to the Loop but goes directly to code.
Now let see some best and easy example through this you can understand the use of do while in c
Example:
Here, I will print 20 numbers in series with the help of do-while loop.
#include
#include
void main()
{
int x=1;
clrscr();
printf("Your 20 numbers are given below");
do
{
printf("%d\n",x);
x++;
}
while(x<=20);
getch();
}
Output
1,2.3.4............20.
Now we will read about the use of ‘for’ loop in c
What is ‘for’ Loop in C Language
for Loop in C Language: This is the most commonly used Loop. This Loop uses "C" for Key Word. In this Loop, all three parts mentioned above have to be written in the same bracket. The feature of this Loop is that all the statements that it contains are written in a block under the middle bracket after writing for Loop, and this Statements Block is executed only when the condition is true.
Syntax of for loop is as follows-
for (Initial Part; Conditional Part; Step Size Part)
{
Statements Block;
}
Other code
How we can use ’for’ loop in c?
When there is the execution of for Loop, the first is the Initialize Variable of Loop and then the Condition Check is done. If the condition is true, then the program goes to Control for Loop's Statement Block and executes the statements there.
When for Loop executes all the Statements of the Statement Block, it executes the Step Size Part of the Loop before exiting the Block and increments or Decrements the value of Variable according to the Size specified. Then checks the condition back if the condition is true then goes back to the Statement Block and after executing all the Statements, executes the Step Size Part back.
This the sequence continues as long as the condition for For loop remains true. Initialization of Loop occurs only once when entering Program Control for Loop for the first time. Execution of for Loop is always in this order.
Now let see some best and easy example through this you can understand the use of For loop in c
Example ;
Here, I will print 20 numbers in series.
#include
#include
void main()
{
int x;
clrscr();
printf("Your 20 numbers are given below");
for(x=1;x<=20;x++)
{
printf("%d\n",x);
}
getch();
}
Output1,2,3,.......20.
Thanks for reading this post
Also, read
(https://grapsvs.blogspot.com/2020/08/for-loop-in-c-do-while-loop-in-c.html)
(https://grapsvs.blogspot.com/2020/07/while-loop-in-c-programming-example.html)
0 Comments
Please do not enter any spam link in the comment box and please follo me.