1 / 19

Tutorial 3

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.

hunter
Download Presentation

Tutorial 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 3 By: ShrutiRathee Concordia University

  2. Relational And Equality Operators • These are the operators used for the comparison of two expressions. • They are as follows :

  3. 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.

  4. 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.

  5. ! 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.

  6. && operator • ( (7 == 7) && (9 > 12) ) // evaluates to false ( true && false )

  7. || operator • ( (7 == 7) && (9 > 12) ) // evaluates to false ( true && false )

  8. 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)

  9. If - Else • The structure of If : if (condition) statement; • The structure of If – else : if (condition) statement1; else statement2;

  10. Example • if (i=0) cout<<“Good Morning”; • if (i=0) cout<<“Good Morning”; else cout<<“Good Night”;

  11. 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”;

  12. Question • Make a program to input a number using “cin” and check if that number is divisible by 2 or not.

  13. 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--;

  14. Contd. • For while loop we have to make condition false somewhere to stop the loop otherwise it will be an infinite loop.

  15. 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.

  16. 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.

  17. 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"; }

  18. Syntax of switch • switch (expression) { case constant1: group of statements 1; break; case constant2: group of statements 2; break; . . . default: default group of statements; }

  19. 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

More Related