整理C,python,matlab中基础分支循环结构(if,switch,for,while)格式

为了防止搞混,整理一份自存(面向CSDN编程法(大雾

1.C语言

http://c.biancheng.net/c/32/

1.1 if语句

#include <stdio.h>
int main(){
    char c;
    printf("Input a character:");
    c=getchar();
    if(c<32)
        printf("This is a control character\n");
    else if(c>='0'&&c<='9')
        printf("This is a digit\n");
    else if(c>='A'&&c<='Z')
        printf("This is a capital letter\n");
    else if(c>='a'&&c<='z')
        printf("This is a small letter\n");
    else
        printf("This is an other character\n");
    return 0;
}

·判断条件用()括起来;
·语句块需要加{ }。

1.2 switch语句

#include <stdio.h>
int main(){
    int a;
    printf("Input integer number:");
    scanf("%d",&a);
    switch(a){
        case 1: printf("Monday\n"); break;
        case 2: printf("Tuesday\n"); break;
        case 3: printf("Wednesday\n"); break;
        case 4: printf("Thursday\n"); break;
        case 5: printf("Friday\n"); break;
        case 6: printf("Saturday\n"); break;
        case 7: printf("Sunday\n"); break;
        default:printf("error\n"); break;
    }
    return 0;
}

1.3 for语句

int 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语句

#include <stdio.h>
int main(){
    int i=1, sum=0;
    while(i<=100){
        sum+=i;
        i++;
    }
    printf("%d\n",sum);
    return 0;
}

do while

#include <stdio.h>
int main(){
    int i=1, sum=0;
    do{
        sum+=i;
        i++;
    }while(i<=100);
    printf("%d\n", sum);
    return 0;
}

while(i<=100);要加分号!!
C++和C在基础分支循环结构上基本一样。

2.python语言

python 不需要加分号’

2.1 if语句

x, 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实现;
社区里也有一些其他方法实现这个功能,比如
字典

def foo(var):
    return {
            'a': 1,
            'b': 2,
            'c': 3,
    }.get(var,'error')

或者匿名函数

def foo(var,x):
    return {
            'a': lambda x: x+1,
            'b': lambda x: x+2,
            'c': lambda x: x+3, 
    }[var](x)

2.3 for 语句

for i in range(10):
    if i%2 != 0:
        print(i)
        continue
    i += 2
    print(i)

2.4 while语句

count = 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 语句

for 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 语句

n = 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

x = [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 语句

for v = 1.0:-0.2:0.0
   disp(v)
end

或者

for v = [1 5 8 17]
   disp(v)
end

遇到矩阵

for I = eye(4,3)
    disp('Current unit vector:')
    disp(I)
end

遇到矩阵时把一列看成一个传统的元素;

3.4 while 语句

limit = 0.8;
s = 0;

while 1
    tmp = rand;
    if tmp > limit
        break
    end
    s = s + tmp;
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值