I'm not finding much about doing a reversed for loop on a slice. Something with this kind of code:
const string: []const u8 = "1234";
for reversed (string) |character| {
std.debug.print("{}", .{character});
}And this output:
4 3 2 1
Is there a language feature I'm not aware of, or a function in the std, or should I just use a plain old dirty while loop?
How to Reverse a for loop? [closed] - Stack Overflow
bash - How do I reverse a for loop? - Unix & Linux Stack Exchange
How to reverse the order in a FOR loop
c - What's the best way to do a reverse 'for' loop with an unsigned index? - Stack Overflow
Videos
range() and xrange() take a third parameter that specifies a step. So you can do the following.
range(10, 0, -1)
Which gives
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
But for iteration, you should really be using xrange instead. So,
xrange(10, 0, -1)
Note for Python 3 users: There are no separate
rangeandxrangefunctions in Python 3, there is justrange, which follows the design of Python 2'sxrange.
for x in reversed(whatever):
do_something()
This works on basically everything that has a defined order, including xrange objects and lists.
You can use it like this
for (var i = 10; i >= 0; i--)
But I'm not sure why are you doing so because you are not using this variable inside your loop so I think it doesn't make any difference to the output as you loop going to run 10 times either way.
If I could guess then I think you want to print the stars in reverse order of quantity. That means you want to have maximum stars in first row then -1 in second and so on.. If so then you need to reverse the order of inner loop only like this
function seethestars1() {
var stars = document.getElementById("emptytext2");
for (var i = 0; i <= 10; i++) {
for (var j = 10; j > i; j--) {
stars.value += "*";
}
stars.value += "\n";
}
}
Js Fiddle Demo
Just try the following code for seethestarts1 method.
function seethestars1() {
for (var i = 0; i <= 10; i++) {
for (var j = i; j > 0; j--) {
document.getElementById("emptytext2").value += "*";
}
document.getElementById("emptytext2").value += "\n";
}
}
In bash or ksh, put the file names in an array, and iterate over that array in reverse order.
files=(/var/logs/foo*.log)
for ((i=${#files[@]}-1; i>=0; i--)); do
bar "${files[$i]}"
done
The code above also works in zsh if the ksh_arrays option is set (it is in ksh emulation mode). There's a simpler method in zsh, which is to reverse the order of the matches through a glob qualifier:
for f in /var/logs/foo*.log(On); do bar $f; done
POSIX doesn't include arrays, so if you want to be portable, your only option to directly store an array of strings is the positional parameters.
set -- /var/logs/foo*.log
i=$#
while [ $i -gt 0 ]; do
eval "f=\${$i}"
bar "
((i-1))
done
Try this, unless you consider line breaks as "funky characters":
ls /var/logs/foo*.log | tac | while read f; do
bar "$f"
done
var num = 10,
reverse = false;
if(!reverse) for( var i=0;i<num;i++) log(i);
else while(num-- ) log(num);
// to avoid duplication if the code gets long
function log( num ) { console.log( num ); }
EDIT:
As noted in the comments below, if i is not declared elsewhere and you do not intend for it to be global, then declare it with the other variables you declared.
And if you don't want to modify the value of num, then assign it to i first.
var num = 10,
reverse = false,
i;
if(!reverse) for(var i=0;i<num;i++) log(i); // Count up
else {var i=num; while(i--) log(i);} // Count down
function log( num ) { console.log( num ); }
Try use 2 loops:
if (reverse) {
for(i=num-1;i>=0;i--){
console.log(i)
}
}
else {
for(i=0;i<num;i++){
console.log(i)
}
}
How about:
for (unsigned i = n ; i-- > 0 ; )
{
// do stuff with i
}
for ( unsigned int loopIndex = n; loopIndex > 0; --loopIndex ) {
unsigned int i = loopIndex - 1;
...
}
or
for ( unsigned int loopIndex = 0; loopIndex < n; ++loopIndex ) {
unsigned int i = n - loopIndex - 1;
...
}