hey people there are lots of things that you need to learn when you start your programming journey and looping concept is the most basic and important concept that you should learn and practice a lot to get mastery in looping .
if you already familiar with looping concept then try to solve question at the last . link is in the end for question.
looping
looping is developed to make the work of programmers easier suppose you have to do same work for number of time's or write the same line like print command for 1000 time so for you it is time consuming to write the same line again and again it so to solve this problem developer developed a technique called looping .
before we move further let discuss the basic of loops.
type of loops in programming
1.indefinite loop:-the loop which can run indefinite time example : while, do while.
2.definite loop : the loop which which can run for a limited no . of times. example : for
for better understanding see the below figure
lets discuss with the indefinite loop first
while loop
while loop is one of the most common yet most useful looping construct .it contains three steps
- initialization
- condition check
- increment or decrement
flow chart for while loop
let take a example to understand it better
int i=1; //intialization
while(i<10) //condition check
{
cout<<i<<endl;
i++; // increament or decreament
}
points to remember while using while loop
- intialization must be proper other wise it will give runtime or compiler time error
- condition check should be logical otherwise loop will run infinite time
- increament and decrement should be properly handle otherwise it will run infinite time
do while loop
do while loop is similar to while loop but the main difference between while and do while loop is condition check in while loop you check the condition before entering to the loop or before the increment and decrement . in case of do while checking condition is done after the execution of code block and atleast one iteration is complete or execution of code is done atleast one before testing the condition .
flow chart for do while loop
example for do while
int i=1;
do
{
cout<<i<<endl;
i++;
}while(i<10);
for loop
for loop is one of the most widely used and simple looping construct as all the three condition are at the same place it consist all the three component of while loop but in a single line.
flowchart for for loop
example
for(int i=1;i<10;i++)
{
cout<<i<<endl;
}
if you observe carefully all three condition is similar to while loop but they are all in one line.
Quick summary
all the concept in loops are same in all languages especially in java and c++ they have common syntax .
Problem with indefinite loop (while,do-while)
after go through all the loops lets discuss some of the point that we keep in mind when we use while or do while loop
- it can execute indefinite no. of time
this is the one of the most common problem with while and do-while loop if you does not handle the test condition carefully then it will run indefinite times.
I am attaching some question link for you guys to get practice and familiar with loops