LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2006, 03:54 AM   #1
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Rep: Reputation: 47
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.

Last edited by Gins; 07-29-2006 at 03:56 AM.
 
Old 07-29-2006, 04:41 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
Old 07-29-2006, 05:57 AM   #3
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
Thanks konsolebox for the reply.

Usually we write only one set of paranthesis. Here we have two set of them.

I removed one of them. No, it didn't work.



tot%2
I wrote ' tot%5 '. It worked; but the output was a different picture.

Last edited by Gins; 07-29-2006 at 06:00 AM.
 
Old 07-29-2006, 10:57 AM   #4
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47
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?

Last edited by Gins; 07-29-2006 at 11:01 AM.
 
Old 07-29-2006, 11:14 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
you can read the following docs:

Bash Reference Manual
http://www.gnu.org/software/bash/manual/bashref.html

Advanced Bash-Scripting HOWTO
http://docsrv.caldera.com:8457/en/AdvBashHowto/

Bash Beginners Guide
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

you can also try the following commands:
Code:
man bash
help
help for
help <command>
 
Old 07-29-2006, 11:45 AM   #6
Gins
Senior Member
 
Registered: Jul 2004
Location: Germany
Distribution: open SUSE 11.0, Fedora 7 and Mandriva 2007
Posts: 1,662

Original Poster
Rep: Reputation: 47

Thanks konsolebox for taking time to help me.

Now look at the following:

---------------------------------------------------
# vi nestedfor.sh
for (( i = 1; i <= 5; i++ )) ### Outer for loop ###
do

for (( j = 1 ; j <= 5; j++ )) ### Inner for loop ###
do
echo -n "$i "
done

echo "" #### print the new line ###

done
-----------------------------------------------------
I named the above as ' nested1 '.
The above works fine and the output is as follows:

[nissanka@c83-250-110-112 ~]$ chmod 755 nested1
[nissanka@c83-250-110-112 ~]$ ./nested1
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

The question is which is the outer loop and which is the inner loop.

Do you have any idea?
 
Old 07-29-2006, 12:21 PM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.
 
Old 07-29-2006, 02:48 PM   #8
Randux
Senior Member
 
Registered: Feb 2006
Location: Siberia
Distribution: Slackware & Slamd64. What else is there?
Posts: 1,705

Rep: Reputation: 55
cough *homework assignment* cough
 
Old 07-29-2006, 03:31 PM   #9
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I don't think so as the example pops up pretty quick when googling for "linux script chess board"

At any rate, I changed it just abit so it will appear more like a chess board. Four squares each .... j = 2
Code:
#!/bin/bash

clear
echo ""

for (( i = 1; i <= 8; i++ )); do
  for (( j = 2 ; j <= 9; j++ )); do
    tot=$(expr $i + $j)
    tmp=$(expr $tot % 2)
    if [ $tmp -eq 0 ]; then
      echo -e -n "\033[47m  "
    else
      echo -e -n "\033[40m  "
    fi
  done
 echo ""
done
echo -e "\033[0m\n"

Last edited by homey; 07-29-2006 at 03:49 PM.
 
Old 07-29-2006, 05:24 PM   #10
whk
Member
 
Registered: Jun 2005
Posts: 202

Rep: Reputation: 37
chessboard trivia

actually a chessboard diagram is dark square at lower left at a1

Code:
#!/bin/bash

clear
echo ""
for (( i = 1; i <= 8; i++ )); do
  for (( j = 2 ; j <= 9; j++ )); do
    tot=$(expr $i + $j)
    tmp=$(expr $tot % 2)
    if [ $tmp -eq 0 ]; then
      echo -e -n "\033[40m   "
    else
      echo -e -n "\033[47m   "
    fi
  done
 echo -e "\033[0m\n"
 ### echo "" ### option ###
done

Last edited by whk; 08-01-2006 at 05:27 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration