Page 39 - DCAP404 _Object Oriented Programming
P. 39
Object-oriented Programming
Notes In the above program code, we determining whether num1 is greater than num2. The variable,
imax is assigned the value, num1, if the expression, (num1 >num2), evaluates to true, and the
value, num2, if the expression evaluates to false. The above program code can be modified using
the conditional operator as:
Imax = (num1 > num2) ? num1 : num2;
The ?: Operator is called the ternary operator since it has three operands.
This example calculates the grade of a Ram based on his marks.
Int marks = 0;
Cout << “Please enter marks of the Ram;
Cin >> marks;
Char grade = (marks < 80), the variable, grade is assigned the value, ‘A’. If Ram’s score is less
than or equal to 80 grade is assigned the value ‘B’.
The following code will display either “PASSED” or “FAILED”, depending on the value in the
variable, score.
Cout << {score > 50? “PASSED” or FAILED, depending on the value in the variable, score.
Cout << (score > 50? “PASSED” : “FAILED”} << end1;
Self Assessment
Fill in the blanks:
4. The data items that operators act upon are called…………………... .
5. A declared keyword cannot be used as a ……………….. name.
6. The operands acted upon by arithmetic operators must represent ………………… values.
7. An operation between a float and double will result in a………………. .
8. A ………….. can have a value of one or zero.
2.4 Control Structures
C++ program may require that a logical test be carried out at some particular point within the
program. One of several possible actions will then be carried out, depending on the outcome of
the logical test. This is known as branching. There is also a special kind of branching, called
selection, in which one group of statements is selected from several available groups. In addition,
the program may require that a group of instructions be executed repeatedly, until some logical
condition has been satisfied. This is known as looping. Sometimes the required number of
repetitions is known in advance; and sometimes the computation continues indefinitely until
the logical condition becomes true.
All of these operations can be carried out using the various control statements included in C++.
Several logical expressions are given below.
count < = 100
sqrt( x + y + z) > 0.00
answer = = 0
32 LOVELY PROFESSIONAL UNIVERSITY