Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Loops; The Loops of C and C++
Topic Started: Jul 26 2008, 04:59 AM (409 Views)
Rexhunter99
Member Avatar
Elite Member
[ *  *  *  * ]
Okay, Im still new to the whole C programming thing, and I know how hard it is to program in.

My first tutorial is on Loops:

WHILE example code:
Code:
 

#include <iostream>

//Define the variable i as an integer
int i = 0;

int main()
{
//Loop until i is equal to 100
while (i<100)
{

//Print a line of text in the windows console, \n means newline.
printf("A line in the loop.\n");

//Add 1 to the integer i
i = i+1;
}

//Make the console wait until Enter is pressed
system("pause");

return (0);
}

Its commented well, so theres no reason you shouldn't understand it.

A while loop can be used for many things and is very handy, here the code is simply repeating 100 times (0-99) and printing some text into the windows console.

-----------------------------------------------------------------------

FOR example code:
Code:
 

#include <iostream>

int main()
{
//Loop until i is equal to 100
for (int i=0; i<100; i++)
{

//Print a line of text in the windows console, \n means newline. %d prints the variable i.
printf("A line in the loop, %d.\n",&i);

}

//Make the console wait until Enter is pressed
system("pause");

return (0);
}

Its very much like a while loop only when you call it, you can define what the variable is equal to at the start of the loop every time.

A for loop can be used for as many things as a while and is good for grid-like systems, here the code is simply repeating 100 times (0-99) and printing some text into the windows console again.

-----------------------------------------------------------------------

DO loop coming soon.
Edited by Rexhunter99, Jul 26 2008, 05:23 AM.
Posted Image
AI = 0%
Environment = 50%
Renderer = 15%
Multiplayer = 0%
Menus = 0%
Models = 2%
Maps = 5%
Total = 11%
Offline Profile Quote Post Goto Top
 
Bazza
Member Avatar
Forum God
[ *  *  *  *  *  * ]
there is also the do loop for things u want to run at least once

Code:
 

//do loop//

#include <iosteam>

using namespace std;

int number = 0; //define our variables//

int main();
{
do{ //what u want it to do while condition is met//
while{//conditon}}



Edited by Bazza, Jul 26 2008, 06:18 AM.
My instinct is to hide in this barrel, like the wily fish.
Offline Profile Quote Post Goto Top
 
Rexhunter99
Member Avatar
Elite Member
[ *  *  *  * ]
Yes, problem is I have never used a DO loop, even in gamemaker. I need to work on DO loops a little more, then Ill add this to my C++ for Beginners tutorial.

What did you think of the explanation/examples so far?
Posted Image
AI = 0%
Environment = 50%
Renderer = 15%
Multiplayer = 0%
Menus = 0%
Models = 2%
Maps = 5%
Total = 11%
Offline Profile Quote Post Goto Top
 
Bazza
Member Avatar
Forum God
[ *  *  *  *  *  * ]
informative and to the point **applause** :clapping:
i just thought ide throw in teh do loop
My instinct is to hide in this barrel, like the wily fish.
Offline Profile Quote Post Goto Top
 
Rexhunter99
Member Avatar
Elite Member
[ *  *  *  * ]
Yep, ty.
Posted Image
AI = 0%
Environment = 50%
Renderer = 15%
Multiplayer = 0%
Menus = 0%
Models = 2%
Maps = 5%
Total = 11%
Offline Profile Quote Post Goto Top
 
« Previous Topic · Tutorials · Next Topic »
Add Reply