0x C++提供的三种基本控制结构
- 顺序结构:按照先后顺序依次执行程序中的语句
- 选择结构:按照给定条件有选择地执行程序中的语句
- 循环语句:按照给定规则重复地执行程序中的语句
1x 第一节 C++语句
语句以分号表示结束
- 六种C++语句
- 声明语句:用于对程序中的各种实体进行声明、定义和初始化。
- 表达式语句:用于对程序中的数据进行具体操作和处理。
- 选择语句:用于实现程序的选择结构。
- 循环语句:用于实现程序的循环结构。
- 跳转语句:用于实现程序执行流程的变换。
- 复合语句:用于表示程序中的语句块。
2x 第二节 顺序结构
顺序结构会按照程序的书写的先后顺序,从左到右,至上而下的执行。
2x.1 声明语句
- 变量声明
- 常量声明
- 函数声明
- 类型声明
2x.2 表达式语句
略
2x.3 基本输入输出
I/O操作通过标准库中的输入/输出流对象来完成。
- 标准输入输出流对象:
cin
cout
- 在使用标准输入/输出流之前必须用处理命令在引用
iostream.h
#include <iostream.h> using namespace std;
- 输出操作
cout<<Exper<<endl;
整个语句的含义是:将表达式Exper的值输出到屏幕上的当前光标位置。
Exper
代表一个表达式,endl
(end line的意思)是输入/输出操作符- 一个
cout
语句可以分写成为若干行,分写的行间有无分号亦可。
- 输入操作
cin>>Var
;Var
代表一个变量,并且支持连续性输入多项数据。cin
语句的一般格式为
cin>>变量1>>变量2>>...>>变量N;
- 输出操作
2x.4 符合语句和空语句
- 复合语句:用花括号
{}
将若干语句包围起来而组成的一条语句:- 函数的函数体
- 循环语句的循环体
- 选择语句的分支
- 空语句
;//无任何操作
3x 选择结构
3x.1 if
语句——条件语句
- 基本的
if
语句- 格式:
if(<条件表达式>) <语句>
- 若条件表达式的结果为真则执行后面的语句。
- 若想执行多条语句,必须用花括号
{}
把后面多条语句括起来。
*C++中规定如果条件表达式中的值不为0就是“真”*,只有为0才为“假”。
- 格式:
3x.2 if...else
语句
- 格式:
if (<条件表达式>) <语句1> else <语句二>
3x.3 if
语句的嵌套
- 格式:
if(<条件表达式>) <语句1>; else if(<条件表达式>) <语句2>; else <语句>;
else关键字
总是于它前面最近的未配对且可见的那个if关键字
匹配- 复合语句内的if关键字对于外界是不可见的。
例如:
if(<条件表达式>) //第一个if if(<条件表达式>) //第二个if { if(<条件表达式>) //第三个if } else <语句>; //与第二个else相匹配
友情提示:在真正的程序设计当中,必须要使用缩进、程序块等等手段来尽可能地避免程序可读性差,以后我会开辟博文写有关这个的方面
3x.2 switch
语句——开关语句
- 根据给定表达式不同取值来决定从语句序列中的哪一个开始执行。
- 常用语法:
switch (表达式) { case 常量表达式1: 语句1;break; case 常量表达式2:语句2; break; ... case 常量表达式n: 语句n; break; default:语句n+1; }
- 注意事项:
- 表达式的值必须是整型、字符型或枚举型
- 多个
case
标号可以共用一组语句序列,以实现对多个常量执行同一个操作 default
标号是可选择的switch
语句是可以嵌套的- 每一个
case
表达式的值必须是不相同的 case
和default
的出现次序不影响程序的运行
4x 循环结构
- 三种循环语句:
for
语句while
语句do...while
语句
4x.1 for
语句
- 常用格式:
for(表达式1;表达式2;表达式3) { <语句>; } //表达式1:循环变量初始化 //表达式2:循环条件 //表达式3:循环变量增值
- 注意事项:
- 先执行
表达式1
。 - 执行
表达式2
,若其值为真(值为非0)执行for中指定的内嵌语句,然后再执行表达式3
,重新进行判断;若其值为假(值为0). - 三个表达式从语法上可以省略
- 先执行
4x.2 While
语句——"当型循环"
- 常用格式:
while(条件表达式) { <语句>; }
- 条件是为真(表达式为非0)时,执行
while语句
中的内嵌语句。先判断,后循环
- 有多条语句要执行时,用花括号
{}
把多个语句括起来。 - 循环体中应该有使循环体催向结束的语句。
4x.3 do...while
语句——直到型循环
- 常用格式
do { <语句>; } while(条件表达式);
- 与
while语句
的区别,do...while语句
先执行后判断,至少执行一次;while语句
先判断后执行,可能一次也不执行。
4x.4 循环的嵌套
- 一个循环体内又包含另外一个完整的循环结构,称为
循环的嵌套
。 - 三种循环体都可以互相循环。
5x 跳转语句
- 使用跳转语句可以实现程序执行流程的无条件转换。
- C++提供了四种跳转语句,分别是:
break
语句continue
语句return
语句goto
语句
5x.1 break
语句
- 常用格式为
break;
break
语句可以用于多分支选择语句——switch
或循环语句的跳出操作,不可单独用于其他语句
5x.2 continue
语句
- 常用格式为
continue;
- 其作用为结束本次循环,即跳过该次循环,继续进行下一轮的循环。
continue
与break
的区别是:continue
只结束本次循环而不是终止整个循环的进行。break
是终止整个循环。
5x.3 return
语句
- 常用格式为:
return ; //或者 return 表达式;
- 值得注意:
- 只能用在函数体中
- 非
void
类型返回值函数,其函数体必须包含一条return
语句并带有一个对应类型的返回值。 - 返回值为
void
的函数,可以使用第一种形式的return
语句,即不带返回值。
5x.4 goto
语句
- 常用格式:
goto <标志>;
标志常用格式
<标志>:<语句>
- 作用:使程序执行流程跳转到标记的语句处
code[class*="language-"],pre[class*="language-"] { color: #333; background: none; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; text-align: left; white-space: pre; word-spacing: normal; line-height: 1.4 }
pre[class*="language-"] { padding: .8em; overflow: auto; background: #f5f5f5 }
:not(pre)>code[class*="language-"] { padding: .1em; white-space: normal; background: #f5f5f5 }
.token.comment,.token.blockquote { color: #969896 }
.token.cdata { color: #183691 }
.token.doctype,.token.punctuation,.token.variable,.token.macro.property { color: #333 }
.token.operator,.token.important,.token.keyword,.token.rule,.token.builtin { color: #a71d5d }
.token.string,.token.url,.token.regex,.token.attr-value { color: #183691 }
.token.property,.token.number,.token.boolean,.token.entity,.token.atrule,.token.constant,.token.symbol,.token.command,.token.code { color: #0086b3 }
.token.tag,.token.selector,.token.prolog { color: #63a35c }
.token.function,.token.namespace,.token.pseudo-element,.token.class,.token.class-name,.token.pseudo-class,.token.id,.token.url-reference .token.variable,.token.attr-name { color: #795da3 }
.token.entity { cursor: help }
.token.title,.token.title .token.punctuation { font-weight: bold; color: #1d3e81 }
.token.list { color: #ed6a43 }
.token.inserted { background-color: #eaffea; color: #55a532 }
.token.deleted { background-color: #ffecec; color: #bd2c00 }
.token.bold { font-weight: bold }
.token.italic { font-style: italic }
.language-json .token.property { color: #183691 }
.language-markup .token.tag .token.punctuation { color: #333 }
code.language-css,.language-css .token.function { color: #0086b3 }
.language-yaml .token.atrule { color: #63a35c }
code.language-yaml { color: #183691 }
.language-ruby .token.function { color: #333 }
.language-markdown .token.url { color: #795da3 }
.language-makefile .token.symbol { color: #795da3 }
.language-makefile .token.variable { color: #183691 }
.language-makefile .token.builtin { color: #0086b3 }
.language-bash .token.keyword { color: #0086b3 }
html body { font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif; font-size: 16px; line-height: 1.6; color: #333; background-color: #fff; overflow: initial }
html body>:first-child { margin-top: 0 }
html body h1,html body h2,html body h3,html body h4,html body h5,html body h6 { line-height: 1.2; margin-top: 1em; margin-bottom: 16px; color: #000 }
html body h1 { font-size: 2.25em; font-weight: 300; padding-bottom: .3em }
html body h2 { font-size: 1.75em; font-weight: 400; padding-bottom: .3em }
html body h3 { font-size: 1.5em; font-weight: 500 }
html body h4 { font-size: 1.25em; font-weight: 600 }
html body h5 { font-size: 1.1em; font-weight: 600 }
html body h6 { font-size: 1em; font-weight: 600 }
html body h1,html body h2,html body h3,html body h4,html body h5 { font-weight: 600 }
html body h5 { font-size: 1em }
html body h6 { color: #5c5c5c }
html body strong { color: #000 }
html body del { color: #5c5c5c }
html body a:not([href]) { color: inherit; text-decoration: none }
html body a { color: #08c; text-decoration: none }
html body a:hover { color: #00a3f5; text-decoration: none }
html body img { max-width: 100% }
html body>p { margin-top: 0; margin-bottom: 16px }
html body>ul,html body>ol { margin-bottom: 16px }
html body ul,html body ol { padding-left: 2em }
html body ul.no-list,html body ol.no-list { padding: 0; list-style-type: none }
html body ul ul,html body ul ol,html body ol ol,html body ol ul { margin-top: 0; margin-bottom: 0 }
html body li { margin-bottom: 0 }
html body li.task-list-item { list-style: none }
html body li>p { margin-top: 0; margin-bottom: 0 }
html body .task-list-item-checkbox { margin: 0 .2em .25em -1.8em; vertical-align: middle }
html body .task-list-item-checkbox:hover { cursor: pointer }
html body blockquote { margin: 16px 0; font-size: inherit; padding: 0 15px; color: #5c5c5c; border-left: 4px solid #d6d6d6 }
html body blockquote>:first-child { margin-top: 0 }
html body blockquote>:last-child { margin-bottom: 0 }
html body hr { height: 4px; margin: 32px 0; background-color: #d6d6d6; border: 0 none }
html body table { margin: 10px 0 15px 0; border-collapse: collapse; border-spacing: 0; display: block; width: 100%; overflow: auto }
html body table th { font-weight: bold; color: #000 }
html body table td,html body table th { border: 1px solid #d6d6d6; padding: 6px 13px }
html body dl { padding: 0 }
html body dl dt { padding: 0; margin-top: 16px; font-size: 1em; font-style: italic; font-weight: bold }
html body dl dd { padding: 0 16px; margin-bottom: 16px }
html body code { font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: .85em !important; color: #000; background-color: #f0f0f0; padding: .2em 0 }
html body code::before,html body code::after { letter-spacing: -0.2em; content: "?" }
html body pre>code { padding: 0; margin: 0; font-size: .85em !important; white-space: pre; background: transparent; border: 0 }
html body .highlight { margin-bottom: 16px }
html body .highlight pre,html body pre { padding: 1em; overflow: auto; font-size: .85em !important; line-height: 1.45; border: #d6d6d6 }
html body .highlight pre { margin-bottom: 0 }
html body pre code,html body pre tt { display: inline; max-width: initial; padding: 0; margin: 0; overflow: initial; line-height: inherit; background-color: transparent; border: 0 }
html body pre code::before,html body pre tt::before,html body pre code::after,html body pre tt::after { content: normal }
html body p,html body blockquote,html body ul,html body ol,html body dl,html body pre { margin-top: 0; margin-bottom: 16px }
html body kbd { color: #000; border: 1px solid #d6d6d6; border-bottom: 2px solid #c7c7c7; padding: 2px 4px; background-color: #f0f0f0 }
.markdown-preview { width: 100%; height: 100% }
.markdown-preview .pagebreak,.markdown-preview .newpage { page-break-before: always }
.markdown-preview pre.line-numbers { position: relative; padding-left: 3.8em; counter-reset: linenumber }
.markdown-preview pre.line-numbers>code { position: relative }
.markdown-preview pre.line-numbers .line-numbers-rows { position: absolute; top: 1em; font-size: 100%; left: 0; width: 3em; letter-spacing: -1px; border-right: 1px solid #999 }
.markdown-preview pre.line-numbers .line-numbers-rows>span { display: block; counter-increment: linenumber }
.markdown-preview pre.line-numbers .line-numbers-rows>span::before { content: counter(linenumber); color: #999; display: block; padding-right: .8em; text-align: right }
.markdown-preview .mathjax-exps .MathJax_Display { text-align: center !important }
.markdown-preview:not([for="preview"]) .code-chunk .btn-group { display: none }
.markdown-preview:not([for="preview"]) .code-chunk .status { display: none }
.markdown-preview:not([for="preview"]) .code-chunk .output-div { margin-bottom: 16px }
.scrollbar-style::-webkit-scrollbar { width: 8px }
.scrollbar-style::-webkit-scrollbar-track { background-color: transparent }
.scrollbar-style::-webkit-scrollbar-thumb { background-color: rgba(150,150,150,0.66); border: 4px solid rgba(150,150,150,0.66) }
html body[for="html-export"]:not([data-presentation-mode]) { position: relative; width: 100%; height: 100%; top: 0; left: 0; margin: 0; padding: 0; overflow: auto }
html body[for="html-export"]:not([data-presentation-mode]) .markdown-preview { position: relative; top: 0 }
html body[for="html-export"]:not([data-presentation-mode]) #sidebar-toc-btn { position: fixed; bottom: 8px; left: 8px; font-size: 28px; cursor: pointer; color: inherit; z-index: 99; width: 32px; text-align: center; opacity: .4 }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] #sidebar-toc-btn { opacity: 1 }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc { position: fixed; top: 0; left: 0; width: 300px; height: 100%; padding: 32px 0 48px 0; font-size: 14px; overflow: auto; background-color: inherit }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar { width: 8px }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-track { background-color: transparent }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc::-webkit-scrollbar-thumb { background-color: rgba(150,150,150,0.66); border: 4px solid rgba(150,150,150,0.66) }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc a { text-decoration: none }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul { padding: 0 1.6em; margin-top: .8em }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc li { margin-bottom: .8em }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .md-sidebar-toc ul { list-style-type: none }
html body[for="html-export"]:not([data-presentation-mode])[html-show-sidebar-toc] .markdown-preview { left: 300px; width: calc(100% - 300px); padding: 2em calc(50% - 457px - 150px); margin: 0 }
html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .markdown-preview { left: 50% }
html body[for="html-export"]:not([data-presentation-mode]):not([html-show-sidebar-toc]) .md-sidebar-toc { display: none }
.markdown-preview.markdown-preview { margin: 0 auto; font-family: "Microsoft YaHei", arial, sans-serif; color: #444444; line-height: 1; padding: 30px }
.markdown-preview.markdown-preview h1,.markdown-preview.markdown-preview h2,.markdown-preview.markdown-preview h3,.markdown-preview.markdown-preview h4 { color: #111111; font-weight: 400; margin-top: 1em }
.markdown-preview.markdown-preview h1,.markdown-preview.markdown-preview h2,.markdown-preview.markdown-preview h3,.markdown-preview.markdown-preview h4,.markdown-preview.markdown-preview h5 { font-family: "Microsoft YaHei", Palatino, serif }
.markdown-preview.markdown-preview h1,.markdown-preview.markdown-preview h2,.markdown-preview.markdown-preview h3,.markdown-preview.markdown-preview h4,.markdown-preview.markdown-preview h5,.markdown-preview.markdown-preview p,.markdown-preview.markdown-preview dl { margin-bottom: 16px; padding: 0 }
.markdown-preview.markdown-preview h1 { font-size: 48px; line-height: 54px }
.markdown-preview.markdown-preview h2 { font-size: 36px; line-height: 42px }
.markdown-preview.markdown-preview h1,.markdown-preview.markdown-preview h2 { border-bottom: 1px solid #EFEAEA; padding-bottom: 10px }
.markdown-preview.markdown-preview h3 { font-size: 24px; line-height: 30px }
.markdown-preview.markdown-preview h4 { font-size: 21px; line-height: 26px }
.markdown-preview.markdown-preview h5 { font-size: 18px; list-style: 23px }
.markdown-preview.markdown-preview a { color: #0099ff; margin: 0; padding: 0; vertical-align: baseline }
.markdown-preview.markdown-preview a:hover { text-decoration: none; color: #ff6600 }
.markdown-preview.markdown-preview a:visited { }
.markdown-preview.markdown-preview ul,.markdown-preview.markdown-preview ol { padding: 0; padding-left: 24px; margin: 0 }
.markdown-preview.markdown-preview li { line-height: 24px }
.markdown-preview.markdown-preview p,.markdown-preview.markdown-preview ul,.markdown-preview.markdown-preview ol { font-size: 16px; line-height: 24px }
.markdown-preview.markdown-preview ol ol,.markdown-preview.markdown-preview ul ol { list-style-type: lower-roman }
.markdown-preview.markdown-preview code,.markdown-preview.markdown-preview pre { background-color: #F2EFE6; color: inherit }
.markdown-preview.markdown-preview code { font-family: Consolas, Monaco, Andale Mono, monospace; margin: 0 2px }
.markdown-preview.markdown-preview pre { line-height: 1.7em; overflow: auto; padding: 6px 10px; border-left: 5px solid #6CE26C }
.markdown-preview.markdown-preview pre>code { border: 0; display: inline; max-width: initial; padding: 0; margin: 0; overflow: initial; line-height: inherit; font-size: .85em; white-space: pre; background: 0 0 }
.markdown-preview.markdown-preview code { color: #376956 }
.markdown-preview.markdown-preview code .keyword { color: #8959a8 }
.markdown-preview.markdown-preview code .number { color: #f5871f }
.markdown-preview.markdown-preview code .comment { color: #998 }
.markdown-preview.markdown-preview aside { display: block; float: right; width: 390px }
.markdown-preview.markdown-preview blockquote { border-left: .5em solid #eee; padding: 0 0 0 2em; margin-left: 0 }
.markdown-preview.markdown-preview blockquote cite { font-size: 14px; line-height: 20px; color: #bfbfbf }
.markdown-preview.markdown-preview blockquote cite::before { content: "—?" }
.markdown-preview.markdown-preview blockquote p { color: #666 }
.markdown-preview.markdown-preview hr { text-align: left; color: #999; height: 2px; padding: 0; margin: 16px 0; background-color: #e7e7e7; border: 0 none }
.markdown-preview.markdown-preview dl { padding: 0 }
.markdown-preview.markdown-preview dl dt { padding: 10px 0; margin-top: 16px; font-size: 1em; font-style: italic; font-weight: bold }
.markdown-preview.markdown-preview dl dd { padding: 0 16px; margin-bottom: 16px }
.markdown-preview.markdown-preview dd { margin-left: 0 }
.markdown-preview.markdown-preview button,.markdown-preview.markdown-preview input,.markdown-preview.markdown-preview select,.markdown-preview.markdown-preview textarea { font-size: 100%; margin: 0; vertical-align: baseline }
.markdown-preview.markdown-preview button,.markdown-preview.markdown-preview input { line-height: normal }
.markdown-preview.markdown-preview button::-moz-focus-inner,
.markdown-preview.markdown-preview input::-moz-focus-inner { border: 0; padding: 0 }
.markdown-preview.markdown-preview button,.markdown-preview.markdown-preview input[type="button"],.markdown-preview.markdown-preview input[type="reset"],.markdown-preview.markdown-preview input[type="submit"] { cursor: pointer }
.markdown-preview.markdown-preview input[type="checkbox"],.markdown-preview.markdown-preview input[type="radio"] { cursor: pointer }
.markdown-preview.markdown-preview input:not([type="image"]),.markdown-preview.markdown-preview textarea { }
.markdown-preview.markdown-preview input[type="search"] { }
.markdown-preview.markdown-preview input[type="search"]::-webkit-search-decoration { }
.markdown-preview.markdown-preview label,.markdown-preview.markdown-preview input,.markdown-preview.markdown-preview select,.markdown-preview.markdown-preview textarea { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 13px; font-weight: normal; line-height: normal; margin-bottom: 18px }
.markdown-preview.markdown-preview input[type="checkbox"],.markdown-preview.markdown-preview input[type="radio"] { cursor: pointer; margin-bottom: 0 }
.markdown-preview.markdown-preview input[type="text"],.markdown-preview.markdown-preview input[type="password"],.markdown-preview.markdown-preview textarea,.markdown-preview.markdown-preview select { display: inline-block; width: 210px; padding: 4px; font-size: 13px; font-weight: normal; line-height: 18px; height: 18px; color: #808080; border: 1px solid #ccc }
.markdown-preview.markdown-preview select,.markdown-preview.markdown-preview input[type="file"] { height: 27px; line-height: 27px }
.markdown-preview.markdown-preview textarea { height: auto }
.markdown-preview.markdown-preview :-moz-placeholder { color: #bfbfbf }
.markdown-preview.markdown-preview ::-webkit-input-placeholder { color: #bfbfbf }
.markdown-preview.markdown-preview input[type="text"],.markdown-preview.markdown-preview input[type="password"],.markdown-preview.markdown-preview select,.markdown-preview.markdown-preview textarea { }
.markdown-preview.markdown-preview input[type="text"]:focus,.markdown-preview.markdown-preview input[type="password"]:focus,.markdown-preview.markdown-preview textarea:focus { outline: none; border-color: rgba(82, 168, 236, 0.8) }
.markdown-preview.markdown-preview button { display: inline-block; padding: 4px 14px; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 13px; line-height: 18px; background-color: #0064cd; background-repeat: repeat-x; background-image: linear-gradient(top, #049cdb, #0064cd); color: #fff; border: 1px solid #004b9a; border-bottom-color: #003f81; border-color: rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.1) rgba(0, 0, 0, 0.25) }
.markdown-preview.markdown-preview button:hover { color: #fff; background-position: 0 -15px; text-decoration: none }
.markdown-preview.markdown-preview button:active { }
.markdown-preview.markdown-preview button::-moz-focus-inner { padding: 0; border: 0 }
.markdown-preview.markdown-preview table { border-spacing: 0; width: 100% }
.markdown-preview.markdown-preview table { border: solid #ccc 1px }
.markdown-preview.markdown-preview table tr:hover { background: #fbf8e9 }
.markdown-preview.markdown-preview table td,.markdown-preview.markdown-preview .table th { border-left: 1px solid #ccc; border-top: 1px solid #ccc; padding: 10px; text-align: left }
.markdown-preview.markdown-preview table th { background-color: #dce9f9; background-image: linear-gradient(top, #ebf3fc, #dce9f9); border-top: none; padding: 5px }
.markdown-preview.markdown-preview table td:first-child,.markdown-preview.markdown-preview table th:first-child { border-left: none }
.markdown-preview.markdown-preview table th:first-child { }
.markdown-preview.markdown-preview table th:last-child { }
.markdown-preview.markdown-preview table th:only-child { }
.markdown-preview.markdown-preview table tr:last-child td:first-child { }
.markdown-preview.markdown-preview table tr:last-child td:last-child { }
原文地址:https://www.cnblogs.com/Earl-Jones/p/8612033.html