一旦你對程序進行了改動,就要對改動的部分進行測試!無論改動多麼簡單,必定要對它進行測試!ios
不要試圖在寫代碼的過程當中設計程序,把須要作的東西寫在紙上。c++
不要依靠編譯器來保證代碼的正確性,要理解所寫的每一行代碼ide
字節:計算機內存的基本單元。比特,也叫位,擁有兩種狀態的數據單元,好比0或1,開或關。函數
1byte=8bit測試
前綴和後綴自增和自減運算符:注意:i值 是6,spa
c++中數據顯示位數的控制:設計
#include<iostream> #include<iomanip>
using namespace std; int main() { double f = 333233.1415926535; float aa = 12.25678; float bb = 0.123456789; cout << f << endl; cout << aa << endl; cout << bb << endl; cout.setf(ios::fixed | ios::showpoint); cout.precision(3); // cout << f << endl; //輸出3位小數,3.142
cout << aa << endl; cin.get(); return 0; }
顯示結果:code
333233
12.2568
0.123457
333233.142
12.257blog
c++控制語句:隊列
c++中if語句:(4種)
1.if-else語句
if (condition)
{
//條件爲真時執行
}
else
{
//條件爲假時執行
}
2.if-else if-else語句
if (condition1)
{
//屬於條件1的語句
}
else if(condition 2)
{
//屬於條件2的語句
}
else
{
//屬於條件3的語句
}
注意,else語句不必定是必須的,也就是說:if或if--else if或者if-else if -else if....-else也是容許的;可是隻能有一個else語句。
3.嵌套if-else語句
示例以下:
if (condition) //第一個條件
{
if (another condition) //這裏是嵌套的if 語句
{
//語句
}
//更多的語句
}
4.條件運算符
int windspeed;
bool bHurricane;
if (winspeed>75)
bHurricane=true;
else
bHurricane=false;
經過條件 運算符「?」只經過一行代碼就能完成上述檢查與操做賦值:
int windspeed;
bool bHurricane;
bHurricane = windspeed>75? true : false
注意,運算符「?」回影響代碼的 易讀性,初學者不建議使用
c++中switch語句;
switch(此處是變量)
{
case value1:
//語句塊1
break; //break不要忘記加上
case value2: case value3:(表示2或者3狀況下)
//語句塊2
break;
default: //缺省狀況下
//語句n
}
c++中的循環有三種:
for 循環:當清楚循環體需重複執行的次數時,多使用之
示例:輸出字母表
結果:
#include<iostream> #include<iomanip> //爲了使用setw using namespace std; int main() { int letter_ctr, i; cout << "we are going to write our ABCs" << endl; letter_ctr = 0; for (i = 65; i < 91; ++i) //A=65,z=90 { //將整數轉化爲字符 cout << setw(6) << static_cast<char>(i); letter_ctr ++; if (letter_ctr == 7) { cout << endl; letter_ctr = 0; } } cout << "\n there are our ABCs"; cin.get(); return 0; }
while循環:可用於在不能事先肯定循環次數的狀況下對循環進行控制。
while(condition)
{
//這些語句在條件爲真時被執行
}
do while循環:與while循環相似,可是在循環體執行結束檢查循環的條件
do
{
//循環語句體
} while (condition);
cin.ignore()函數:
用於回車符的刪除,
有時在cin>>後面跟有getline(),cin會將回車符留在輸入隊列中,須要將回車符移除才能正確賦值給變量
int aa; string bb; cout << "how many do you have?" << endl; cin >> aa; cout << aa << endl;; cin.ignore(); // 加上這個getlIne才能正確賦值,遇到回車符會中止 cout << "the color is ???" << endl; getline(cin,bb); cout << bb;