190 likes | 278 Views
Tutorial 3. By: Shruti Rathee Concordia University. Relational And E quality Operators. These are the operators used for the comparison of two expressions. They are as follows :. Example. (4 == 3) // evaluates to false. (67> 23) // evaluates to true. (34 != 12) // evaluates to true.
E N D
Tutorial 3 By: ShrutiRathee Concordia University
Relational And Equality Operators • These are the operators used for the comparison of two expressions. • They are as follows :
Example • (4 == 3) // evaluates to false. • (67> 23) // evaluates to true. • (34 != 12) // evaluates to true. • (2 >= 2) // evaluates to true. • (12< 7) // evaluates to false.
Logical operators • There are three types of logical operators: !, &&, || • ! : This is used for performing the boolean expression Not. Whatever value will be provided to this operator it will inverse it. • && : This is used for performing boolean expression And. So if both the operands will be true the result will true. • || : This is for performing boolean expression Or. In this the result will be always true until both the operands will be false.
! operator • !(6 == 6) // evaluates to false because the expression at its right (5 == 5) is true. • !(8 <= 4) // evaluates to true because (6 <= 4) would be false. • !true// evaluates to false • !false// evaluates to true.
&& operator • ( (7 == 7) && (9 > 12) ) // evaluates to false ( true && false )
|| operator • ( (7 == 7) && (9 > 12) ) // evaluates to false ( true && false )
Control Structures • Usually in programming language statements executed are in sequence. But when we need to control that how the statements are to be executed we use control structures. We have these for control in C++ : • If – Else • While • Do while • Switch • For (done in last tutorial already)
If - Else • The structure of If : if (condition) statement; • The structure of If – else : if (condition) statement1; else statement2;
Example • if (i=0) cout<<“Good Morning”; • if (i=0) cout<<“Good Morning”; else cout<<“Good Night”;
Contd. • We can use if else with other range of values. It is not compulsary that you need only one value that is to be taken. Like : • if (i=0) cout<<“Good Morning”; else if (i=1) cout<<“Good Afternoon”; else cout<<“Good Night”;
Question • Make a program to input a number using “cin” and check if that number is divisible by 2 or not.
While • The format for while loop is : while (expression) statement; • Like we can run a loop with while for vale of i greater than 5. while (i>5) cout<<“hello”; i--;
Contd. • For while loop we have to make condition false somewhere to stop the loop otherwise it will be an infinite loop.
do while • The format is like: do statement ; while (condition); • It is similar to while loop just that in this the statement will be executed at least once even if the condition is not true as it is first executing the statement and then it is checking the condition.
Break statement • This statement is used to exit the loop although if the condition for the exit is not there. Like : for (i=0;i<=10;i++) { cout<<“\nvalue of i:”<<i; if(i=5) cout<<“loop exit”; break; } • So even if condition for exit is when “i” is 11 but it will exit as soon as value of i will reach at 5 as we have given a break statement there.
Switch • With the switch we can select a value which will match the needed condition among the multiple sections of the code. Like : switch (x) { case 1: cout<< "x is 1"; break; case 2: cout<< "x is 2"; break; default: cout << "value of x unknown"; }
Syntax of switch • switch (expression) { case constant1: group of statements 1; break; case constant2: group of statements 2; break; . . . default: default group of statements; }
Question • Make a program to assign student grades based on their marks. Like : 90-95% : A 80-90% : B 70-80%: C 60-70%: D