菜卵加油敲代码
下面是今天做的几个题
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char *argv[]) {
//test1 max
int n1 = 12;
int n2 = 30;
int n3 = -43;
string maxSt=(n1>n2)?((n1>n3)?"n1":"n3"):((n2>n3)?"n2":"n3");
cout<<maxSt;
//test2遍历100内偶数(含)
//wrongAnswer!!
/* for(int i=1;i%2==0&&i<=100;i++){
cout<<i<<endl;
}
//这写得什么鬼东西嘛!!气死
*/
//这个才是对的,就简单判断就好了
/* for(int i=1;i<=100;i++){
if(i%2==0){
cout<<i<<endl;
}
}
*/
//test3
//first try,!!!菜卵警告!!!
/*
for(int i=1;i<=150;i++){
if(i%3==0){
cout<<endl<<i<<"foo";
if(i%5==0){
cout<<"biz";
if(i%7==0){
cout<<"baz"<<endl;
}}}
else if(i%5==0){
cout<<endl<<i<<"biz";
if(i%7==0){
cout<<"baz"<<endl;
}
}
else if(i%7==0){
cout<<endl<<i<<"baz"<<endl;
}
else{
cout<<endl<<i<<endl;
}
}*/
//很好,我是菜卵,我写了一大堆,换行还不行
//其实用三个并列的if就可以执行了
for(int i=1;i<=150;i++){
cout<<i<<" ";
if(i%3==0){
cout<<"foo"<<" ";
}
if(i%5==0){
cout<<"biz"<<" ";
}
if(i%7==0){
cout<<"baz"<<" ";
}
cout<<endl;
}
}
因为上面的例子,要求输出是这样的:

总结一下子:
if语句的3种格式:
1.单独if ,可并列使用,如上面的例子
if(条件表达式){
执行代码块;
}
2.if- else
if(条件表达式){//true
执行代码块1;
}
else{//false
执行代码块2;
}
3.if- else if -…-else
if(条件表达式1){
执行代码块1;
}
else if(条件表达式2){
执行代码块2;
}
else if(条件表达式3){
执行代码块3;
}
...
else{//都不满足
执行代码块n;
}
for循环:
为啥要写英文?怕到时候去读书不认识啥意思。原本就敲得不好= =
1初始化部分 init-statement
2循环条件部分 test-exp ----->boolean型
3循环体部分 body-statement
4迭代部分 alter-statement
for(1;2;4){
3;
}
1-2-3-4- - -2-3-4- - -…- - -2-3-4
switch循环:
表达式只能是byte, short, char, int, 枚举, String
一旦匹配成功,
进入相应case,调用其执行语句,一直执行到break
明天继续做题
本文记录了一名程序员在实践中学习C++,通过实例演示了if、else和elseif的三种格式,并优化了for循环和switch语句的编写技巧。
1143

被折叠的 条评论
为什么被折叠?



