Shell learning whimsy ^_^ print 9*9 multiplication table

Source: Internet
Author: User

From the National Day holiday to today, "linux Command Line and advanced shell programming" has initially learned chapter 10. Basically, I can write some simple process control scripts. Today, an unexpected whim occurred, trying to create a multiplication table of 9*9. After thinking for a long time, I finally got the result. However, it is not mature enough. Then I asked a few friends in the group. I learned a lot about their ideas and posted them here for reference and study only.

Requirement: print the 9*9 multiplication table.

My methods

C-type for loop nested C-type sub-for loop;

Nested if statement in the C-type subfor loop;

Break control loop;

[root@Jason64-17 ~]# cat pro99.sh#!/bin/bash#program#  this program shows your 9 * 9 = ?#history#10/04/13   lisp    first   release#QQ:1031239088for ((i=1; i<10; i++))do  for ((j=1; j<=10; j++))  do    if [ $i -lt $j ]    then      break    fi    pro=$[$i * $j]    echo -ne "$i*$j=$pro\t"  done  echo -e "\n"done


You can also use continue to control loops.


[root@Jason64-17 ~]# cat pro99.sh#!/bin/bash#program#  this program shows your 9 * 9 = ?#history#10/04/13   lisp    first   release#QQ:1031239088for ((i=1; i<10; i++))do  for ((j=1; j<=10; j++))  do    if [ $i -lt $j ]    then    #  break       continue    fi    pro=$[$i * $j]    echo -ne "$i*$j=$pro\t"  done  echo -e "\n"done


Both outputs are the same, as shown below:


[root@Jason64-17 ~]# sh pro99.sh1*1=12*1=2   2*2=43*1=3   3*2=6   3*3=94*1=4   4*2=8   4*3=12  4*4=165*1=5   5*2=10  5*3=15  5*4=20  5*5=256*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=367*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=498*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=649*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81[root@Jason64-17 ~]#


PS: the reason for the editor is that each line is separated by one blank line. Not shown here]

A netizen's solution, he usedC Language.

#include<stdio.h>int main(){int i,j;for (i=1;i<=9;i++){   for(j=1;j<=i;j++){   printf("%d*%d=%d\t",j,i,i*j);}printf("\n");}system("pause");return 0;}


The output result is

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131227/2024295943-0.jpg "title =" C Language Output "alt =" 175103354.jpg"/>

After reading his code, I found that he is better than my code.

The good thing is that he used variable I directly in the iteration loop of the C-type subfor.

Then I made some improvements.

[root@Jason64-17 ~]# cat pro99.sh#!/bin/bash#program#  this program shows your 9 * 9 = ?#history#10/04/13   lisp    first   release#QQ:1031239088for ((i=1; i<10; i++))do  for ((j=1; j<=$i; j++))  do    pro=$[$i * $j]    echo -ne "$i*$j=$pro\t"  done  echo -e "\n"done


[root@Jason64-17 ~]# sh pro99.sh1*1=12*1=2   2*2=43*1=3   3*2=6   3*3=94*1=4   4*2=8   4*3=12  4*4=165*1=5   5*2=10  5*3=15  5*4=20  5*5=256*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=367*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=498*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=649*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81[root@Jason64-17 ~]#


In this way, the code is simplified and the logic is clearer than the first one.
I am deeply aware of the wide and profound nature of the program ~~~ Haha -_-


Bytes ----------------------------------------------------------------------------------------

However, I have seen some netizens write this on the Internet.

[root@Jason64-17 ~]# cat wnet.sh#!/bin/bashfor i in `seq 1 9`do for n in `seq 1 9` do echo -ne " $n*$i=$(($i*$n))"  if [ $n -ge $i ]  then   echo -e '\n'  break fi done echodone


This for loop uses the for Loop in bash to read the value of a variable from the variable set in sequence;

Then, the if Condition Statement is used to control loop execution through break.

However, you must note the following two cyclic control commands: break and continue.

Break can jump out of a single loop; can jump out of an External Loop; can jump out of an internal loop.

You can also stop the commands in the loop ahead of schedule. Do not terminate the loop!

Both of them have their own advantages. Therefore, when using them, be sure to execute certain external or internal circular commands. These are just not what you want!

Be sure to pay attention to: the location depends on the situation.

......

......

That's all about it. I will try again later.

----------------------------------------------

This article from the "lisp O & M path" blog, please be sure to keep this source http://lspgyy.blog.51cto.com/5264172/1304627

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.