FACTOID # 20: Brazil is the heliport capital of the world.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > C plus plus examples

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; } 

  Results from FactBites:
 
DAMMCQs: Appendix. C: MCQs and Bloom's Taxonomy (2460 words)
Examples of learning objectives at this level are: understand facts and principles, interpret verbal material, interpret charts and graphs, translate verbal material to mathematical formulae, estimate the future consequences implied in data, justify methods and procedures.
Examples of learning objectives at this level are: apply concepts and principles to new situations, apply laws and theories to practical situations, solve mathematical problems, construct graphs and charts, demonstrate the correct usage of a method or procedure.
Examples of learning objectives at this level are: recognize unstated assumptions, recognises logical fallacies in reasoning, distinguish between facts and inferences, evaluate the relevancy of data, analyse the organizational structure of a work (art, music, writing).
C++ examples - Wikipedia, the free encyclopedia (564 words)
These are some examples of code from the C++ Programming Language.
The word "grade" in the previous example is a variable to which a number is stored.
C++ supports classes, which are objects which one may define.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m