• (2 Pages)
  • +
  • 1
  • 2

Understanding Loops in C++ A brief look at loops in C++ Rate Topic: ***-- 7 Votes

#1 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 185
  • Joined: 28-December 05

Posted 28 December 2005 - 01:31 PM

*
POPULAR

Loops are basically means to do a task multiple times, without actually coding all statements over and over again.

For example, loops can be used for displaying a string many times, for counting numbers and of course for displaying menus.

Loops in C++ are mainly of three types :-

1. 'while' loop
2. 'do while' loop
3. 'for' loop

For those who have studied C or JAVA, this isn't very new, as the syntax for the loops are exactly same. So if you know the above languages, don't waste your time understanding loop syntax in C++.


The 'while' loop :-
Let me show you a small example of a program which writes ABC three(3) times.

#include<iostream>

int main()
{ 
  int i=0;
  while(i<3)
  {
    i++;
    cout<<"ABC"<<endl;
  }
}



The output of the above code will be :-
ABC
ABC
ABC

A point to notice here is that, for making it more easy to understand, we could also write,

int i=1;
while(i<=3)



This would make our code more easy for a newbie, but in actuality it doesn't make a difference either way.


The 'do while' loop :-
It is very similar to the 'while' loop shown above. The only difference being, in a 'while' loop, the condition is checked beforehand, but in a 'do while' loop, the condition is checked after one execution of the loop.

Example code for the same problem using a 'do while' loop would be :-

#include <iostream>
int main()
{ 
  int i=0;
  do
  {
    i++;
    cout<<"ABC"<<endl;
  }while(i<3);
}



The output would once again be same as in the above example.


The 'for' loop :-
This is probably the most useful and the most used loop in C/C++. The syntax is slightly more complicated than that of 'while' or the 'do while' loop.

The general syntax can be defined as :-
for(<initial value>;<condition>;<increment>)

To further explain the above code, we will take an example. Suppose we had to print the numbers 1 to 5, using a 'for' loop. The code for this
would be :-

#include<iostream>

int main()
{ 
  for(inti=1;i<=5;i++)
  cout<<i<<endl;
}



The output for this code would be :-
1
2
3
4
5

Here variable 'i' is given an initial value of 1. The condition applied is till 'i' is less than or equal to 5. And for each iteration, the value of 'i' is also incremented by 1.

Notice here that if we wanted to print,
5
4
3
2
1

we would change our 'for' loop to :-
for(int i=5;i>=1;i--)




This is a very brief introduction to loops in C++. Hope it helped. A few reference books that I would suggest are :-

1. Object Oriented Programming using C++
By E Balagurusamy
Tata McGraw Hill

2. Using C++ by Rob McGregor
Prentice Hall of India

This post has been edited by JackOfAllTrades: 15 October 2010 - 05:46 PM
Reason for edit:: Brought example code into the 21st century.


Is This A Good Question/Topic? 10
  • +

Replies To: Understanding Loops in C++

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Posted 28 December 2005 - 08:16 PM

Excellent tutorial...the only comment I have is to point out that the iostream.h header library syntax has been deprecated, and is no longer accetable in ANSI standard code. It is preferred to use namespaces instead...this changes
#include<iostream.h>


to
#include<iostream>
using namespace std;


Great work!
Was This Post Helpful? 3
  • +
  • -

#3 Mrafcho001  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 41
  • View blog
  • Posts: 769
  • Joined: 01-November 05

Posted 29 December 2005 - 12:00 PM

Very nice tutorial rahulbatra!

I really like how you use a lot of examples and explain them well.

I know many people will disagree on this, but void main() is not a good practive/habit to get into.

instad:

int main()
{
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Posted 29 December 2005 - 01:37 PM

I would tend to agree...structuring the program to return an int upon exit ensures that it can work with other programs should the occasion require.
Was This Post Helpful? 0
  • +
  • -

#5 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 185
  • Joined: 28-December 05

Posted 29 December 2005 - 02:53 PM

Its completely true. I actually am in nasty habit of using 'void main()' since the first book that I read used that, and the Turbo C++ compiler I use doesn't give a warning on this.

However whenever possible use 'int main()'. Infact while talking to a kernel developer, he told me that the void is just an abstraction in this case. In all such cases, a '0' is returned. Though I can't verify the correctness of this as of now, it certainly seems possible.
Was This Post Helpful? 0
  • +
  • -

#6 shikha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 23-May 06

Posted 23 May 2006 - 03:04 AM

hi Mr Rahul...... nice try ....... 1 thing ....... this article would hav been gr8 if with more examples.....
Was This Post Helpful? 0
  • +
  • -

#7 virendra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-September 06

Posted 15 September 2006 - 05:56 AM

:rolleyes: rahul really it is in very easy language. and 100% understandable .could u plz explain the necessity of the each loop i.e. which loop is use when and under which condition
virendra
thanks for such nice tutoria;
VIRENDRA :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#8 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 185
  • Joined: 28-December 05

Posted 16 September 2006 - 01:14 AM

Actually most programmers prefer one type of loop or another. It becomes a choice of personal preference actually. But most of them prefer to use the 'for loop' because of its power and flexibility, having the condition and the increment in the same construct.

'For loops' also (in my opinion) look a lot cleaner when nested. The 'do-while' becomes too cumbersome when nesting though this may not always be the case.
Was This Post Helpful? 0
  • +
  • -

#9 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 16 September 2006 - 03:13 AM

View Postrahulbatra, on 30 Dec, 2005 - 03:23 AM, said:

Its completely true. I actually am in nasty habit of using 'void main()' since the first book that I read used that, and the Turbo C++ compiler I use doesn't give a warning on this.

However whenever possible use 'int main()'. Infact while talking to a kernel developer, he told me that the void is just an abstraction in this case. In all such cases, a '0' is returned. Though I can't verify the correctness of this as of now, it certainly seems possible.

It is true...I think I've even mentioned that on </dic> on some posts.
If you know Assembly Language, use any debugger loaded with any program, and study the value of the AX or EAX (depending on the whether your program uses 16-bit or 32-bit code) after main() is done executing.
Was This Post Helpful? 0
  • +
  • -

#10 browngod2002  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 27
  • Joined: 16-September 06

Posted 16 September 2006 - 03:41 PM

good topic. liked the examples. next time maybe cover nested loops.as a beginner that is wher I am having some problems.
Was This Post Helpful? 0
  • +
  • -

#11 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Posted 17 September 2006 - 06:45 AM

View Postrahulbatra, on 29 Dec, 2005 - 02:01 AM, said:

A few reference books that I would suggest are :-

1. Object Oriented Programming using C++
By E Balagurusamy
Tata McGraw Hill

Don't know about the second book but please anyone reading this thread should not follow this book. Yes, the language is easy but the author does lot of naive mistakes. Do good to yourself by not reading this book.

This post has been edited by Xing: 17 September 2006 - 06:59 AM

Was This Post Helpful? 0
  • +
  • -

#12 Therois  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 20-September 07

Posted 20 September 2007 - 02:37 AM

good tut i forgot alot of stuff over the summer this will refresh my mind
Was This Post Helpful? 1
  • +
  • -

#13 nirvanarupali  Icon User is offline

  • D.I.C Stomach
  • member icon

Reputation: 13
  • View blog
  • Posts: 1,119
  • Joined: 01-August 07

Posted 25 September 2007 - 08:28 PM

View PostAmadeus, on 29 Dec, 2005 - 01:37 PM, said:

I would tend to agree...structuring the program to return an int upon exit ensures that it can work with other programs should the occasion require.



That's right. It is never used and will never use in C++ the
void main()
even your compiler compiles it.

For more information visit :

http://www.research.....html#void-main
Was This Post Helpful? 0
  • +
  • -

#14 Lt_Kernel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 05-September 07

Posted 28 October 2007 - 02:18 PM

your "for" loop example gives a not so good errors

1. inti undeclared first use this function
2.each undeclared identifiers isreported only once for each functions it appears in
3. i undeclared first use this function
4. build error error 1
5.in fuction int amin ()

and you gave only numbers as an example
there is also a thing called "letters" there is also a "still images, videos and more others,

can u give examples of letters or words using for loop

:v:

ooops for got to check the date
sorry for the not so late reply

This post has been edited by Lt_Kernel: 28 October 2007 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#15 XxXnucronXxX  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-February 08

Posted 13 February 2008 - 07:42 AM

thank you for that mr.rahulbatra.. very brief explanation on the loops! :^:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2