1 / 10

C++ Language Tutorial

C++ Language Tutorial. Lesson 2 – Assignment and expressions. Variables and Assignment Literals Evaluation Operators/Operands Order of Precedence. Statements.

thina
Télécharger la présentation

C++ Language Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Language Tutorial Lesson 2 – Assignment and expressions • Variables and Assignment • Literals • Evaluation • Operators/Operands • Order of Precedence

  2. Statements Last week we learned that a statement such as “std::cout<<“Hello World”<<std::endl;” must end with a semi colon. It is essential you don’t forget this. • C++ programs are composed of statements • Statements generally perform a single action

  3. Variables Programmers are lazy and don’t like to have to repeat things. Which is a reason why we use variables. A variable stores a value. • Variables MUST be declared before they are used • In c++ a variable must be given a data type. Meaning we need to tell the program what data to expect within the variable. There are three data types by default: • An integer denoted by “int” (eg. 2 or 257) • A character denoted by “char” (eg ‘j’ or ‘y’) • A floating point number denoted by “float” (eg. 2.3 or 5.7) • Variable declaration is a statement

  4. Assignment Assignment will assign data to a variable using the =operatior. The variable is first declared. Below statement declares a variable called anint as the data type of an int, which is an integer. intanInt; Now we have declared the variable we can assign some data to it. So to assign the variable to the integer 12 we write below the first statement. • anInt= 12; Assignment can be done along with the declaration as one statement: • IntanInt = 12;

  5. Other Data types Some data types are not part of c++. These can be user defined and imported with the #include statement. An example is the string data type that holds text An example would be: #include <string> String variablename = “Hello World”; A literal is not a variable. A literal is the value. for example take the statement: std::cout<<”Hello”; In this case ”Hello” is a literal.

  6. Operators and Operands In c++ you can do math, which you may need to use at some point. For this you will need operators which c++ provides. The values you use operators on are called operands. For example 2 + 7 The + is the operator while 2 and 7 are the operands.

  7. Key operators • THE MAIN ARITHMETIC OPERATORS. • + -> the addition operator • - -> the subtraction operator • -> the multiplication operator • / -> the division operator • % -> the modulus operator (which return the remainder of division. For example 7 % 3 evaluates to 1 because we goes in to 7 two times with a remainder of 1. • An extensive list will be available in the next few slides.

  8. Evaluation We call a statement that performs a calculation an expression. An expression is said to be evaluated when operators are used in the expression. For example: • 1 + 1 expresses 2 • 12 * 5 expresses 60 After evaluating an expression we usually do something useful with the result such as outputting it. You can evaluate two variables as long as the variables are both of int or both of floats. Actually an int and a float can be evaluated together however this doesn't output the expected result.

  9. Precedence When an expression is evaluated unless brackets are used to state what must be calculated first, then c++ will use the order of precedence to determine what to calculate first. This is essential to know as with complex expressions you need to know what will be evaluated first. *, /, and % have higher precedence than + or -. For example: • 2 * 4 + 8 Will evaluate to 16 because * has a higher precedence than +. Please visit http://en.cppreference.com/w/cpp/language/operator_precedence Brackets () can be used to force precedence where for the use of more than one set of brackets the inner most bracket is always evaluated first then works outwards.

  10. The end That concludes are last lesson you should now understand variables and assignment Along with how to form an expression and store the evaluation. Knowing the the main Operators. As allways the source code for the demonstration with fully explained comments (more extensive than the video), along with the execution able file

More Related