C and C++ Language Guide

Add to Favourites
Post to:

Description
This tutorial is a part of series of C++ online sessions explaining the basics of the C and C++ language. Since C++ is an enhancement to the language C in terms of C, most of the variable declarations,operators, and functions pertain to both the languages discussed here. It shows the operators all Relational, Unary, Binary and Logical operators in action along with their precedence rules.

Comments
Presentation Transcript Presentation Transcript

Variables and Operators : Variables and Operators

Objectives : Objectives C++ Character set Tokens keywords identifiers literals punctuators operators. variable declaration variables initialization formatting output Operators

Assignment Operators : Assignment Operators Assignment (=) The assignment operator assigns a value to a variable. a = 5; // assignment operator #include int main () { int a, b; // a:?, b:? a = 10; // a:10, b:? b = 4; // a:10, b:4 a = b; // a:4, b:4 b = 7; // a:4, b:7 cout << "a:"; cout << a; cout << " b:"; cout << b; return 0; } a = 2 + (b = 5); is equivalent to: 1 2 b = 5; a = 2 + b;

Contd.. : Contd.. that means: first assign 5 to variable b and then assign to a the value 2 plus the result of the previous assignment of b (i.e. 5), leaving a with a final value of 7. The following expression is also valid in C++: a = b = c = 5; It assigns 5 to the all three variables: a, b and c. Arithmetic operators ( +, -, *, /, % ) The five arithmetical operations supported by the C++ language are:

Arithmetic operators ( +, -, *, /, % ) : Arithmetic operators ( +, -, *, /, % ) The five arithmetical operations supported by the C++ language are: + addition - subtraction * multiplication / division % modulo Assignment operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators:

Contd.. : Contd.. expression is equivalent to value += increase; value = value + increase; a -= 5; a = a - 5; a /= b; a = a / b; price *= units + 1; price = price * (units + 1); Increase and decrease (++, --) Operators Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. Thus: 1 2 3 c++; c+=1; c=c+1;

Examples : Examples Example 1 Example 2 B=3; A=++B; // A contains 4, B contains 4 B=3; A=B++; // A contains 3, B contains 4

Relational and equality operators ( ==, !=, >, <, >=, <= ) : Relational and equality operators ( ==, !=, >, <, >=, <= ) In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is. Here is a list of the relational and equality operators that can be used in C++: Relational and equality operators ( ==, !=, >, <, >=, <= ) In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is. Here is a list of the relational and equality operators that can be used in C++: == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to

examples : examples Here there are some examples: 1 2 3 4 5 (7 == 5) // evaluates to false. (5 > 4) // evaluates to true. (3 != 2) // evaluates to true. (6 >= 6) // evaluates to true. (5 < 5) // evaluates to false. Of course, instead of using only numeric constants, we can use any valid expression, including variables. Suppose that a=2, b=3 and c=6, 1 2 3 4 (a == 5) // evaluates to false/0 since a is not equal to 5. (a*b >= c) // evaluates to true/1 since (2*3 >= 6) is true/1. (b+4 > a*c) // evaluates to false/0 since (3+4 > 2*6) is false/0. ((b=2) == a) // evaluates to true.

Logical operators ( !, &&, || ) : Logical operators ( !, &&, || ) The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example: 1 2 3 4 !(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true. !(6 <= 4) // evaluates to true because (6 <= 4) would be false. !true // evaluates to false !false // evaluates to true. The logical operators && and || are used when evaluating two expressions to obtain a single relational result. The operator && corresponds with Boolean logical operation AND. This operation results true if both its two operands are true, and false otherwise. The following panel shows the result of operator && evaluating the expression a && b:

Contd.. : Contd.. && OPERATOR a b a && b true true true true false false false true false false false false The operator || corresponds with Boolean logical operation OR. This operation results true if either one of its two operands is true, thus being false only when both operands are false themselves. Here are the possible results of a || b: || OPERATOR a b a || b true true true true false true false true true false false false 2 ( (5 == 5) && (3 > 6) ) // evaluates to false ( true && false ). ( (5 == 5) || (3 > 6) ) // evaluates to true ( true || false ).

Contd.. : Contd.. When using the logical operators, C++ only evaluates what is necessary from left to right to come up with the combined relational result, ignoring the rest. Therefore, in this last example ((5==5)||(3>6)), C++ would evaluate first whether 5==5 is true, and if so, it would never check whether 3>6 is true or not. This is known as short-circuit evaluation, and works like this for these operators: operator short-circuit && if the left-hand side expression is false, the combined result is false (right-hand side expression not evaluated). || if the left-hand side expression is true, the combined result is true (right-hand side expression not evaluated). This is mostly important when the right-hand expression has side effects, such as altering values: if ((i<10)&&(++i

Conditional operator ( ? ) : Conditional operator ( ? ) The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is: condition ? result1 : result2 If condition is true the expression will return result1, if it is not it will return result2. 1 2 3 4 7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5. 7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2. 5>3 ? a : b // returns the value of a, since 5 is greater than 3. a>b ? a : b // returns whichever is greater, a or b.

example : example // conditional operator #include using namespace std; int main () { int a,b,c; a=2; b=7; c = (a>b) ? a : b; cout << c; return 0; } 7 In this example a was 2 and b was 7, so the expression being evaluated (a>b) was not true, thus the first value specified after the question mark was discarded in favor of the second value (the one after the colon) which was b, with a value of 7.

Comma operator ( , ) : Comma operator ( , ) The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered. For example, the following code: a = (b=3, b+2); Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3.

Bitwise Operators ( &, |, ^, ~, <<, >> ) : Bitwise Operators ( &, |, ^, ~, <<, >> ) Bitwise operators modify variables considering the bit patterns that represent the values they store. operator asm equivalent description & AND Bitwise AND | OR Bitwise Inclusive OR ^ XOR Bitwise Exclusive OR ~ NOT Unary complement (bit inversion) << SHL Shift Left >> SHR Shift

Explicit type casting operator : Explicit type casting operator Type casting operators allow you to convert a datum of a given type to another. There are several ways to do this in C++. The simplest one, which has been inherited from the C language, is to precede the expression to be converted by the new type enclosed between parentheses (()): 1 2 3 int i; float f = 3.14; i = (int) f; The previous code converts the float number 3.14 to an integer value (3), the remainder is lost. Here, the typecasting operator was (int). Another way to do the same thing in C++ is using the functional notation: preceding the expression to be converted by the type and enclosing the expression between parentheses: i = int ( f ); Both ways of type casting are valid in C++.

Precedence of operators : Precedence of operators When writing complex expressions with several operands, we may have some doubts about which operand is evaluated first and which later. For example, in this expression: a = 5 + 7 % 2 we may doubt if it really means: 1 2 a = 5 + (7 % 2) // with a result of 6, or a = (5 + 7) % 2 // with a result of 0 The correct answer is the first of the two expressions, with a result of 6.

Contd. : Contd. There is an established order with the priority of each operator, and not only the arithmetic ones (those whose preference come from mathematics) but for all the operators which can appear in C++. From greatest to lowest priority, the priority order is as follows:

Precedence : Precedence Level Operator Description Grouping 1 :: scope Left-to-right 2 () [] . -> ++ -- dynamic_cast static_cast reinterpret_cast const_cast typeid postfix Left-to-right 3 ++ -- ~ ! sizeof new delete unary (prefix) Right-to-left * & indirection and reference (pointers) + - unary sign operator 4 (type) type casting Right-to-left 5 .* ->* pointer-to-member Left-to-right 6 * / % multiplicative Left-to-right 7 + - additive Left-to-right 8 << >> shift Left-to-right 9 < > <= >= relational Left-to-right 10 == != equality Left-to-right 11 & bitwise AND Left-to-right 12 ^ bitwise XOR Left-to-right 13 | bitwise OR Left-to-right 14 && logical AND Left-to-right 15 || logical OR Left-to-right 16 ?: conditional Right-to-left 17 = *= /= %= += -= >>= <<= &= ^= |= assignment Right-to-left 18 , comma Left-to-right

Contd.. : Contd.. Grouping defines the precedence order in which operators are evaluated in the case that there are several operators of the same level in an expression. All these precedence levels for operators can be manipulated or become more legible by removing possible ambiguities using parentheses signs ( and ), as in this example: a = 5 + 7 % 2; might be written either as: a = 5 + (7 % 2); or a = (5 + 7) % 2; depending on the operation that we want to perform.

Want to learn?

Sign up and browse through relevant courses.

Name:
Your Email:
Password:
Country:
Contact no:


Area code Number
Subjects you are interested in:
Word verification: (Enter the text as in image)


Sign Up Already a member? Sign In
I agree to WizIQ's User Agreement & Privacy Policy
jyoti insan
Certified and experienced IT trainer with 8 years teaching experience
User
5 Members Recommend
20 Followers

Your Facebook Friends on WizIQ

Give live classes, create & sell online courses

Try it free Plans & Pricing

Connect