Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,162 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,370 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Understanding Loops in C++

2 Pages V  1 2 >  
Reply to this topicStart new topic

> Understanding Loops in C++, A brief look at loops in C++

Rating  3
rahulbatra
Group Icon



post 28 Dec, 2005 - 01:31 PM
Post #1


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.

CODE

#include<iostream.h>

void 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,

CODE

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 :-

CODE

void 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 :-

CODE

#include<iostream.h>

void 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 :-
CODE

for(inti=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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Amadeus
Group Icon



post 28 Dec, 2005 - 08:16 PM
Post #2
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
CODE

#include<iostream.h>

to
CODE

#include<iostream>
using namespace std;

Great work!
Go to the top of the page
+Quote Post

Mrafcho001
Group Icon



post 29 Dec, 2005 - 12:00 PM
Post #3
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:

CODE

int main()
{
   return 0;
}
Go to the top of the page
+Quote Post

Amadeus
Group Icon



post 29 Dec, 2005 - 01:37 PM
Post #4
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.
Go to the top of the page
+Quote Post

rahulbatra
Group Icon



post 29 Dec, 2005 - 02:53 PM
Post #5
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.
Go to the top of the page
+Quote Post

shikha
*



post 23 May, 2006 - 03:04 AM
Post #6
hi Mr Rahul...... nice try ....... 1 thing ....... this article would hav been gr8 if with more examples.....
Go to the top of the page
+Quote Post

virendra
*



post 15 Sep, 2006 - 05:56 AM
Post #7
rolleyes.gif 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.gif
Go to the top of the page
+Quote Post

rahulbatra
Group Icon



post 16 Sep, 2006 - 01:14 AM
Post #8
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.
Go to the top of the page
+Quote Post

born2c0de
Group Icon



post 16 Sep, 2006 - 03:13 AM
Post #9
QUOTE(rahulbatra @ 30 Dec, 2005 - 03:23 AM) *

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.
Go to the top of the page
+Quote Post

browngod2002
Group Icon



post 16 Sep, 2006 - 03:41 PM
Post #10
good topic. liked the examples. next time maybe cover nested loops.as a beginner that is wher I am having some problems.
Go to the top of the page
+Quote Post

Xing
Group Icon



post 17 Sep, 2006 - 06:45 AM
Post #11
QUOTE(rahulbatra @ 29 Dec, 2005 - 02:01 AM) *
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 Sep, 2006 - 06:59 AM
Go to the top of the page
+Quote Post

Therois
*



post 20 Sep, 2007 - 02:37 AM
Post #12
good tut i forgot alot of stuff over the summer this will refresh my mind
Go to the top of the page
+Quote Post


2 Pages V  1 2 >
Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/27/08 09:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month