#include <iostream>
using namespace std;
int main()
{
}
CODE |
MEANING |
SAMPLE |
|---|---|---|
|
cout |
แสดงผลหน้าจอ |
cout << "abc"; |
|
\n |
ขึ้นบรรทัดใหม่ |
hello world! |
|
\t |
tab ข้อความ |
hello plug |
|
<<endl |
ขึ้นบรรทัดใหม่ |
cout << "abc" <<endl; |
|
// |
comment บรรทัดเดียว |
// this is comment |
|
/* */ |
comments หลายบรรทัด |
/* hello |
|
int |
ประกาศตัวแปรจำนวนเต็ม |
int num = 4; |
|
double |
ประกาศตัวแปรทศนิยม |
double num = 4.00; |
|
string |
ประกาศตัวแปรอักขระ |
string num = "123"; |
|
ต่อ string |
string str1 = "plug"; |
|
|
.append() |
ต่อ string หรือเพิ่ม string |
string name = "plug"; |
|
.size() |
ดูตัวอักษรใน string |
string str1 = "phuwadon"; |
|
str1[0] |
เข้าถึง string แต่ละตำแหน่ง |
string str1 = "phuwadon"; |
|
str1[0] = 'P'; |
เปลี่ยน string ในตำแหน่งที่กำหนด |
string str1 = "phuwadon"; |
|
char |
ประกาศตัวแปรอักขระตัวเดียว |
char grade = 'A'; |
|
bool |
ประกาศตัวแปรอักขระ logic |
bool win = true; |
|
const |
ประกาศตัวแปรค่าคงที่ |
const int num = 4; |
|
cin |
user input |
cin >> num; |
|
getline(cin,...) |
input string ที่ tap ได้ |
string fullname; getline(cin,fullname); cout << fullname; |
|
+,-,*,/,% |
operators |
int num1 = 2 + 2; |
|
++ |
เพิ่มค่าลดค่าที่ละ1 |
cout << ++n; |
|
+= |
บวกเพิ่มให้ตัวแปร |
int n1 = 4; |
|
== |
เท่ากับ มากกว่า น้อยกว่า มากกว่าหรือเท่ากับ น้อยกว่าหรือเท่ากับ |
n1 == n2; n1 != n2; n1 > n2; n1 < n2; n1 >= n2; n1 <= n2; |
|
&& || ! |
and or not |
n1 && n2; n1 || n2; n1 not n2; |
|
max() |
หาค่าสูงสุด |
cout << max(5,10); |
|
sqrt() round() log() |
ต้อง #include <cmath> สแควร์รูท ปัดเศษตัวเลข ลอการิทึมธรรมชาติ |
cout << sqrt(64); cout << round(2.6); cout << log(2); |
|
if |
เช็คเงื่อนไขลำดับแรก |
int x = 0; if(x < 85) { cout << "if"; } |
|
else if |
เช็คเงื่อนต่อจาก if |
int x = 0; if(x > 85) { cout << "if"; } else if(x < 85) { cout << "else if"; } |
|
else |
เช็คเงื่อนต่อจาก else if |
int x = 0; if(x > 85) { "cout << "if"; } else if(x == 85) { cout << "else if"; } else() { cout << "else"; } |
|
for |
loop จำนวนแน่นอน |
for (int i = 0; i <= 5; i++) { cout << "grade 4\n" |
|
while |
loop ไม่แน่นอน |
int i = 0; while (i < 10) { cout << "grade 4\n"; i++; |
|
break |
ใช้หยุดโปรแกรมถ้าอยู่ใน loop จะกระโดดออกไปทำ funktion main |
int i = 1; for (i; i <= 10; i++) { if (i == 4) { break; } cout << "grade 4\n"; cout << "grade 4"; } |
|
continue |
ข้ามการทำงาน |
int g = 1; for (g; g <= 4; g++) { if (g == 1) { continue; } else if (g == 2) { continue; } else if (g == 3) { continue; } cout << "grade " << g << endl; } |
|
"Arrays variable" |
string pets[3] = {"cat","dog","tigle"}; int num[3] = {10,20,30}; |
|
|
เข้าถึงตำแหน่งข้อมูลใน arrays |
string g[2] = {"3","4"}; cout << "grade " << g[1]; |
|
|
เปลี่ยนแปลงข้อมูลใน arrays |
int num[2] = {3,4}; num[0] = 4; cout << num[0]; |
|
|
sizeof() |
ตรวจสอบประเภทข้อมูล output in bytes |
int a = 1; cout << sizeof(a); |
|
"Function" สร้าง block การทำงานของโปรแกรม จะทำงานเมื่อถูกเรียกใช้ |
cout << Sum(5); int Sum(int a) { total = a + 5; return(total) } Disp(); void Disp() { cout << "hello c++"; } int a[] = {2,4,6}; array(a); for(int i = 0 ; i < 3 ; i++){ cout << a[i]; } int array(int A[]) { for(int i = 0 ; i < 3 ; i++){ A[i] *= 2; } |
|
|
struct |
จัดกลุ่มตัวแปรที่เกี่ยวข้องหลายตัวไว้ในที่เดียว ตัวแปรแต่ละตัวในโครงสร้างเรียกว่าสมาชิกของโครงสร้าง |
struct car { string brand; string model; int year; }; car myCar1; myCar1.brand = "BMW"; myCar1.model = "X5"; myCar1.year = 1999; cout << myCar1.brand; |