Introduction - Getting Started
Flow of Control
You must’ve heard about an expression in your school days. An expression in C++ is any valid combination of operators, constants and variables. Expressions are quite simple such that users can understand it effortlessly. There’s nothing more to discuss expressions. Now let’s discuss about the remaining few operators.
C++ offers a conditional operator ( ? : ) that stores a value depending upon a condition. It is also known as the ternary operator as it requires three operands. The syntax is as follows:
expression1 ? expression2 : expression3;
result = a > b ? a : b;
This statement basically means that if ‘a’ is greater than ‘b’ then ‘result’ will be equal to ‘a’ and if ‘a’ is less than ‘b’ then ‘result’ will be equal to ‘b’. Let’s say a=10 and b=5, then result will be equal to 10 and if a=20 and b=40, then result will be equal to 40.
A comma operator is used to string together several expressions. The group of expressions separated by comma is evaluated left-to-right in sequence and the result of the right-most expression becomes the value of the total comma-separated expression.
b=( a=2 , a+1 ); Here’s what happens, firstly ‘a’ gets the value two and then assigns ‘b’ the value ‘a+1′ that is three.
In C++ programming language, the assignment operator is a operator used for assignment. It is represented by the equality sign ( = ). It assigns value to the variable based on a property. The assignment operator property is a basically a property where a variable is assigned with either the value of an expression or a constant. The variable may be on the LHS or the RHS.
Example: If x=2
The following expression can be written as “2+10=x”. Thus the value of x is equal to twelve.
Increment / Decrement Operators
C++ includes two useful operators not generally found in other computer languages. These are the increment and decrement operators. The increment operator ( ++ ) adds 1 to its operand. The decrement operator ( — ) subtracts 1 from its operand. However, both the increment and the decrement operators come in two varieties:
When an increment or decrement operator precedes an operand we refer to it as the prefix version. When C++ encounters a prefix version, it performs the increment/decrement before using the value of the operand that is the prefix operator follows the change then use rule. They first change the value of the operand and then use the new value to evaluate the expression.
Example: If sum=0 and count=10
sum= sum+( ++count );
Firstly, the value of ‘count’ gets incremented to 11. So the expression becomes “sum=0+11“. Thus ‘sum’ is equal eleven. For those curious why in the LHS we did not equate sum to zero, that’s cause its against the assignment operator property.
When an increment or decrement operator follows its operand we refer to it as the postfix version. When C++ encounters a postfix version, it first uses the value of the operand and then performs the increment/decrement that is the postfix operator follows the use then change rule.
Example: If sum=0 and count=10
sum= sum+( count-- );
Firstly, we evaluate the expression that is “sum=0+10“. The value of ‘sum’ is equal ten. Thereafter the value if ‘count’ gets decremented to nine. This illustrates the use then change rule in action.
The Weekly Program
Now let’s create a program to check whether a student passed an examination or not.
int grade, perc;
cout<<"Enter Grade: ";
cout<<"Enter Overall Percentage: ";
cout<<"Result: Student ";
cout<<(perc>=35 ? "Passed" : "Failed");
cout<<( perc>=35 ? “Passed” : “Failed” )
This could be the only statement you failed to grasp. It’s alright, let me explain. We have put the conditional operator inside the ‘cout’ so that option ( Passed/Failed ) may be displayed on the screen rather than just storing the options in a variable. This is the reason the options are written in double quotes.
If the percentage is greater than or equal to 35 then “Passed” will be displayed on the screen and if the percentage is less than 35 then “Failed” will be displayed on the screen.
Execution of the Program
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!!!