当前位置:网站首页>C language learning
C language learning
2022-07-01 12:53:00 【51CTO】
Branches and loops (1)
Text
One . Branch statements and loop statements
Branch statement
if switch
Loop statement
while for do while go to sentence
What is a sentence ?
c There is a semicolon in language (;) Separated by a statement .
1. Branch statement ( Selection structure )
1).if sentence
if The grammatical structure of a sentence :
if( expression ) or if( expression )
sentence ; person sentence 1;
else
sentence 2;
Multiple branches
if( expression 1)
sentence 1;
else if( expression 2)
sentence 2;
else
sentence 3;
If the result of the expression is true , Then the statement executes (0 Said the false , Nonzero means true )
int main()
{
int age = 111;
if ( age < 18)
printf( " A minor \n");
else if ( age >= 18 && age < 20) //&& Representation logic and , If the result of the expression is true , Then the statement executes (0 Said the false , Nonzero means true )
printf( " teenagers \n");
else if ( age >= 20 && age < 50)
printf( " Prime of life \n");
else if ( age >= 50 && age < 90)
printf( " aged \n");
else
printf( " Old man does not die \n");
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
If the condition holds , To execute multiple statements , Code blocks should be used
#include <stdio.h>
int main()
{
if( expression )
{
Statement list 1;
}
else
{
Statement list 2
}
return 0;
}
int main()
{
int age = 111;
if ( age < 18)
{
printf( " A minor \n"); // Statement list 1
printf( " Can't fall in love \n"); // You can print multiple printf
}
else
{
if ( age >= 18 && age < 20)
printf( " teenagers \n");
else if ( age >= 20 && age < 50) // Statement list 2
printf( " Prime of life \n");
else if ( age >= 50 && age < 90)
printf( " aged \n");
else
printf( " Old man does not die \n");
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
there {} It's just a code block
In the air else
// Use properly {} It can make the logic of the code clearer
// Code style is important
practice : Output 1~100 Between the odd numbers
2).switch sentence
switch The grammatical structure of a sentence :
switch( Integer expression )
{
Statement item ;
}
What are statement items ?
// It's some case sentence
// as follows :
case Integral constant expression :
sentence ;
//switch There can be if sentence
int main()
{
int day = 0;
scanf( "%d", & day); //& Fetch address , Take out variables day
if ( 1 == day) // Judgment variable day Is it equal to 1, If yes, output on Monday
printf( " Monday \n");
else if( 2 == day)
printf( " Tuesday \n");
else if ( 3 == day)
printf( " Wednesday \n");
else if ( 4 == day)
printf( " Thursday \n");
else if ( 5 == day)
printf( " Friday \n");
else if ( 6 == day)
printf( " Saturday \n");
else if ( 7 == day)
printf( " Sunday \n");
return 0;
}
// use else if...else if More complicated , use switch Multi branch statements are relatively simple
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
After the change
int main()
{
int day = 0; //day For integer variables
scanf( "%d", & day);
switch ( day) //day Is an integer expression
{
case 1: //case 1(1 Is a constant variable expression ):
printf( " Monday \n");
break; //break It means to end the cycle
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;
}
return 0;
}
// summary : Easy to understand
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
stay switch Statement break:
stay switch In the sentence , We can't Branch , With break You can realize the real Branch .
practice : Input 1~5, Output “weekday”; Output 6~7, Input “weekend”
int main()
{
int day = 0;
scanf( "%d", & day);
switch( day)
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf( "weekday\n");
break;
case 6:
case 7:
printf( "weekend\n");
break;
default: //default Can be placed in switch Anywhere , When switch The value of the expression is not equal to case The value of the tag , This is the time default The following statement will execute .
printf( " Input error \n");
break;
}
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
break The actual effect of the statement is to divide the statement list into different parts .
default Clause (default and case There is no order , It can be used anywhere )
If the expressed value is the same as all case What if the value of the tag is different ?
It's nothing , The result is that all statements are skipped ; The program will not terminate , No errors reported , Because this situation is c It is not suitable to report errors .
What if you don't want to ignore the values of expressions that don't match all tags ?
You can add an item to the statement list default Clause , Put the label below default: Write in any one case Where labels can appear ( When switch The value of the expression does not match all case The value of the tag , This default The statement after the clause will be executed . all , Every switch Only one... Can appear in the statement default Clause , But it can appear anywhere in the statement list , And the statement flow will run through case The label runs through default Clause .)
practice :
int main()
{
int n = 1;
int m = 2;
switch ( n)
{
case 1: m ++;
case 2: n ++;
case 3:
switch ( n)
{ //switch Allow nesting
case 1: n ++;
case 2: m ++; n ++; break;
}
case 4: m ++;
break;
default:
break;
}
printf( "m=%d,n=%d\n", m, n);
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
1. Loop statement
while for do while
1).while sentence
while Grammatical structure :(while sentence , Loop can be realized )
while( expression )
Loop statement ;
practice : Print... On the screen 1~10
break stop it
continue skip , Make the entry judgment of the next cycle
Conclusion :continue It means a dead cycle ( The cycle is not over )
边栏推荐
- leetcode:226. 翻转二叉树【dfs翻转】
- Logstash error: cannot reload pipeline, because the existing pipeline is not reloadable
- 用.Net Core接入微信公众号开发
- 79. Word search [DFS + backtracking visit + traversal starting point]
- 79. 单词搜索【dfs + 回溯visit + 遍历起点】
- Perl 5.10.0 installation package download
- 请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
- Topic 1004: the story of cows (recursion)
- mysql统计账单信息(下):数据导入及查询
- localtime居然不可重入,踩坑了
猜你喜欢
【脑洞大开】《西潮》及《走向世界丛书》
游戏公会在去中心化游戏中的未来
codeforces -- 4B. Before an Exam
The popular major I chose became "Tiankeng" four years later
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现
我花上万学带货:3天赚3元,成交靠刷单
基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
软件测试中功能测试流程
VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
随机推荐
Three stages of aho
Need your own cognition
数字信号处理——线性相位型(Ⅱ、Ⅳ型)FIR滤波器设计(2)
木架的场景功能
题目 1004: 母牛的故事(递推)
There are still many things to be done in the second half of the year
Fiori 应用通过 Adaptation Project 的增强方式分享
[brain opening] west tide and going to the world series
Blocking sockets的读写操作该怎么玩?
高薪程序员&面试题精讲系列118之Session共享有哪些方案?
香港科技大学李泽湘教授:我错了,为什么工程意识比上最好的大学都重要?
How to count the status of network sockets?
哪个券商公司开户佣金低又安全又可靠
MHA high availability cluster deployment and failover of database
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
买卖其实也有风险
Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures
Router.use() requires a middleware function but got a Object
数字化转型再下一城,数字孪生厂商优锘科技宣布完成超3亿元融资