Why and how Loops used in programming:-
In many programming language there is a time when you want to some thing in many time like to print your name, it is very difficult to write so many times, it takes a lot of your precious time, also takes so much memory size, for thats loops are introduce to make programming languages user friendly.
To day i will teach you loops used in c++ programming, i have selected this because c programming is the mother of all languages.
In every programming loops are introduced to make it easier for the user, but there syntax which means their way to write code may be different for different languages.
There are basics three types of loops:
For loop:-
First of all is for loop, in programming when you know about the first number and last number means if you know form where the loop should start and where to end then you should for loop, there is also a thing called increment means after how long it will give you next number or after what number next number is generated.
This program prints hello world four times.
After main program there is a statement 'for' this is the for loop, as we know from where loop should start and where to end.
I have started this loop from 1 to less then 5, as x variable continuously increases its value from 1 till less then 5, x++ means it increase the value of previous x, like x=x+1 it renew it value by adding 1, it checks value after every time it runs, like when x=1 the it moves to nest condition and checks whether it is less then 5 or not if less then 5 it moves to next condition and increases the value of x and after loop statement it runs the program, notice that value of x is now 2 after first run, it continously run till less then five.
As there is cout statement which print hello world in every time when it runs.
If you don't know how to print then check my post
how to print in c program
While Loop
If you don't know about how many times loop must run, in for loop you have to give instruction about initial condition then final condition, now here you should know only final value where loop should end.
here is a program, look at the program and try to understand by your self, don't worry i will guide you when we move on.
after main, we declared variables and their variable, counter is the first condition for the loop to start and howmany is the variable which is used define means we want user to input any value means final value till loop ends.
In next line cin is the command used to get data from user if you don't about how to get input from user then check out this post
how to get input from user , now next if while loop, in while loop there is a single condition counter<howmany which means as counter is 1 we already declared while run the loop for the first time as in howmany variable if user enters 10 then counter is greater then 10 which is already stored in howmany, then there is no problem in running this program for first time, in next line there is print statement then a counter++ which increments the counter for making loop to run till user defined variable.
Do while loop:-
Now coming to next loop is do while as named indicates do some thing then while loop. So this loop is for when you want a any portion of program to run before the loop statement then you should try this.
Here is a simple program
In many programming language there is a time when you want to some thing in many time like to print your name, it is very difficult to write so many times, it takes a lot of your precious time, also takes so much memory size, for thats loops are introduce to make programming languages user friendly.
To day i will teach you loops used in c++ programming, i have selected this because c programming is the mother of all languages.
In every programming loops are introduced to make it easier for the user, but there syntax which means their way to write code may be different for different languages.
There are basics three types of loops:
- For loop
- While loop
- Do while loop
For loop:-
First of all is for loop, in programming when you know about the first number and last number means if you know form where the loop should start and where to end then you should for loop, there is also a thing called increment means after how long it will give you next number or after what number next number is generated.
This program prints hello world four times.
After main program there is a statement 'for' this is the for loop, as we know from where loop should start and where to end.
I have started this loop from 1 to less then 5, as x variable continuously increases its value from 1 till less then 5, x++ means it increase the value of previous x, like x=x+1 it renew it value by adding 1, it checks value after every time it runs, like when x=1 the it moves to nest condition and checks whether it is less then 5 or not if less then 5 it moves to next condition and increases the value of x and after loop statement it runs the program, notice that value of x is now 2 after first run, it continously run till less then five.
As there is cout statement which print hello world in every time when it runs.
If you don't know how to print then check my post
how to print in c program
While Loop
If you don't know about how many times loop must run, in for loop you have to give instruction about initial condition then final condition, now here you should know only final value where loop should end.
here is a program, look at the program and try to understand by your self, don't worry i will guide you when we move on.
after main, we declared variables and their variable, counter is the first condition for the loop to start and howmany is the variable which is used define means we want user to input any value means final value till loop ends.
In next line cin is the command used to get data from user if you don't about how to get input from user then check out this post
how to get input from user , now next if while loop, in while loop there is a single condition counter<howmany which means as counter is 1 we already declared while run the loop for the first time as in howmany variable if user enters 10 then counter is greater then 10 which is already stored in howmany, then there is no problem in running this program for first time, in next line there is print statement then a counter++ which increments the counter for making loop to run till user defined variable.
Do while loop:-
Now coming to next loop is do while as named indicates do some thing then while loop. So this loop is for when you want a any portion of program to run before the loop statement then you should try this.
Here is a simple program
Only we added a do statement for portion of program to run first then while loop.
This is not a complete program, you should try thing on by own, as this is very simple program, while statement is same as previous in while loop.
So theses are the main loops which are commonly used in many programming languages.
Hope you understand these program and the difference between all loop.
If you have any query about loop please let me know through your comments, i will try to reply you as soon as possible.
No comments:
Post a Comment