这是第三个了,前两个在这,有兴趣可以自己去看:
这次要讲的是——
分支语句的应用
- 关系运算符
- 单分支语句
- 双分支语句
- 多分支语句
关系运算符
符号 | 含义 |
---|---|
> | 大于 |
< | 小于 |
== | 等于 |
!= | 不等于 |
>= | 大等于 |
<= | 小等于 |
首先,单分支语句就是只有if与条件的if语句
#include < iostream >
using namespace std;
int main ( void ) {
if (条件) {
cout<<1;
}
return 0;
}
此处,条件如果成立,那么执行"cout<<1;"
双分支语句
#include < iostream >
using namespace std;
int main ( void ) {
if ( 条件 ) {
cout<<"true";
}
else {
cout<<"false";
}
return 0;
}
else是指当if()的条件不成立时会执行的事件
(else 和 if 内的语句只会执行其中的一个)
多分支语句
#include <iostream>
using namespace std;
int main ( void ) {
if ( 条件1 ) {
cout<<1;
}
else if ( 条件二 ) {
cout<<2;
}
......
else {
cout<<10;
}
return 0;
}
使用 else if ( ) 与 else 一样,只会执行其中的一个
而分支嵌套是另一个东西,分支嵌套是在分支语句内再放一个分支语句
#include <iostream>
using namespace std;
int main ( void ) {
if ( 条件 ) {
if( 条件1 ) {
cout<<1;
}
else {
cout<<2;
}
}
return 0;
}
它只有外部条件成立才会执行内部语句
@刘子睿 这是代码框架(上一个好像有点问题)
#include <bits/stdc++.h>
using namespace std ;
const int N = 1e5 + 5 ; // n的大小以题目中的为准
struct Node {
int score ;
int /*string*/ id;// 当id是字符时用string
int x ;// 坐标x、y
int y ;
} node [ N ] ;
bool cmp ( Node a , Node b ) {
if ( a.score == b.score ) {
return a.id < b.id ;
}
else {
return a.score < b.score ;
}
if ( a.x == b.x ) {
return a.x < b.x ;
}
else {
return a.y < b.y ;
}
}
int main ( void ) {
return 0;
}