switch case without break in C language
2015-11-23 09:42
369 查看
In C switch case senario, if you not using break key word, pay attention the control flow!
Code example(net/bridge/br_input.c in linux kernel)
forward:
switch (p->state) {
case BR_STATE_FORWARDING: //[1]
rhook = rcu_dereference(br_should_route_hook);
if (rhook != NULL) {
if (rhook(skb))
return skb;
dest = eth_hdr(skb)->h_dest;
}
/* fall through */ <========== the above case without break !!!
case BR_STATE_LEARNING: //[2]
if (!compare_ether_addr(p->br->dev->dev_addr, dest))
skb->pkt_type = PACKET_HOST;
NF_HOOK(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
br_handle_frame_finish);
break;
default:
drop:
kfree_skb(skb);
}
return NULL;
So above piece of code will following below control flow:
when p->state == BR_STATE_LEARNING, the code will skip [1] and only execute [2],
when p->state == BR_STATE_FORWARDING, the code will first go [1], then will continue execute [2], no need judge condition "p->state == BR_STATE_LEARNING" again.
Finally summary the function br_handle_frame_finish will be called either p->state == BR_STATE_LEARNING or when p->state == BR_STATE_FORWARDING.
In shell language, there is no break key word, but it's behavior like exist break.
Following example will show if apple match, the case will break out immediately, means output will be
Code example(net/bridge/br_input.c in linux kernel)
forward:
switch (p->state) {
case BR_STATE_FORWARDING: //[1]
rhook = rcu_dereference(br_should_route_hook);
if (rhook != NULL) {
if (rhook(skb))
return skb;
dest = eth_hdr(skb)->h_dest;
}
/* fall through */ <========== the above case without break !!!
case BR_STATE_LEARNING: //[2]
if (!compare_ether_addr(p->br->dev->dev_addr, dest))
skb->pkt_type = PACKET_HOST;
NF_HOOK(PF_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL,
br_handle_frame_finish);
break;
default:
drop:
kfree_skb(skb);
}
return NULL;
So above piece of code will following below control flow:
when p->state == BR_STATE_LEARNING, the code will skip [1] and only execute [2],
when p->state == BR_STATE_FORWARDING, the code will first go [1], then will continue execute [2], no need judge condition "p->state == BR_STATE_LEARNING" again.
Finally summary the function br_handle_frame_finish will be called either p->state == BR_STATE_LEARNING or when p->state == BR_STATE_FORWARDING.
In shell language, there is no break key word, but it's behavior like exist break.
Following example will show if apple match, the case will break out immediately, means output will be
"Apple pie is quite tasty."
#!/bin/sh FRUIT="apple" case "$FRUIT" in "apple") echo "Apple pie is quite tasty." ;; "banana") echo "I like banana nut bread." ;; "kiwi") echo "New Zealand is famous for kiwi." ;; esac
相关文章推荐
- Spark Streaming和Kafka整合开发指南
- centos7.0设置网络连接(IP,网关DNS)
- Python 最佳实践
- Oracle查询重复数据并删除,只保留一条记录
- android截屏
- 一个正则表达式测试(只可输入中文、字母和数字)
- IOS多线程编程的3种实现方法
- Android中延迟加载的问题
- iOS开发——React Native入门总结
- 程序存储问题
- AD域控制器 dsmod 修改命令
- Eclipse一些操作
- JAVA中三种URL连接方法
- 正则验证js大全
- 买基金,就买指数基金
- netperf 与网络性能测量
- Excel Sheet Column Title leetcode oj
- Wireshark实战分析之ARP协议(一)
- 记录驾校考点(20160426拿到驾照,完结)
- Qt on_pushButton_clicked()的用法