|
These are some examples of code from the C++ Programming Language. C++ (pronounced see plus plus) is a general-purpose computer programming language. ...
Conditionals
if (tester == true) { std::cout << "true" << std::endl; } else if (tester == false) { std::cout << "false" << std::endl; } This code checks whether the boolean variable is true or false and prints out the corresponding value. Note that if tester is of bool type, it is the same as the following code: (If tester is not a boolean variable, and not either 0 or 1, then the code above and code below mean something completely different) if (tester == true) { std::cout << "true" << std::endl; } else { std::cout << "false" << std::endl; } This is a more conventional way of doing it: if (tester) { std::cout << "true" << std::endl; } else { std::cout << "false" << std::endl; } You can also combine ifs, else ifs, and elses all in the same statement, such as this code that prints out a grade letter based on percentages: if (grade >= 90) { std::cout << "A" << std::endl; } else if (grade >= 80) { std::cout << "B" << std::endl; } else if (grade >= 70) { std::cout << "C" << std::endl; } else if (grade >= 60) { std::cout << "D" << std::endl; } else { std::cout << "F" << std::endl; } Note that the braces are not actually part of the syntax of the if statement. The syntax requires only that the clauses be statements, not necessarily compound statements. Thus, the following is also correct: if (tester) std::cout << "true" << std::endl; else std::cout << "false" << std::endl; Of course, a compound statement, if needed, will carry with it a pair of braces: if (tester) { std::cout << "true" << std::endl; n = 1; } else { std::cout << "false" << std::endl; n = 0; } One case requiring a compound statement is the use of nested if statements. The C++ rules state that the 'else' clause matches to the closest 'if'. If this is not what is intended, the inner if must be enclosed in a compound statement. // NB misleading indentation obscuring an error if (tester1) if (tester2) std::cout << "tester1 and tester2 are both true" << std::endl; else std::cout << "tester1 is false" << std::endl; In this example, the logic implied by the indentation does not match the logic implied by the rules of C++, as the else will actually match to the inner if. To correct this, braces are required: if (tester1) { if (tester2) std::cout << "tester1 and tester2 are both true" << std::endl; } else std::cout << "tester1 is false" << std::endl; STL The Standard Template Library provides a variety of useful containers for you to use. Here is an example involving the STL vector: The Standard Template Library (STL) is a software library. ...
A binary tree, a simple type of branching linked data structure. ...
#include <vector> #include <iostream> int main() { std::vector<int> my_vector; // Create a vector that will hold integers int input = 1; // Create a temporary variable to hold the user's input std::cout << "Enter some numbers, enter zero to quit!" << std::endl; while(input) { // While input is nonzero std::cin >> input; my_vector.push_back(input); // This function adds input to the back of the vector } std::cout << "You entered the following numbers:" << std::endl; // Now, iterate through the contents of the vector for(std::vector<int>::iterator i = my_vector.begin(); // Vector.begin() returns an iterator that points to the beginning of the list i != my_vector.end(); ++i) { // Use (*my_iterator) to access the contents of the iterator std::cout << (*i) << std::endl; } std::cout << "Goodbye!" << std::endl; } |