Click to See Complete Forum and Search --> : For Loops?


rascal22
11-24-2003, 10:12 PM
How would I write a program outputs the following using a for loop:
________x
_______x
______x
_____x
____x
___x
__x
_x
x

I can't figure out how I'd use the loop to create the number of spaces. Spaces are a char and the number of them an int. I know it involves a --, and a 9 and a 0, but I'm not sure where to go from there...can anyone please help? It's probably something really simple that I'm just not thinking of (lol). Thanks ;)

drsmartman
11-24-2003, 10:31 PM
The syntax may vary depending on what you are writing the loop in, but it should look something like this...(in PHP). There are many ways to write this in many languages...

#10 underscore characters
$line='__________';

for($i=10;$i>0;$i--){

#print underscores based on value of $i
#use substr() function in PHP or write another loop
echo substr($line,0,$i).'<Xbr />';
}

rascal22
11-24-2003, 10:43 PM
Ok, I'm kind of confused. I haven't learned anything about $ signs yet... i actually used the _ to represent space because my computer wasn't showing them when I posted... so would that be
char space= ' ';
int i;

for(i=9;i>=0;i--)
{
System.out.println(

now I'm not sure how I combine spaces and the number of them because they are two different data types...this is where I am getting confused... lol. Thank you.

Gollum
11-25-2003, 02:29 AM
Looks like you're trying to use Java.

In javascript you could do...

<script type="text/javascript">
var i,j;
for ( i = 8; i >= 0; i-- )
{
for ( j = 0; j < i; j++ ) document.write('&amp;nbsp;');
document.write('X<br>');
}
</script>

rascal22
11-25-2003, 07:44 PM
Thanks so much!