Results 1 to 12 of 12

Thread: For...Next Statement

  1. #1
    Guest

    Question

    Is there like a For...Next Statement in Borland C++?

    Something like:

    Code:
    void main()
    {
    	int a;
    	cout<<"1-100:\n\n\";
            'For a = 0 to 100
    	cout<< a;
            'Next a
            cin>> a;
    }
    So I can print a list of numbers down the screen?
    (1\n\2\n\3\n\4\n\5\n\6\n\7\n\8\9\10\n\.....)

    Hope you get what I mean.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    Code:
    for(int intCount, intCount =<100, intCount++)
    {
      cout << intCount << endl;
    }

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Close

    Code:
    for(int intCount = 0; intCount <= 100; intCount++)
    {
        cout << intCount << endl;
    }
    you have to put semicolons between the parts of the for statement.

    this translates into something like this
    Code:
    int intCount  = 0;     //First Statement
    
    StartLoop:  //a label
    
    if !(intCount <= 100) //second Statement
    {
        goto EndLoop;    //I don't think there's a goto in C++ 
    }                    //but I'm just saying what th for loop does
    
    
    cout << intCount << endl; //the code inside the brackets
    
    
    intCount++;  //third statement
    
    goto StartLoop;
    
    EndLoop:
    
    //code after for loop
    the statements can be anything you like, so the for statement can be a do while ,a do until loop or whatever, the statements have to be separated by semicolons not commas tho.
    If it wasn't for this sentence I wouldn't have a signature at all.

  4. #4

  5. #5
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Megatron right on both counts
    (just out of interest if you can do gotos how do you do labels, cos mine clearly don't work.)


    Matthew Sorry about the confusion, I find being able to think of for loops "written out" really helpful, so I thought I'd put it in.

    Did you declare intCount before your for loop? if you did then you've declared it twice, try getting rid of the int in the for statement

    Code:
    for(intCount = 0; intCount <= 100; intCount++) {
        cout << intCount << endl;
    }
    see what happens.
    If it wasn't for this sentence I wouldn't have a signature at all.

  6. #6
    Guest
    Argh!

    It still don't work. Same errors.
    Is it me? Or is it Turbo C++ 3.0?

    Maybe I need to upgrade to a higher C++ (possibly VC++ or Borland C++ 4.x) .

  7. #7
    Guest
    Code:
    #include <iostream.h>
    int main()
    {
    
    for(int i = 1; i <= 10; i++)
    cout << i;
    
    return 0;
    }
    that exact code works perfect for me..

    and the same as everything else(while loop, if statement, else statement, etc) you only need the curly braces if you need more than one statement executed.


    by the way.....

    when you do this:

    Code:
    for(int i = 1; i <= 10; i++)
    cout << i;
    it goes from 1 to 10,
    when you do this:

    Code:
    for(int i = 0; i < 10; i++)
    cout << i;
    it goes from 0 to 9...

    I am sure you knew this already, but just pointing that out..

    [Edited by denniswrenn on 12-15-2000 at 07:57 AM]

  8. #8
    Guest

    Thumbs up It works!

    Thanks Dennis and everyone else that helped clear things up for me.

    Just one more thing, I want the numbers to show up like:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    .
    .
    .
    .
    ...100

    In C++, what is the & operator?

    Something like: ?

    Code:
    #include <iostream.h>
    int main()
    {
    
    for(int i = 1; i <= 10; i++)
    cout << i & "\n\";
    
    return 0;
    }

  9. #9
    Guest
    Code:
    #include <iostream.h>
    int main()
    {
    
    for(int i = 1; i <= 10; i++)
    cout << i << "\n";
    
    return 0;
    }
    there is no \ after \n, if you left it like that, the compiler would think you were escaping the quote, and it would give you an error about a missing end quote, and probably missing semicolon too.

    and the & is used like the VB addressof operator, but it can be used on variables too...

    if you want 2 things to be output from 1 cout statement, use <<

    the logical and operator is &&,
    ex:

    Code:
    #include <iostream.h>
    
    int main()
    {
    int a = 1, b = 2, c = 3;
    if ((a == 1) && (b == 2) && (c == 3))
    cout << "Ok";
    else cout << "not ok";
    return 0;
    }

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The & operator is similar to the AddressOf operator in VB, but more useful. It gets the address of anything - variable, function, etc.

    So, for example:
    Code:
    void func(char *str) {
        cout << str << endl;
    }
    void other() {
        char *mystr = "This is a string";
    
        func(&(mystr[1]));
    }
    Calling other displays "his is a string".

    To do what you want, just add another << item insertion:
    Code:
    cout << "i = " << i << endl;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well & is not only the AddressOf operator, it's also the bitwise And operator, and in C++ it's also the ReferenceTo operator (not sure if it's called that). It depends on the context.

    Code:
    CClass *var = &otherVar; // address-of
    var&  //a reference
    var1 = var2 & var3; // bitwise and
    Harry.

    "From one thing, know ten thousand things."

  12. #12
    Guest
    [QUOTE]Originally posted by Sam Finch
    [B]Megatron right on both counts
    (just out of interest if you can do gotos how do you do labels, cos mine clearly don't work.)


    This should work:
    Code:
    #include <iostream.h>
    
    void main() {
    	int n;
    
    StartPgm:
    	cout << "Press '1' to exit or press 2 to display this message again" << endl;
    	cin >> n;
    	if( n == 2 )
    		goto StartPgm;
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width