为了防止搞混,整理一份自存(面向CSDN编程法(大雾
1.C语言
http://c.biancheng.net/c/32/
1.1 if语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <stdio.h> int main(){ char c; printf("Input a character:"); c=getchar(); if(c<32) printf("This is a control charactern"); else if(c>='0'&&c<='9') printf("This is a digitn"); else if(c>='A'&&c<='Z') printf("This is a capital lettern"); else if(c>='a'&&c<='z') printf("This is a small lettern"); else printf("This is an other charactern"); return 0; }
·判断条件用()括起来;
·语句块需要加{ }。
1.2 switch语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <stdio.h> int main(){ int a; printf("Input integer number:"); scanf("%d",&a); switch(a){ case 1: printf("Mondayn"); break; case 2: printf("Tuesdayn"); break; case 3: printf("Wednesdayn"); break; case 4: printf("Thursdayn"); break; case 5: printf("Fridayn"); break; case 6: printf("Saturdayn"); break; case 7: printf("Sundayn"); break; default:printf("errorn"); break; } return 0; }
1.3 for语句
1
2
3
4
5
6int main(){ int i = 1, sum = 0; for( ; i<=100; i++){ sum+=i; }
·for()内语句在一定情况下可省略,for(;; ) = while(1);
·for( sum=0,i=1; i<=100; i++ ) sum=sum+i;
1.4 while语句
1
2
3
4
5
6
7
8
9
10
11#include <stdio.h> int main(){ int i=1, sum=0; while(i<=100){ sum+=i; i++; } printf("%dn",sum); return 0; }
do while
1
2
3
4
5
6
7
8
9
10
11#include <stdio.h> int main(){ int i=1, sum=0; do{ sum+=i; i++; }while(i<=100); printf("%dn", sum); return 0; }
while(i<=100);要加分号!!
C++和C在基础分支循环结构上基本一样。
2.python语言
python 不需要加分号’
2.1 if语句
1
2
3
4
5
6
7
8
9
10x, y, z = 6, 5, 4 if x < y: small = x if z < small: small = z elif y < z: small = y else: small = z
·判断语句没有(),加了:;
·语句块通过缩进,不需要{ };
·else if 简化为elif;
·三元操作符(当然这个C也有)small = x if (x < y and x < z) else(y if y < z else z)
2.2 switch 语句
python没有这个语句,官方网站介绍该功能可以通过elif实现;
社区里也有一些其他方法实现这个功能,比如
字典
1
2
3
4
5
6
7def foo(var): return { 'a': 1, 'b': 2, 'c': 3, }.get(var,'error')
或者匿名函数
1
2
3
4
5
6
7def foo(var,x): return { 'a': lambda x: x+1, 'b': lambda x: x+2, 'c': lambda x: x+3, }[var](x)
2.3 for 语句
1
2
3
4
5
6
7for i in range(10): if i%2 != 0: print(i) continue i += 2 print(i)
2.4 while语句
1
2
3
4
5
6
7count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!"
3.matlab
https://ww2.mathworks.cn/help/matlab/control-flow.html
matlab需要加end
3.1 if 语句
1
2
3
4
5
6
7
8
9
10
11
12
13for c = 1:ncols for r = 1:nrows if r == c A(r,c) = 2; elseif abs(r-c) == 1 A(r,c) = -1; else A(r,c) = 0; end end end A
·判断语句不需要加( ),也不需要:
3.2 switch 语句
1
2
3
4
5
6
7
8
9
10
11
12n = input('Enter a number: '); switch n case -1 disp('negative one') case 0 disp('zero') case 1 disp('positive one') otherwise disp('other value') end
·常量n不需要加括号
·case后也没有:
·default:变成了otherwise
1
2
3
4
5
6
7
8
9
10
11
12
13
14x = [12 64 24]; plottype = 'pie3'; switch plottype case 'bar' bar(x) title('Bar Graph') case {'pie','pie3'} pie3(x) title('Pie Chart') otherwise warning('Unexpected plot type. No plot created.') end
基于 plottype 的值确定要创建哪种类型的绘图。如果 plottype 为 ‘pie’ 或 ‘pie3’,则创建一个三维饼图。使用元胞数组包含两个值。
·与C语言不通,switch不仅可以使整型,也可以是字符串;
·也可以多个值进行比较;
3.3 for 语句
1
2
3
4for v = 1.0:-0.2:0.0 disp(v) end
或者
1
2
3
4for v = [1 5 8 17] disp(v) end
遇到矩阵
1
2
3
4
5for I = eye(4,3) disp('Current unit vector:') disp(I) end
遇到矩阵时把一列看成一个传统的元素;
3.4 while 语句
1
2
3
4
5
6
7
8
9
10
11limit = 0.8; s = 0; while 1 tmp = rand; if tmp > limit break end s = s + tmp; end
最后
以上就是悲凉楼房最近收集整理的关于整理C,python,matlab中基础分支循环结构(if,switch,for,while)格式1.C语言2.python语言3.matlab的全部内容,更多相关整理C,python,matlab中基础分支循环结构(if,switch,for,while)格式1内容请搜索靠谱客的其他文章。
发表评论 取消回复