pppppppppppppopppo [opooc ## all programming languages can not do without flow control, no flow control also play a hair ~
- Process control is divided into three types: sequence, selection, cycle
1) order:
This nothing to say it, as a function of the previous chapter basis, top-down line by line explanation , demand: How to cook instant noodles to complete
the process of programming abstractions of the human behavior, the human thing to do with each describe the process, Therefore, the following pseudo-code:
#定义变量
面饼,调料,水,承载器皿
#制作方法 (这个就是函数了,选择不同的方法,二选一,也可以掰一半,一半泡一半煮-_-#)
function 泡() {
制作过程xxxxx
}
function 煮() {
#定义变量
火
制作过程xxxxx
}
2) Select:
Branch statement, and if the conditions are met xx, xx execution; yy meet the conditions, execution yy ...
I do not speak complex changes, they talk about a fixed format, later wrote as I write this fixed format, certainly right
shell script fixed selection process is as follows :
if [ ];then
xxxxx #做什么事儿,自己填内容
elif [];then #不是必要的格式,增加条件分支
xxxxx #做什么事儿,自己填内容
else
xxxxx #做什么事儿,自己填内容
fi
for example:
if [ 3 -gt 2 ];then #如果3>2,打印我最帅
echo "我最帅"
elif [ 2 -lt 3 ];then #如果2<3,打印我最帅,if...elif 和 if...if的区别我在后续流程进阶中讲
echo "我最帅"
else
echo "我也最帅" #如果上述条件都不满足,我也最帅
fi
3) cycle:
Childhood teacher asked 1 + ... + 100 is equal to the number, a lot of people know that 5050, there are several ways to calculate?
Attended school knew two kinds:
one is superimposed silly Baji, not to mention 1 + ... + 100, I pain free eggs may calculate 1 + ... + ∞;
One is Mr. genius mathematician Gaosi 1 + 100 + 99 = 101 = 101,2 algorithm of this quality.
Distinguish the difference between the two algorithms, data structures, if the university learned to know first algorithm complexity is O (n), the second is O (1)
to learn more about Big O count method, please see the data structure textbooks University (Yan Wei Min version)
with the most stupid way is to count the cycle:
Do not learn grammar, science fixed format, for and while two kinds of cycles , until I advanced to the follow-up talk anymore.
for i in {1..200} #1到200,打印出每个值,循环200次呗
do
echo $i
done
i=0
while [ $i -lt 201 ]
do
echo $i
let i++ #只要i比201小,那么我就让i自增1,直到i=201的时候跳出循环,即完成1-200的循环
done
for example:
#!/bin/bash
i=0
sum=0 #两个变量初始化为0
while [ $i -lt 5 ] #如果i小于5
do
echo $i #那么打印i值
let sum+=$i #让sum增加当前i值
let i++ #i自增1
done
echo $sum #sum是i的累加值
Code does not require understanding, understanding fixed format written shell procedure, follow-up article I will open the Advanced