Introduction - Getting Started
Flow of Control
C++ is a very rich in built-in operators. In fact, C++ places more significance on operators that most other computer languages do. Operators are tokens that trigger some computation when applied to variables and other objects in an expression. The objects of the operation are referred as operands. C++’s rich set of operators comprises of arithmetic, relational, logical, conditional and certain other types of operators.
Let’s discuss these operators in detail.
To do arithmetic, C++ uses arithmetic operators. These are again subdivided into the following:
Operators that act on one operand are referred to as Unary Operators. C++ has the following Unary Operators:
Unary Negation Operator
The unary negation operator ( – ) produces the negative of its operand. The operand is preceded by a negative sign ( – ). The operand to the unary negation operator must be an arithmetic type.
Example: If a=5 then –a means -5
Unary Plus Operator
The unary plus operator ( + ) produces the positive of its operand. The operand is preceded by a positive sign ( + ). The operand to the unary plus operator must be an arithmetic type.
Example: If a=–5 then +a means –5
Operators that act upon two operands are referred to as Binary Operators. C++ has the following Binary Operators:
The operator which is used to find the sum of the two operands. It is represented by the plus sign ( + ).
Example: If x + y ( x=5, y=10 ) results in 15
The operator which is used to find the difference between the two operands. It is represented by the negative sign ( – ).
Example: If x – y ( x=15, y=10 ) results in 5
The operator multiplies the values of its two operands. It is represented by the multiplication sign ( * ).
Example: If x * y ( x=15, y=10 ) results in 150
The operator divides its first operand by the second. It is represented by the division sign ( / ).
Example: If x / y ( x=50, y=10 ) results in 5
The operator finds the remainder by dividing the first by the second operand. It is represented by the modulus sign ( % ).
Example: If x % y ( x=50, y=10 ) results in zero
The operator which determines the relation between two operands. The operators return a true value if the statement is correct and return a false value if the statement is incorrect. The operators are as follows:
The operators that refer to the ways these relationship among values can be connected. C++ provides three logical operators to combine existing expressions.
Note: True value is equal to one and False value is equal to zero
Logical OR Operator
The Logical OR Operator ( || ) evaluates to true ( 1 ) if either of its operands evaluates to true. The operator works as if the operands are getting added ( A+B ).
Logical AND Operator
The Logical AND Operator ( && ) evaluates to true ( 1 ) only if both of its operands are true. The operator works as if the operands are getting multiplied ( A*B ).
Logical NOT Operator
The Logical NOT Operator ( ! ) negates or reverses the truth value of the expression following it and vice versa.
The last few operators will be discussed in the next module.
The Weekly Program
Now let’s create a program to print the Unit test report card of a student.
float s1, s2, s3, s4, s5, marks, perc;
cout<<"Marks obtained in Science:\n";
cout<<"Marks obtained in Mathematics:\n";
cout<<"Marks obtained in English:\n";
cout<<"Marks obtained in Computer Science:\n";
cout<<"Marks obtained in Social Studies:\n";
cout<<"Unit Test Report Card\n\n";
cout<<"Science Marks: "<<s1<<"\n";
cout<<"Mathematics Marks: "<<s2<<"\n";
cout<<"English Marks: "<<s3<<"\n";
cout<<"Computer Science Marks: "<<s4<<"\n";
cout<<"Social Studies Marks: "<<s5<<"\n";
cout<<"Percentage of is: "<<perc<<"%";
Execution of the Program
The program is quite self-explanatory. Now Compile the program. If the compiler shows errors then check my program and resolve it. Then Run the program. Here’s the output:
Now it’s time to do it yourself and feel free to mess around with the program to understand in-depth what each statement does. Don’t forget to save your program.
That’s it for today, see you next week!!!