Lecture 3 Relational Operators
Lecture 3 Relational Operators
Operators
Week 03 Sp 23
• Examples
• ‘a’ < ‘e’ // True
Equality (==) and Assg (=)
Operators
• Common error : Does not cause syntax errors
• Example if ( payCode == 4 )
cout << "You get a bonus!“;
• If == was replaced
if with =
( payCode = 4 )
cout << "You get a bonus!“;
• && operator yields a true result only when both its operands are true.
• || operator yields a false result only when both its operands are false.
Conditional Operators
0
Type conversion
• Automatic Type Conversion
• Casting
Automatic Type Conversion
void main(void)
{
int number = 2;
float factor = 1.5;
double result = number * factor;
cout << "Result is : " <<
result;
}
void main(void)
{
short x = 2;
int y = x;
}
Type conversion
Casting
void main(void)
{
short number = 30000;//(2^16 – 1)/2 (-32768 to
32767)
short result = (number * 10) / 10;
cout << "Result is : " << result;//Result
Incorrect
number = 30000;
result = (long(number) * 10) / 10; //Casting
cout << "Result is : " << result;
}
Operator Precedence!
Example!
int x = 1, y = 2;
int result = y++ + ++x;
cout << result << endl;
cout << x << endl;
cout << y << endl;
1.\n (New line) – We use it to shift the cursor control to the new line
2.\t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same
line.
3.\a (Audible bell) – A beep is generated indicating the execution of the program to alert the user.
4.\r (Carriage Return) – We use it to position the cursor to the beginning of the current line.
5.\\ (Backslash) – We use it to display the backslash character.
6.\’ (Apostrophe or single quotation mark) – We use it to display the single-quotation mark.
7.\” (Double quotation mark)- We use it to display the double-quotation mark.
8.\0 (Null character) – We use it to represent the termination of the string.
9.\? (Question mark) – We use it to display the question mark. (?)
Control Structures:
if, if else and else if Statements
• if statement
• Use of if else
• else-if Statement
• Switch statement
Control Structures
• 3 control structures
• Sequence structure
• Programs executed sequentially by default
• Selection structures
• if, if/else, switch
• Repetition structures
• for, while, do/while
The If Selection Structure
• Selection structure
• Choose among alternative courses of action
• Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”
If the condition is true
Print statement executed, program continues to next statement
If the condition is false
Print statement ignored, program continues
The If Selection Structure
• Translation into C++
If student’s grade is greater than or equal to 60
Print “Passed”
if ( grade >= 60 )
cout << "Passed";
if/else Selection Structure
• if
• Performs action if condition true
• if/else
• Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
The If Selection Structure
• Ternary conditional operator (?:)
– Three arguments (condition, value if true, value if false)
• Code could be written:
cout << ( grade >= 60 ? “Passed” :
“Failed” );
false true
grade >= 60
• Compound statement
• Set of statements within a pair of braces
if ( grade >= 60 )
cout << "Passed.\n";
else
{
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
• Without braces,
cout << "You must take this course again.\n";
always executed
• Block
• Set of statements within braces
The If Selection Structure
• Nested if/else structures
• One inside another, test for
if student’s multiple
grade cases
is greater than or equal to 90
Print “A”
• Once condition met,
else
other statements skipped
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
The If Selection Structure
Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
The If Selection Structure
• You can assign an int type variable a non zero value for true or zero for
false.
• Example
even = (n%2 == 0);
if(even) { do something }
if (x > 0)
num_pos = num_pos + 1;
if (x < 0)
num_neg = num_neg + 1;
if (x == 0)
num_zero = num_zero +1;
if(x > 0)
num_pos = num_pos + 1;
else if(x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;