Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

1 - Loops

Just practicing code, not really making a whole project out of it

1 - Loops

Postby antiRTFM on Wed Apr 30, 2008 2:12 pm

Make a loop that prints "Hello world" 24 times - in 10 different ways! make sure you have at least one for loop, one while loop, and one do {} while loop.
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Postby Doctor Salt on Wed Jul 30, 2008 10:23 pm

I don't have access to a comp. with a compiler at the moment, but i could probably think up a few ways to do that.

1) using the

Code: Select all
for (x=1;x<25;x++)
{ cout << "helloworld";}
method.

3) doing
Code: Select all
while (x++<25)
{ cout << "helloworld";}
method

4) making the main function just have one cout << "hello world"; section in it, and also include a x=x++ somewhere in it. Make it send you to another function, say "hello world" there, then have it send you back.

using if/else branching, you could add in a

Code: Select all
if (x>12)
{do something involving ending the program}


Code: Select all
5) the obvious "write 'hello kitty' 24 times" method


Code: Select all
6) you could make 24 strings that all mean "hello kitty" then just print them all to the screen.


thats all i could think up at the moment.... getting tired :P
Code: Select all
Doctor Salt
 
Posts: 147
Joined: Wed Jul 30, 2008 3:32 pm

Re: 1 - Loops

Postby Goose on Mon Mar 30, 2009 7:16 pm

I had an idea to make a loop that would print out "Hello world!" 24 times, and each time it will add another exclamation mark at the end.

[code]
#include <iostream>

int main()
{
char x = '!';
for ( int t = 0; t < 25; t++ )
{
std::cout << "Hello world" << x * t << std::endl;
}
}
[/code]

But when it runs instead of seeing exclamation marks after "Hello world" I see the result of t multiplied by 33. Is 33 the ASCII value of '!' ?
User avatar
Goose
 
Posts: 9
Joined: Sat Feb 14, 2009 10:44 pm

Re: 1 - Loops

Postby antiRTFM on Mon Mar 30, 2009 8:10 pm

the * operator is mathematical, not something that will execute code a given amount of times (like print ! some number of times etc)

to make something happen multiple times, you need...... a loop! so you prolly want to do this;

Code: Select all
for ( int t = 0; t <= 24; t++ )
{
     std::cout << "Hello world";
     for(int x = 0; x <= t; x++) cout << "!";
     cout << endl;
}
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Re: 1 - Loops

Postby Goose on Tue Mar 31, 2009 12:59 pm

Of course! Why didn't I think of just adding another loop. :P
User avatar
Goose
 
Posts: 9
Joined: Sat Feb 14, 2009 10:44 pm

Re: 1 - Loops

Postby Dathknight on Wed Apr 01, 2009 7:28 am

What about the do - while - loop?

[code]#include <iostream>

void main()
{
int i = 1;
do
{
printf("Hello World\n");
i++;
}while( i < 25);
system("PAUSE");
}[/code]
Dathknight
 
Posts: 0
Joined: Tue Mar 31, 2009 2:58 am

Re: 1 - Loops

Postby antiRTFM on Wed Apr 01, 2009 7:08 pm

hm why is your bbcode off?

try to uncheck the "disable bbcode" box at the bottom of your posts

as per your question, going back on this threads topic (!), you can do any loopy stuff in any of for/while/do-while loops. its just for convenience that the language has these different loops for common situations which suit a for() better or a do-while() better but they can all do the exact same stuff. which is precisely what this practice snippet's purpose was about; for you to code same stuff using different loops. like

Code: Select all
for (int a = 1; a <= 24; a++) cout << "hello world";
Code: Select all
int x =1;
while (x <= 24)
{
    cout << "hello world";
    x++;
}
Code: Select all
int x =1;
do {
    cout << "hello world";
    x++;
} while (x <= 24);

do the same exact thing in 3 different ways. sorry Doctor Salt, i said "using loops" specifically so no functions or string tricks...

here's the loop snippets i wanted someone to come up with. the above 3 loops, plus the following seven (or any variation of them cuz there arent only 10);

Code: Select all
for (int x = 1; x <= 24; x++, cout << "hello world") ;

Code: Select all
int x =1;
for (;;)
{
    cout << "hello world";
    x++;
    if (x > 24) break;
}

Code: Select all
int x=0;
for (; x++ <= 24;) cout << "hello world";

Code: Select all
int x=1;
while (x++ <= 24) cout << "hello world";

Code: Select all
int x =1;
while (true)
{
    cout << "hello world";
    x++;
    if (x > 24) break;
}

Code: Select all
int x =24;
while (x--) cout << "hello world";

Code: Select all
int x=0;
do {
cout << "hello world";
} while (++x <= 24);


and many many more variations.

go ahead and come up with more variations (or find mistakes in mine :) )
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Re: 1 - Loops

Postby OtTa on Mon Jun 15, 2009 4:10 pm

Here is one more variant:

Code: Select all
int main() {short i=0; begin: if (i++ < 24) {cout << "Hello world\n"; goto begin;}}


Kind of manual loop :D
OtTa
 
Posts: 2
Joined: Mon Jun 15, 2009 2:07 pm

Re: 1 - Loops

Postby antiRTFM on Mon Jun 15, 2009 4:56 pm

Yea

not to mention "goto" is strongly avoided
User avatar
antiRTFM
Administrator
 
Posts: 462
Joined: Sun Apr 13, 2008 9:10 am

Re: 1 - Loops

Postby GreySkull++ on Thu Aug 20, 2009 6:53 pm

Code: Select all
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
wtf:
   cout << "Hello world! \n";
   cout << "Hello world!! \n";
   cout << "Hello world!!! \n";
   cout << "Hello world!!!! \n";
   cout << "Hello world!!!!! \n";
   cout << "Hello world!!!!!! \n";
   cout << "Hello world!!!!!!! \n";
   cout << "Hello world!!!!!!!! \n";
   cout << "Hello world!!!!!!!!! \n";
   cout << "Hello world!!!!!!!!!! \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   cout << "Hello world \n";
   goto wtf;
}
GreySkull++
 
Posts: 1
Joined: Thu Aug 20, 2009 6:11 pm

Re: 1 - Loops

Postby sawm on Sat Aug 29, 2009 2:55 pm

here this isint what you asked but i was bored and made it anyways you chose how many times to loop and what word to loop im not sure how to add stuf tho id love to understand that if there a short explanation to adding lets say a "!" at the end every time it loops hmmm...

p.s i seem to have my bbcode off sometimes helpppp xD

Code: Select all
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string whatToWrite;
    int howManyTimes;
    cout << "ok lets spam a word! :) so type how many time we should spam word" << endl;
        cin>>howManyTimes;
            cout<<"ok now what word you wana spam?: ";
                cin>>whatToWrite;
    for (int limit = 0; limit < howManyTimes  ; ++limit )
    {

     cout<< whatToWrite ;

    }

    return 0;
}
sawm
 
Posts: 8
Joined: Fri Aug 28, 2009 3:24 pm

Re: 1 - Loops

Postby damsol on Sun Sep 13, 2009 5:44 am

Not exactly what you asked for, but I thought I would give switches a try for some fun and practice along with the loops

Code: Select all
int main()
{
    srand((unsigned)time(0));
    int rand_num; /* Used for random output switch */
    int n = 1; /* Number of times executed */
    while (n <= 24)
    {
        for( int index = 1; index < 25; index++ )
        {
        rand_num = (rand()%10)+1;
        }
        switch (rand_num) /* Make the output random */
        {

            case 1 :
            cout << "Hello World!\n";
            break;

            case 2 :
            cout << "Hello World!!\n";
            break;

            case 3 :
            cout << "Hello World!!!\n";
            break;

            case 4 :
            cout << "Hello World!?!\n";
            break;

            case 5 :
            cout << "Hello0o World!\n";
            break;

            case 6 :
            cout << "H3ll0w W0rld!\n";
            break;

            case 7 :
            cout << "Hello World!?!?!\n";
            break;

            case 8 :
            cout << "Hello World!@!!\n";
            break;

            case 9 :
            cout << "Hello World!$!$\n";
            break;

            case 10:
            cout << "Hello World?\n";
            break;

            default:
            cout << "Something has gone wrong./nPlease double check the source code and try again.\n";
            break;
        }
        n++;
    }
    cin.get();
    return 0;

}


**NOTE: BBCode isn't working for me, and it's enabled...
damsol
 
Posts: 3
Joined: Sun Sep 13, 2009 2:20 am

Re: 1 - Loops

Postby V!rus on Wed Nov 18, 2009 8:28 pm

I seriously recommend to you to use for and not a while. It kinda better for that kind of application.
V!rus
 
Posts: 0
Joined: Wed Nov 18, 2009 7:57 pm

Re: 1 - Loops

Postby noobgrammer on Thu Nov 19, 2009 5:57 pm

This is a practice loop thread to practice different ways to use loops with no real point. So they may seem a poor way of using them but that is how you learn to use them right
User avatar
noobgrammer
 
Posts: 176
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando


Return to Snippets

Who is online

Users browsing this forum: No registered users and 0 guests