Switch 没有break和default位置乱放情况分析

package flowcontrol;

public class SwitchCase {
// first default
public static void testFirst(int i) {
switch (i) {
default:
System.out.println("default");// first default
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("there");
}
}

// last default
public static void testLast(int i) {
switch (i) {
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("there");
default:
System.out.println("default");// last default
}
}

// middle default
public static void testMiddle(int i) {
switch (i) {
case 1:
System.out.println("one");
case 2:
System.out.println("two");
default:
System.out.println("default");// middle default
case 3:
System.out.println("there");

}
}

public static void main(String[] args) {
// first default
testFirst(2);
System.out.println("------------------");
testFirst(9);

System.out.println("|||||||||||||||||||||||||||||||||||");

// last default
testLast(2);
System.out.println("----------------");
testLast(9);

System.out.println("|||||||||||||||||||||||||||||||||||");
// middle default
testMiddle(2);
System.out.println("----------------");
testMiddle(9);

}

}



输出结果是什么呢,看了结果,我是这么理解的:
(1)switch语句关键地方是进入点,有了进入点没有break的情况下会执行到底
(2)没有匹配值的时候default就是进入点,进入default以后会和普通进入点一样,如果没有break继续执行下面语句
two
there
------------------
default
one
two
there
|||||||||||||||||||||||||||||||||||
two
there
default
----------------
default
|||||||||||||||||||||||||||||||||||
two
default
there
----------------
default
there
#include <msp430f5438a.h> #include <stdint.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #define CPU_F (1045000UL) #define Delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0)) #define uchar unsigned char unsigned char duty_cycle_value = 75; // 初始占空比值 char data[20] = {0}; char datapro[5]={0}; char hex[10]={0}; volatile uint8_t id_index = 0; // 数据索引 volatile uint8_t id_ready = 0; // 数据准备标志 volatile uint8_t jiebo = 0; //几阶波的标志 volatile uint8_t sanjie =0; volatile uint8_t jie =0; char e=0; int i=0; int m=0; int s=0; void XT2(); char five_code[32]={1,1,1,1,1,1,1,0,1,1,1,0,1,0,0,0,1,1,1,0,1,0,0,0,1,0,0,0,0,0,0,0};//五阶码 char three_code[8]={1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0,0, 0,0,0,0, 0,0,0,0, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,0, 1,1,1,1, 1,1,1,0, 1,1,1,0, 1,0,0,0, 1,1,1,1, 1,1,1,0, 1,1,1,0, 1,0,0,0, 1,1,1,0, 1,0,0,0, 1,0,0,0, 0,0,0,0, 1,1,1,1, 1,1,1,0, 1,1,1,0, 1,0,0,0, 1,1,1,0, 1,0,0,0, 1,0,0,0 ,0,0,0,0, 1,1,1,0, 1,0,0,0, 1,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,1,1,0, 1,0,0,0, 1,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}; int main (void) { WDTCTL = WDTPW | WDTHOLD; XT2(); P3SEL |= 0x30; // 设置P3.4/P3.5为UART模式 // 配置UART UCA0CTL1 |= UCSWRST; // 进入复位状态 UCA0CTL1 |= UCSSEL_1; // 使用ACLK (32768 Hz) UCA0BR0 = 0x03; // 波特率设置: 32768 / 9600 ≈ 3.41 UCA0BR1 = 0x00; UCA0MCTL = UCBRS_3; // 调制参数 UCA0CTL1 &= ~UCSWRST; // 退出复位状态 UCA0IE |= UCRXIE; // 使能接收中断 __bis_SR_register(GIE); while(1){ if(id_ready==1){ //数据提取并处理,ASCLL码转十进制数 int shu1 = data[10]-'0'; int shu2 = data[11]-'0'; int shu3 = data[12]-'0'; //端口配置 int v = shu1*100 + shu2*10 + shu3*1; //端口配置 P4DIR |= BIT1 ; P4DIR |=BIT2; P4OUT &=~ BIT1 ; P4OUT &=~BIT2; //运算求周期 int c= sizeof(three_code); int b=c*v*8; int a =1000000/b ; //TIMER_B配置 TB0CCR0 = a ; TB0CCTL0 = CCIE; TB0CTL = TBSSEL_2 | MC_3 | TBCLR ;//SMCLK;增减计数;清零; id_ready=0; //清除标志 //运行结束更新标志位 } /*if((e==1)&&){ if((three_code[i-1]!=three_code[i])&&(jie==1)) { P4OUT &=~BIT2; P4OUT &=~BIT1; __delay_cycles(10); m=0; } }*/ } } //目前适用于100-999频率,可改进(if语句判别数据长度) #pragma vector=USCI_A0_VECTOR __interrupt void USCI_A0_ISR(void) { switch (__even_in_range(UCA0IV, 4)) { case 0: break; // 无中断 case 2: // 接收中断 data[id_index++] = UCA0RXBUF; // 修正寄存器名 //判断数据帧尾 AA 55 E7 if((data[id_index-1]==0xE7)&&(data[id_index-2]==0x55)&&(data[id_index-3]==0xAA)){ if(data[2]==0x07){ //只接收数据data[2]=07的数据 if((data[6]==0x02)&&(data[4]==0x03)){ id_ready = 1;//标志更新 sanjie=1; } if((data[6]==0x0B)&&(data[4]==0x03)){//标志分辨是几阶波 //三阶波 if(data[10]==0x33){ jiebo = 1;//标志更新 jie=1; } //五阶波 if(data[10]==0x35){ jiebo = 2;//标志更新 jie=1; } } } else { id_ready=0; id_index=0; return;//退出中断 } break; } case 4: break; // 发送中断 default: break; } } //定时器B中断 高低电平转换 #pragma vector=TIMER0_B0_VECTOR __interrupt void TIMER0_B0_ISR(void) { // 根据数组设置输出 switch(m=0){ case 0: if(three_code[i]==1) { P4OUT |= BIT1; // 数组为1 高电平 P4OUT &=~ BIT2; } else { P4OUT &= ~BIT1; // 数组为0 低电平 P4OUT |= BIT2; } /* if(three_code[i]==0) { P4OUT |= BIT2; // 数组为0 高电平 } else { P4OUT &=~ BIT2; // 数组为1 低电平 } */ i++; if(three_code[i-1]!=three_code[i]) { m=1; } if(i==8){ i=0; } case 1: if(m==1){ // P4OUT &=~BIT2; Delay_us(10); m=0; } } } //XT2外部晶振配置 void XT2 () { uchar i ; P7SEL |= BIT0 + BIT1; //P7.0 P7.1 P5SEL |= BIT2+BIT3;//P5.2 P5.43 UCSCTL6 &=~ XT2OFF;//启用 UCSCTL6 |= XCAP_3;//12PF电容调用 do{ UCSCTL7 &=~ (XT2OFFG | XT1LFOFFG | XT1HFOFFG | DCOFFG);//标志位 SFRIFG1 &= ~OFIFG; for (i = 0xFF;i > 0; i--); }while(SFRIFG1 & OFIFG ); UCSCTL4 |= SELS__XT2CLK ;//XT2给smclk UCSCTL5 |= DIVS_3;//8分频 1MHz } P4OUT &=~BIT2;这一句注释了波形正确,不注释错误,这段是为了将波变为低电平持续时间取决于延时函数
最新发布
07-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值