欢迎光临
我们一直在努力

学C++第二天_逻辑运算

if语句嵌套:

示例代码

#include<iostream>

using namespace std;

int main() {
int weight1 = 0, weight2 = 0, weight3 = 0;

cout << "请输入A的体重" << endl;
cin >> weight1;
cout << "请输入B的体重" << endl;
cin >> weight2;
cout << "请输入C的体重" << endl;
cin >> weight3;

if (weight1 > weight2) {
if (weight1 > weight3) {
cout << "A最重" << endl;
}
else
cout << "C最重" << endl;
}
else {
if (weight2 > weight3) {
cout << "B最重" << endl;
}
else
cout << "C最重" << endl;
}
}


三目运算法 :                           

 ?   :   

示例问号前放置条件,条件为真,执行第一个冒号后的语句;条件为假,执行第二个冒号后的语句

示例代码:

#include<iostream>

using namespace std;

int main() {
int a, b, c;

//取a、b中更大的值赋值给c
cout << "请输入a" << endl;
cin >> a;
cout << "请输入b" << endl;
cin >> b;
a > b ? c = a : c = b;
cout<<"c的值为:"<<c<<endl;

//三目运算符中返回的数是变量,可以继续赋值
(a > b ? a : b) = 10;
cout << "a:" << a <<" "<<"b:" << b << endl;

return 0;
}


switch语句:

switch(表达式){

case 结果一:执行语句;break;

case 结果二:执行语句;break;

case 结果三:执行语句;break;

…

default:执行语句;break;

}

示例代码:

#include<iostream>

using namespace std;

int main() {
int score;
cout << "请输入电影分数:" << endl;
cin >> score;
switch (score) {
case 10:cout << "神作" << endl;
break;
case 9:cout << "经典" << endl;
break;
case 8:cout << "很好" << endl;
break;
case 7:cout << "很好" << endl;
break;
case 6:cout << "一般" << endl;
break;
case 5:cout << "不好" << endl;
break;
case 4:cout << "不好" << endl;
break;
case 3:cout << "差劲" << endl;
break;
case 2:cout << "差劲" << endl;
break;
case 1:cout << "差劲" << endl;
break;
default:cout << "差劲" << endl;
break;
}
return 0;
}


while循环:

示例代码:

#include<iostream>

using namespace std;

int main() {
//生成0~9数字
int num=0;
while (num < 10) {
cout << num << endl;
num++;
}
//体验无限循环
int light = 1;
while (1) {
cout << light << endl;
light = -light;
}
return 0;
}

例题:猜数字游戏

#include<iostream>

using namespace std;

int main() {
//利用系统时间生成随机数,防止每次生成的随机数都相同
srand((unsigned int)time(NULL));

int num = rand() % 100 + 1;

while (1) {
int guss;
cout << "请输入猜测数:";
cin >> guss;
if (guss > num) {
cout << "过大" << endl;
}
else if (guss < num) {
cout << "过小" << endl;
}
else {
cout << "猜对了!" << endl;
break;
}
}
return 0;
}


do…while语句:

先执行do后面的语句,后判断while里的条件

示例代码:

#include<iostream>

using namespace std;

int main() {
int num = 0;
do {
cout << num << " ";
num++;
} while (num < 10);

return 0;
}

例题:100~999内的水仙花数

#include<iostream>

using namespace std;

int main() {
int num = 100;

do {
int a = 0;
int b = 0;
int c = 0;
a = num % 10;
b = num / 10 % 10;
c = num / 100;
if (a * a * a + b * b * b + c * c * c == num) {
cout << num << endl;
}
num++;
} while (num < 1000);
return 0;
}


for循环:

for(起始表达式;条件表达式;末尾循环体){循环语句}

例题:1~100报数,报到7的倍数或十位、个位含7的数字则敲桌子

#include<iostream>

using namespace std;

int main() {
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) {
cout << "敲桌子" << endl;
}
else
cout << i << endl;
}
return 0;
}


嵌套循环:

例题:打印星形图

#include<iostream>

using namespace std;

int main() {
for (int i = 1; i <=10; i++) {
for (int j = 1; j <= 10; j++) {
cout << "*"<<" ";
}
cout << " " << endl;
}
return 0;
}

例题:打印九九乘法表

#include<iostream>

using namespace std;

int main() {
for (int i = 1; i <10; i++) {
for (int j = 1; j <= i; j++) {
cout << j<<"*"<<i<<"=" << j * i << " ";
}
cout << endl;
}
return 0;
}


跳转语句:

break:跳出循环

continue:执行下一次循环

goto:跳转到标记处    …goto Flag…Flag…

赞(0)
未经允许不得转载:171主机测评 » 学C++第二天_逻辑运算
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址