വേരിയബിളുകൾ#
ആമുഖം#
വേരിയബിളുകൾ ഡാറ്റ സംഭരിക്കുകയും നിങ്ങളുടെ പ്രോഗ്രാമിലുടനീളം അത് വീണ്ടും ഉപയോഗിക്കാനും കൈകാര്യം ചെയ്യാനും നിങ്ങളെ അനുവദിക്കുന്നു. C++ ഒരു സ്റ്റാറ്റിക്കലി ടൈപ്പ് ചെയ്ത ഭാഷയാണ്, അതായത് നിങ്ങൾ ഒരു വേരിയബിൾ സൃഷ്ടിക്കുമ്പോൾ അതിന്റെ തരം വ്യക്തമായി പ്രഖ്യാപിക്കണം. ഡിക്ലറേഷന് ശേഷം തരം മാറ്റാൻ കഴിയില്ല. ഉദാഹരണത്തിന്:
int angle = 90; // angle is an integer
char dist[] = "Distance"; // dist is an array of characters
double steps = 2.5; // steps is a double
C++ ഉം ശക്തമായി ടൈപ്പ് ചെയ്തിരിക്കുന്നു, അതായത് വ്യക്തമായ പരിവർത്തനം കൂടാതെ നിങ്ങൾക്ക് പൊരുത്തപ്പെടാത്ത തരങ്ങളിൽ പ്രവർത്തനങ്ങൾ നടത്താൻ കഴിയില്ല. ഉദാഹരണത്തിന്:
int blocks = 2; // blocks is an integer
char rings[] = "4"; // rings is a string
int result = blocks + rings; // Creates a compilation error
ഈ API C++ ലെ സാധാരണ വേരിയബിൾ തരങ്ങളെ വിശദീകരിക്കുന്നു. ഒരു സമഗ്രമായ പട്ടികയല്ലെങ്കിലും, പ്രായോഗികമായി നിങ്ങൾ ഏറ്റവും കൂടുതൽ ഉപയോഗിക്കാൻ സാധ്യതയുള്ള തരങ്ങളെ ഇത് ഉൾക്കൊള്ളുന്നു.
ലോക്കൽ വേരിയബിളുകൾ — ഒരു ഫംഗ്ഷനുള്ളിൽ പ്രഖ്യാപിക്കുകയും ആ പരിധിക്കുള്ളിൽ മാത്രം ഉപയോഗിക്കുകയും ചെയ്യുന്നു; താൽക്കാലിക അല്ലെങ്കിൽ ഒറ്റപ്പെട്ട മൂല്യങ്ങൾക്ക് ഏറ്റവും അനുയോജ്യം.
ഗ്ലോബൽ വേരിയബിളുകൾ — ഏതൊരു ഫംഗ്ഷനും പുറത്ത് പ്രഖ്യാപിക്കുകയും പ്രോജക്റ്റിലുടനീളം ഉപയോഗിക്കുകയും ചെയ്യുന്നു; ഫംഗ്ഷനുകൾക്കിടയിൽ ഡാറ്റ പങ്കിടുന്നതിന് നല്ലതാണ്.
പൂർണ്ണസംഖ്യ — എണ്ണുന്നതിനോ, ദൂരങ്ങൾക്കോ, അല്ലെങ്കിൽ ദശാംശങ്ങളില്ലാതെ മറ്റെന്തെങ്കിലുമോ ഉപയോഗിക്കുന്ന പൂർണ്ണ സംഖ്യകൾ.
ഇരട്ട — കൃത്യത ആവശ്യമുള്ള ഗണിതശാസ്ത്ര കണക്കുകൂട്ടലുകൾക്കുള്ള ഉയർന്ന കൃത്യതയുള്ള ദശാംശ സംഖ്യകൾ.
ഫ്ലോട്ട് — അളവുകൾക്കോ കണക്കുകൂട്ടലുകൾക്കോ ഉപയോഗപ്രദമായ സ്റ്റാൻഡേർഡ്-പ്രിസിഷൻ ഡെസിമൽ സംഖ്യകൾ.
C-ശൈലി സ്ട്രിംഗ് — സന്ദേശങ്ങൾ, ലേബലുകൾ അല്ലെങ്കിൽ വായിക്കാവുന്ന ഔട്ട്പുട്ട് പ്രദർശിപ്പിക്കുന്നതിന് ഉപയോഗിക്കുന്ന വാചക മൂല്യങ്ങൾ.
ബൂളിയൻ (ബൂൾ) — യുക്തിക്കും തീരുമാനമെടുക്കലിനും
trueഅല്ലെങ്കിൽfalseമൂല്യങ്ങൾ.അറേകൾ — ഒരേ തരത്തിലുള്ള ഘടകങ്ങളുടെ നിശ്ചിത വലുപ്പ ശേഖരങ്ങൾ.
2D അറേകൾ — അറേകളുടെ അറേകൾ; വരികൾ, ഗ്രിഡുകൾ അല്ലെങ്കിൽ പട്ടിക പോലുള്ള ഡാറ്റയെ പ്രതിനിധീകരിക്കുന്നതിന് അനുയോജ്യം.
Const —
constഉപയോഗിച്ച് പ്രഖ്യാപിച്ച ഒരു വേരിയബിൾ പരിഷ്കരിക്കാൻ കഴിയില്ല.
ഒരു വേരിയബിൾ പ്രഖ്യാപിക്കുകയും നിയോഗിക്കുകയും ചെയ്യുന്നു#
C++ ൽ ഒരു വേരിയബിൾ സൃഷ്ടിക്കാൻ, നിങ്ങൾ അതിന്റെ തരം പ്രഖ്യാപിക്കുകയും തുടർന്ന് വേരിയബിൾ നാമം നൽകുകയും വേണം, തുടർന്ന് ഓപ്ഷണലായി = ഓപ്പറേറ്റർ ഉപയോഗിച്ച് ഒരു മൂല്യം നൽകുക:
int distance = 100;
ഒരു വേരിയബിളിന് പേരിടുമ്പോൾ, ഇനിപ്പറയുന്ന നിയമങ്ങൾ പാലിക്കേണ്ടതുണ്ട്:
പേരിൽ പ്രത്യേക പ്രതീകങ്ങൾ (ഉദാഹരണത്തിന്, ഒരു ആശ്ചര്യചിഹ്നം) അടങ്ങിയിരിക്കരുത്.
പേര് ഒരു സംഖ്യയിൽ തുടങ്ങാൻ പാടില്ല.
പേരിൽ സ്പെയ്സുകൾ ഉപയോഗിക്കാൻ കഴിയില്ല.
മുൻകൂട്ടി നിശ്ചയിച്ച VEXcode ഒബ്ജക്റ്റുകളുമായി പേരിന് വൈരുദ്ധ്യമുണ്ടാകരുത് (ഉദാഹരണത്തിന്,
Drivetrain).പേര് ഒരു C++ റിസർവ്ഡ് കീവേഡ് ആകരുത് (ഉദാ:
int,for, മുതലായവ).വേരിയബിൾ പേരുകൾ കേസ് സെൻസിറ്റീവ് ആണ് (ഉദാ:
Distanceഉംdistanceഉം വ്യത്യസ്ത വേരിയബിളുകളാണ്).
Local Variables#
ലോക്കൽ വേരിയബിളുകൾ ഒരു ഫംഗ്ഷന്റെയോ കോഡിന്റെയോ ബ്ലോക്കിനുള്ളിൽ *നിർവചിച്ചിരിക്കുന്നു. അവ ആ ഫംഗ്ഷന്റെയോ ബ്ലോക്കിന്റെയോ പരിധിക്കുള്ളിൽ മാത്രമേ ആക്സസ് ചെയ്യാൻ കഴിയൂ, അതിന് പുറത്ത് ദൃശ്യമാകില്ല.
ഫംഗ്ഷൻ നിർവചിക്കുക (
int main()ന് പുറത്ത്)void show_local() { // This variable only exists inside this function int angle = 90; Brain.Screen.print("%d", angle); }
int main()നുള്ളിലെ ഫംഗ്ഷൻ വിളിക്കുക.int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code show_local(); }
ഒരു പ്രത്യേക ഫംഗ്ഷനിലോ പ്രോഗ്രാമിന്റെ ഭാഗത്തിലോ മാത്രം പ്രസക്തമായ താൽക്കാലിക മൂല്യങ്ങൾ സംഭരിക്കുന്നതിനാണ് ലോക്കൽ വേരിയബിളുകൾ സാധാരണയായി ഉപയോഗിക്കുന്നത്.
Global Variables#
ഗ്ലോബൽ വേരിയബിളുകൾ ഏതൊരു ഫംഗ്ഷന്റെയോ ബ്ലോക്കിന്റെയോ പുറത്ത് നിർവചിച്ചിരിക്കുന്നു. ഫംഗ്ഷനുകൾക്കുള്ളിൽ ഉൾപ്പെടെ, പ്രോഗ്രാമിൽ എവിടെയും അവ ആക്സസ് ചെയ്യാനും വായിക്കാനും കഴിയും.
കുറിപ്പ്: ഗ്ലോബൽ വേരിയബിളുകൾ പ്രോഗ്രാമിൽ എവിടെ നിന്നും ആക്സസ് ചെയ്യാൻ കഴിയും, ഇത് ഫംഗ്ഷനുകളിലുടനീളം ഡാറ്റ പങ്കിടുന്നതിന് അവ സൗകര്യപ്രദമാക്കുന്നു. എന്നിരുന്നാലും, ഗ്ലോബൽ വേരിയബിളുകളെ വളരെയധികം ആശ്രയിക്കുന്നത് അപ്രതീക്ഷിത പാർശ്വഫലങ്ങൾക്ക് കാരണമാകും, കാരണം പ്രോഗ്രാമിന്റെ ഒരു ഭാഗത്തെ വേരിയബിളിലെ മാറ്റങ്ങൾ മറ്റ് ഭാഗങ്ങളെ പ്രവചനാതീതമായി ബാധിച്ചേക്കാം. ഇക്കാരണത്താൽ, സാധ്യമാകുമ്പോൾ ലോക്കൽ വേരിയബിളുകൾ സാധാരണയായി തിരഞ്ഞെടുക്കപ്പെടുന്നു, കാരണം അവ ഒരു വേരിയബിളിന്റെ വ്യാപ്തി അത് നിർവചിച്ചിരിക്കുന്ന നിർദ്ദിഷ്ട ഫംഗ്ഷനിലേക്ക് പരിമിതപ്പെടുത്തുന്നു. ഇത് വൈരുദ്ധ്യങ്ങളുടെ സാധ്യത കുറയ്ക്കുകയും ഡീബഗ്ഗിംഗ് എളുപ്പമാക്കുകയും ചെയ്യുന്നു.
ഫംഗ്ഷൻ നിർവചിക്കുക (
int main()ന് പുറത്ത്)// The variable is defined outside a function int angle = 90; void show_global() { // You can access 'angle' inside a function Brain.Screen.print("%d", angle); Brain.Screen.newLine(); }
int main()നുള്ളിലെ ഫംഗ്ഷൻ വിളിക്കുക.int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code show_global(); Brain.Screen.print("%d", angle); }
സി+±ൽ, പ്രത്യേക കീവേഡുകളൊന്നുമില്ലാതെ തന്നെ ഫംഗ്ഷനുകൾക്കുള്ളിൽ നിന്ന് ഗ്ലോബൽ വേരിയബിളുകൾ നേരിട്ട് ആക്സസ് ചെയ്യാനും പരിഷ്ക്കരിക്കാനും കഴിയും. എന്നിരുന്നാലും, അതേ പേരിൽ ഒരു ലോക്കൽ വേരിയബിൾ ഉണ്ടെങ്കിൽ, ഗ്ലോബൽ വേരിയബിൾ ആക്സസ് ചെയ്യാൻ നിങ്ങൾക്ക് സ്കോപ്പ് റെസല്യൂഷൻ ഓപ്പറേറ്റർ :: ഉപയോഗിക്കാം.
ഫംഗ്ഷൻ നിർവചിക്കുക (
int main()ന് പുറത്ത്)// Define the global variable int count = 0; void increase_count() { // Global variables can be accessed directly count = count + 1; Brain.Screen.print("Count: %d", count); Brain.Screen.newLine(); }
int main()നുള്ളിലെ ഫംഗ്ഷൻ വിളിക്കുക.int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Begin project code increase_count(); increase_count(); }
ഡാറ്റ തരങ്ങൾ#
വേരിയബിളുകളിൽ സൂക്ഷിക്കാൻ കഴിയുന്ന നിരവധി തരം ഡാറ്റകളെ C++ പിന്തുണയ്ക്കുന്നു. ചില തരങ്ങൾ വീണ്ടും നിയോഗിക്കാൻ കഴിയും, മറ്റുള്ളവ വ്യക്തിഗത ഘടകങ്ങൾ പരിഷ്കരിക്കാൻ അനുവദിക്കുന്നു. ഏറ്റവും സാധാരണയായി ഉപയോഗിക്കുന്ന തരങ്ങൾ ചുവടെയുണ്ട്:
Integer#
ഒരു പൂർണ്ണസംഖ്യ (int) ഒരു പൂർണ്ണസംഖ്യയാണ്.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
int distance = 100;
// Move the robot forward for the variable value in mm
Drivetrain.driveFor(forward, distance, mm);
// Add to the variable and move forward the new value,
// for 200mm total
wait(1, seconds);
distance = distance + 100;
Drivetrain.driveFor(forward, distance, mm);
}
Double#
ഇരട്ട എന്നത് ഉയർന്ന കൃത്യതയുള്ള (സാധാരണയായി 64-ബിറ്റ്) ഒരു ദശാംശ ബിന്ദു സംഖ്യയാണ്.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Store a value with decimal points
double raw_value = 0.88;
// Print the decimal value as a percentage
Brain.Screen.print("%.1f%%", raw_value * 100);
}
Float#
float ദശാംശ സംഖ്യകൾ സംഭരിക്കുന്നു. മിക്ക VEX പ്രോജക്റ്റുകളിലും, മെമ്മറി ഉപയോഗം ഒരു ആശങ്കയല്ലെങ്കിൽ, floats നെക്കാൾ doubles ആണ് അഭികാമ്യം.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Store a value with decimal points
float raw_value = 0.88;
// Print the decimal value as a percentage
Brain.Screen.print("%.1f%%", raw_value * 100);
}
String#
ഒരു C-ശൈലി സ്ട്രിംഗ് (char variable_name[]) എന്നത് ഒരു പ്രതീക ശ്രേണിയിൽ സംഭരിച്ചിരിക്കുന്ന പ്രതീകങ്ങളുടെ ഒരു ശ്രേണിയാണ്. C-ശൈലി സ്ട്രിംഗുകൾ വാചകവും ഒരു ശൂന്യ ടെർമിനേറ്ററും സംഭരിക്കാൻ പര്യാപ്തമായിരിക്കണം.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Set the variable to a string then print the string
char message[] = "Ready!";
Brain.Screen.print(message);
}
Boolean#
ഒരു ബൂളിയൻ (bool) true അല്ലെങ്കിൽ false മൂല്യങ്ങളെ പ്രതിനിധീകരിക്കുന്നു.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Set the state of the variable
bool delivered = false;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
}
പ്രോജക്റ്റിലെ ഏത് ഘട്ടത്തിലും ബൂളിയനുകൾ മാറ്റാൻ കഴിയും.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Print the value of the delivered variable
bool delivered = false;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
wait(2, seconds);
// Clear the screen and print the value of the variable again
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1,1);
delivered = true;
// Print different messages depending on the Boolean.
if (delivered) {
Brain.Screen.print("Package delivered!");
} else {
Brain.Screen.print("Delivering...");
}
}
Arrays#
അറേകൾ എന്നത് സൂചിക 0 മുതൽ ആരംഭിക്കുന്ന, ഒരേ തരത്തിലുള്ള ഘടകങ്ങളുടെ നിശ്ചിത വലുപ്പത്തിലുള്ള ശേഖരങ്ങളാണ്. അറേയുടെ നീളം അറിയുകയും സൃഷ്ടിക്കുന്ന സമയത്ത് പ്രഖ്യാപിക്കുകയും ചെയ്യുന്നത് നല്ല രീതിയാണ്.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Declare an array of 5 integers
int distances[5] = {100, 200, 150, 300, 250};
// Drive and turn 4 times to move the distance of the first four distances of the array
for (int i = 0; i < 4; i++) {
Drivetrain.driveFor(forward, distances[i], mm);
Drivetrain.turnFor(right, 90, degrees);
}
}
2D Arrays#
ഗ്രിഡുകൾ, പട്ടികകൾ അല്ലെങ്കിൽ മാട്രിക്സുകൾ പ്രതിനിധീകരിക്കാൻ സാധാരണയായി ഒരു 2D അറേ ഉപയോഗിക്കുന്നു. ഓരോ വരിയും ഡാറ്റയുടെ ഒരു പ്രത്യേക ഗ്രൂപ്പിംഗിനെ പ്രതിനിധീകരിക്കുന്നു.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Assign the values in the matrix 2D array
int matrix[3][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 }
};
// Loop through each row
for (int i = 0; i < 3; i++) {
// Loop through each column in the row
for (int j = 0; j < 3; j++) {
Brain.Screen.print("%d, ", matrix[i][j]);
}
Brain.Screen.newLine();
}
}
ഒരു 2D അറേയ്ക്കുള്ളിൽ നിങ്ങൾക്ക് നിർദ്ദിഷ്ട ഘടകങ്ങൾ പരിഷ്കരിക്കാൻ കഴിയും:
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
// Begin project code
// Assign the values in the matrix 2D array
int matrix[3][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 }
};
// Modify the color (in column 2) in row 0
matrix[0][1] = 200;
// Print the modified row from the matrix 2D array
for (int j = 0; j < 3; j++) {
Brain.Screen.print("%d, ", matrix[0][j]);
}
}
Constant Variables#
const ഉപയോഗിച്ച് പ്രഖ്യാപിച്ച ഒരു വേരിയബിൾ ഇനീഷ്യലൈസ് ചെയ്ത ശേഷം പരിഷ്കരിക്കാൻ കഴിയില്ല.
int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();
const int maxSpeed = 100;
// maxSpeed can be used like a normal variable
Drivetrain.setDriveVelocity(maxSpeed, percent);
}