Loops In C++

Loops In C++

simple and crisp

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

basics of loop.png

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

  1. initialization
  2. condition check
  3. increment or decrement

flow chart for while loop

cpp-while-loop-230x300.jpg

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

  1. intialization must be proper other wise it will give runtime or compiler time error
  2. condition check should be logical otherwise loop will run infinite time
  3. 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

do-while-loop-example-c-3.png

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

loop-control.jpg

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 .

java-loops.png

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

  1. 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

(Link)codesdope.com/practice/cpp-loops

(Link)hackerrank.com/challenges/c-tutorial-for-lo..