ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Rep:
A chessboard
# Making a chessboard
for (( i = 1; i <= 20; i++ )) ### Outer for loop ###
do
for (( j = 1 ; j <= 110; j++ )) ### Inner for loop ###
do
tot=`expr $i + $j`
tmp=`expr $tot % 2`
if [ $tmp -eq 0 ]; then
echo -e -n "\033[41m "
else
echo -e -n "\033[44m "
fi
done
echo -e -n "\033[40m" #### set back background colour to black
echo "" #### print the new line ###
done
-------------------------------------------------
The above works fine. However, I have a few questions.
1. What is the necessity of two paranthesis ' (( '
in the for statement?
2. There are two variables, namely 'tot' and 'tmp'. Why is ' tot%2 ' ? Why can't it be ' tot%5 ' or some other figure?
3. I can't see an outer loop and an inner loop when I look at the final outcome. It is just a chessboard.
4. Usually we initialize variables. Here we have two variables, namely 'i' and 'j'. Is it unnecessary to initialize in this program?
5. What is the use of 'else' statement here? We write the 'else' statement if something is not true. In this programme, the untrue aspect doesn't arise.
I hope our friends shed some light on my questions.
I don't know if you really mean to ask these questions. But I'll answer them anyway.
1. What is the necessity of two paranthesis ' (( '
in the for statement?
It's only part of the for syntax.
2. There are two variables, namely 'tot' and 'tmp'. Why is ' tot%2 ' ? Why can't it be ' tot%5 ' or some other figure?
Because there are only two colors in the board not 5.
3. I can't see an outer loop and an inner loop when I look at the final outcome. It is just a chessboard.
I guess the output of this program is only to create a board.
4. Usually we initialize variables. Here we have two variables, namely 'i' and 'j'. Is it unnecessary to initialize in this program?
Most of the time it's no longer needed in bash.
5. What is the use of 'else' statement here? We write the 'else' statement if something is not true. In this programme, the untrue aspect doesn't arise.
It's true if tmp is an even number and false if not.
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662
Original Poster
Rep:
I still need your help to understand those double parenthesis.
The explantion is as follows:
for (( i = 1; i <= 20; i++ ))
do Begin the outer loop which runs 20 times., and the outer loop terminets when the value of i exceeds 20
for (( j = 1 ; j <= 110; j++ ))
do
Begins the inner loop, for each value of i the inner loop is cycled through 110 times, with the varible j taking values from 1 to 110. The inner for loop terminates when the value of j exceeds 110.
I can't fathom out the need of double parnthesis. Because we usually need a pair of parenthesis to write the 'for' statement.
Are you able to tell me more about it or show me a webpage to learn about it?
The outer loop is the one controlling which number is being printed (and starting new lines) and the inner one is actually printing that number 5 times. I haven't done shell scripting before, but it's pretty simple. Maybe a good (?) and simple way to check you understand how the nested loops are working is to go through the code and kind of "execute" it yourself. What I mean is to get some paper and just write down the output and keep track of the loop variables (i and j) in your head. It helps me when I need to use nested loops, anyway.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.