摘要: #include<iostream>usingnamespace std;int main(){ int i=4,j=1; do { cout<<"i="<<i<<endl; i--; }while(i!=0);//先执行循环,再判断条件,决定是否继续循环。 do++j; while(j>999);//注意,虽然此时条件为假,但是仍然执行了循环。 cout<<"j="<<j<<endl; return0;}/*do-while循环: 当只有一条循环语句时,用:do 阅读全文
posted @ 2012-06-23 18:57 蚂蚁踩死了大象 阅读(227) 评论(0) 推荐(0)
摘要: /*主要内容: 1.单个变量的测试表达式。 2.数值求和。 3.循环中使用break的作用。 4.避免创建无限循环。 5.提前终止循环。*/#include<iostream>usingnamespace std;int main(){ int i=4,k=1,sum=0; while(i) { cout<<"old i="<<i; sum+=i; i--; cout<<",new i="<<i<<",sum="<<sum<<endl; } 阅读全文
posted @ 2012-06-23 18:28 蚂蚁踩死了大象 阅读(142) 评论(0) 推荐(0)
摘要: //使用while循环。#include<iostream>usingnamespace std;int main(){ int i; i=1; while(i<=5)//满足循环条件就进入循环。当不满足循环条件时,循环终止。 { cout<<"Loop number"<<i<<"in the while loop\n"; i++;//改变i的值,使得循环条件情况改变,不会成为死循环。 } return0;}/*while循环:基本结构为: while(expression) { state... 阅读全文
posted @ 2012-06-23 18:15 蚂蚁踩死了大象 阅读(208) 评论(0) 推荐(0)
摘要: /*1.bool数据类型的使用。 2.bool数据的输入和输出。*/#include<iostream>#include<fstream>#include<iomanip>usingnamespace std;int main(){ bool salty,hard,acidic,good_taste,have_service; double sodium,Ca,Mg,pH; ifstream infile("A.txt"); infile>>sodium>>Ca>>Mg>>pH; salty 阅读全文
posted @ 2012-06-23 16:56 蚂蚁踩死了大象 阅读(524) 评论(0) 推荐(0)
摘要: /*1.if-else-if控制结构的使用方法。 2.switch语句的使用方法。 3.比较if-else-if控制结构和switch控制结构。*/#include<iostream>usingnamespace std;int miain(){ int option; cout<<"Please type 1,2,or 3\n"; cin>>option; if(option==1) { cout<<"Attend meeting\n"; } elseif(option==2) { cout<< 阅读全文
posted @ 2012-06-23 16:18 蚂蚁踩死了大象 阅读(361) 评论(0) 推荐(0)
摘要: /*1.逻辑运算符的优先次序。 2.逻辑表达式的求解方法。*/#include<iostream>usingnamespace std;int main(){ int a=4,b=-2,c=0,x; if(a) cout<<"a="<<a<<",!a="<<!a<<endl;//一般非零值都是真,例如c=0,则c为假。输出else. if(b) cout<<"b="<<b<<",!b="<<!b& 阅读全文
posted @ 2012-06-23 15:32 蚂蚁踩死了大象 阅读(144) 评论(0) 推荐(0)
摘要: /*1.逻辑运算符的使用。 2.逻辑表达式的使用。*/#include<iostream>usingnamespace std;int main(){ int x=5,y=0; cout<<"x="<<x<<",y="<<y<<endl; if(x>0&&y>0)//使用了运算符'与'=&&. cout<<"x is greater than 0 and" "y is greater 阅读全文
posted @ 2012-06-22 21:35 蚂蚁踩死了大象 阅读(113) 评论(0) 推荐(0)
摘要: //嵌套的if-else控制结构#include<iostream>usingnamespace std;int main(){ int day; double time; cout<<"Type the day and time of interest"<<endl; cin>>day>>time; if(day<=5) { if(time<=9.00) cout<<"Drive piles"<<endl; else cout<<"Co 阅读全文
posted @ 2012-06-22 21:03 蚂蚁踩死了大象 阅读(149) 评论(0) 推荐(0)
摘要: /*1.简单的if-else控制结构2.条件运算符"?:"*/#include<cmath>#include<iostream>#include<iomanip>usingnamespace std;int main(){ double revenue=0,expenses=0,profit=0,loss=0,interest=0; cout<<setprecision(2)<<setiosflags(ios::showpoint); cout<<"Enter the company' 阅读全文
posted @ 2012-06-22 20:26 蚂蚁踩死了大象 阅读(133) 评论(0) 推荐(0)
摘要: /*1.简单的if语句。 2.if语句块。 3.控制程序流。 4.关系运算符 5.关系表达式。*/#include<iostream>usingnamespace std;int main(){ int pcode_entered; constint pcode=8765; cout<<"Enter your pass code."<<endl; cin>>pcode_entered; if(pcode_entered<pcode) cout<<"Incorrect code" " 阅读全文
posted @ 2012-06-22 19:34 蚂蚁踩死了大象 阅读(148) 评论(0) 推荐(0)