Operating System - HP-UX
1834796 Members
3219 Online
110070 Solutions
New Discussion

Re: for (int i = 200; i > 10; i++)

 
SOLVED
Go to solution
Donny Jekels
Respected Contributor

for (int i = 200; i > 10; i++)

how do I perform the same in ksh or shell scripts?
"Vision, is the art of seeing the invisible"
10 REPLIES 10
John Meissner
Esteemed Contributor
Solution

Re: for (int i = 200; i > 10; i++)

could you put a little more information please

i=200
if [ $i > 10 ]
then
((i=$i+1))
fi
All paths lead to destiny
Pete Randall
Outstanding Contributor

Re: for (int i = 200; i > 10; i++)

I take it that's C? I'm guessing it says that while i is more than 10, up until i = 200, do something, then increment i??


If so:

i=10
while (( i < 201 ))
do
print -n $i
(( i=i + 1 ))
done



Pete

Pete
John Poff
Honored Contributor

Re: for (int i = 200; i > 10; i++)

Hi,

I assume your construct means to start i with a value of 10, and increment it by one until it is greater than 200. Your syntax looks backwards, but if I understand what you are asking, here is one way to do it in some ksh script code:

let i=10
while (( $i < 200 ));
do
let i=i+1
echo $i
done

JP
Helen French
Honored Contributor

Re: for (int i = 200; i > 10; i++)

First of all, this will be an indefinite loop on the program since "i" will always be > 10!

You can use if commands to do the same in shell scripts. Also while ...do ...done is another option.
Life is a promise, fulfill it!
Helen French
Honored Contributor

Re: for (int i = 200; i > 10; i++)

Pete, John and John P:

As far as I know "for (int i = 200; i > 10; i++)" means:

1) i=200 (integer value)

2) if "i" > 10, then do list of commands and increment "i" by one.

This loop will never end unless "i" should be "i-1" in the loop.
Life is a promise, fulfill it!
John Poff
Honored Contributor

Re: for (int i = 200; i > 10; i++)

Hi Shiju,

Exactly. That's why I commented that his syntax looks backwards. I assume he intends to start i at 10 and increment by one to 200, but the way it is written it would be an infinite loop. Sometimes we just have to guess at what people are really asking. :)

JP
Helen French
Honored Contributor

Re: for (int i = 200; i > 10; i++)

JP,

Absolutely! I didn't read your first reply carefully :(( I think your guessing was right.
Life is a promise, fulfill it!
Pete Randall
Outstanding Contributor

Re: for (int i = 200; i > 10; i++)

And that's why I prefaced my response with a guess at what was being attempted as well.


Pete

Pete
Donny Jekels
Respected Contributor

Re: for (int i = 200; i > 10; i++)

thanx guys,
I was'nt looking for syntactical answers, jut the meat.

peace
Donny
"Vision, is the art of seeing the invisible"
Ralph Grothe
Honored Contributor

Re: for (int i = 200; i > 10; i++)

I really cannot see why everyone is so much fixed on the Korn shell.
First it is proprietary software, second there is a far more powerful OpenSource alternative, viz. Bash.

In Bash you can exactly use the C-like idiom, you were suggesting.

e.g. count from 10-20

$ echo $0
bash

$ for ((i=10; i<=20; i++)); do printf "%4u\n" $i; done
10
11
12
13
14
15
16
17
18
19
20
Madness, thy name is system administration