Reverse Loop?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to write to the screen all numbers, in increments of .5, from 80
to 50. The results need to be in descending order like so:

80
79.5
79
78.5
78
77.5
77
76.5
76
75.5
....
....
505
50

I can't seem to get the for (int i = 80; i <= 50; i++) loop working out
properly. Any advice would be great. Thank you.
 
Thanks a lot, everyone. Not a homework assignment. I've been doing a lot
in vb.net and now trying learn more about how c# works. Looping seems so
far to be the more complicating piece to adjust to.
 
Scott,

Nice solution, however why then not

for (int i = 160;i>100;i--)
{
console.writeline(i/2);
}

However the idea was yours

Cor
 
Thanks a lot, everyone. Not a homework assignment. I've been doing a lot
in vb.net and now trying learn more about how c# works. Looping seems so
far to be the more complicating piece to adjust to.

No it is almost the same as in VB, only you have to write something more and
you soon will start to think that you only can do a step 1 (which is default
in VB).

However there is something more in C# then in VB, you can by using the ++i
even start later to increment, although this is in fact something cosmetic,
because AFAIK with setting the starting and ending point one lower has the
same effect.

Cor
 
----- Original Message -----
From: "Cor Ligthert[MVP]" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Sunday, May 18, 2008 12:05 AM
Subject: Re: Reverse Loop?

Scott,

Nice solution, however why then not

for (int i = 160;i>100;i--)
{
console.writeline(i/2);
}

However the idea was yours

And it won't work either.....integer math

I am sure you knew that, Cor....So you may proceed to kick yourself now.
<grin>

console.writeline(i/2.0); // That's better

Bill
 
Before you misunderstand me, you have to write in C# a little bit more (and
type more as things as Next are set automaticly in VB),

This C# code
\\\
for (int i = -160; i <= -100; i++)
{
Console.WriteLine((Math.Abs((double)i / 2)).ToString());
}
Console.ReadLine();
///
Would be in VB 2008 as you probably know
\\\
For i = -160 To -100
Console.WriteLine(Math.Abs(CDbl(i) / 2))
Next
Console.ReadLine
///

But in fact it is the same and does it as well give the same technical (CLI)
and visible results

And you have now again another alternative

:-)

Cor
 
Bill,

I saw it, see my next message, what was written before I saw your reply

However I smile in a good mood that somebody saw it.

:-)

Cor
 
Love the hints, espectially this one *******. Thats really made my day......

;-D



[...]
I can't seem to get the for (int i = 80; i <= 50; i++) loop working out
properly. Any advice would be great. Thank you.

Sounds like a homework problem.

***** Here's a hint: if you want your loop to go backwards, adding to
******
(incrementing) the counter isn't going to work.

Here's another hint: if you want fractional values, using integers isn't
going to work.

One final hint: in a "for()" statement, the three parts have very broad
rules for usage. The first part is executed once, before the loop begins,
and may contain variable declarations; the second part must simply be some
sort of logical expression that evaluates to a boolean...it can be _any_
expression that meets that requirement; and finally, the third part can be
any statement or statements (separated by commas)...there's nothing that
says it can only be a variable combined with a post-increment operator.

With that information, I think you should be able to rewrite your loop so
that it meets your need.

Pete
 
Because the OP wants numbers 80 to 50 not 160 to 100 and he doesn't want all
decimals inbetween (.9,.8,.7, etc.) only .5 so i-- is out.

-Scott
 
Scott said:
Because the OP wants numbers 80 to 50 not 160 to 100 and he doesn't want all
decimals inbetween (.9,.8,.7, etc.) only .5 so i-- is out.

Hmm.

If we use /2.0 and not /2 as discussed in several posts, then:

what is 160/2.0 and 100/2.0 ?

Arne
 
To me, it seems more straight-forward to deal with 800 and 500, than to deal
with 160 and 100 when the desired result is 80 to 50, that's all.

-Scott
 
Scott said:
To me, it seems more straight-forward to deal with 800 and 500, than to deal
with 160 and 100 when the desired result is 80 to 50, that's all.

Sure.

But if that is what you mean then write that.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top