PDA

Click to See Complete Forum and Search --> : For...Next Statement


Dec 13th, 2000, 09:47 PM
Is there like a For...Next Statement in Borland C++?

Something like:

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.

kovan
Dec 14th, 2000, 07:18 AM
for(int intCount, intCount =<100, intCount++)
{
cout << intCount << endl;
}

Sam Finch
Dec 14th, 2000, 07:34 AM
Close


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

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.

kovan
Dec 14th, 2000, 07:45 AM
a crap :)
i havent coded c++ in a while :)
yep your right

Sam Finch
Dec 14th, 2000, 07:18 PM
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


for(intCount = 0; intCount <= 100; intCount++) {
cout << intCount << endl;
}

see what happens.

Dec 14th, 2000, 08:07 PM
Argh! :mad:

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) :rolleyes:.

Dec 15th, 2000, 06:54 AM
#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:


for(int i = 1; i <= 10; i++)
cout << i;


it goes from 1 to 10,
when you do this:


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]

Dec 15th, 2000, 01:47 PM
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: ?

#include <iostream.h>
int main()
{

for(int i = 1; i <= 10; i++)
cout << i & "\n\";

return 0;
}

Dec 15th, 2000, 01:57 PM
#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:


#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;
}

parksie
Dec 15th, 2000, 01:57 PM
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:

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:

cout << "i = " << i << endl;

HarryW
Dec 15th, 2000, 02:49 PM
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.


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

Dec 15th, 2000, 03:48 PM
[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:

#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;

}