1 / 24

In the Name of Allah The Most Merciful The Most Compassionate DECISIONS vustudents.ning

In the Name of Allah The Most Merciful The Most Compassionate DECISIONS http://vustudents.ning.com. Relational Operators. It finds a relationship between two expressions . These operators include: Greater than > Less than < Greater than or equal to >= Less than or equal to <=

thetis
Download Presentation

In the Name of Allah The Most Merciful The Most Compassionate DECISIONS vustudents.ning

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. In the Name of Allah The Most Merciful The Most CompassionateDECISIONShttp://vustudents.ning.com

  2. Relational Operators • It finds a relationship between two expressions.These operators include: • Greater than > • Less than < • Greater than or equal to >= • Less than or equal to <= • Equal to == • Not equal to != • e-g; if x=2,y=6,z=1 • x>y is True/False • y==z is True/False • z<=y is True/False

  3. Logical Operators • Logical operators are used to form compound statements. The result of a logical expression is true or False. The operators include: • AND && • OR || • NOT ! • e-g; if x=3,y=7,z=2 • x>y && x>z is True/False • y==z || y>= x is True/False • !(z<=y) is True/False

  4. The if Statement • The “if statement” is used to execute (or ignore) a set of statements after testing a condition. It evaluates a condition,if it is true the statement(s) following the if statement is executed otherwise the control is transferred to the next statement • The syntax is: 1) if (condition*) statement -1; statement -2; * specifies a condition or a relational expression

  5. If the condition is true, statement-1 will be executed otherwise control shifts to statement-2 • Syntax for set of statements 2) if (condition) { statements; } statement -n;

  6. FLOWCHART FALSE TRUE condition Set of Statements Statements after if statement

  7. In C++ any non-zero value is TRUE. Similarly, all zero values are FALSE. # include<iostream.h> void main() { int a=100; b=200; if(a>b) cout<<“a is greater then b”; cout<<“\nbye”; } Output?? a is greater then b bye

  8. # include<iostream.h> void main() { int a; cout<<“\nEnter a number: ”; cin>>a; if (a%3 == 0) cout<<“\nNumber ”<<a<<” is divisible by 3”; } • Output?? Enter a number: 12 Number 12 is divisible by 3

  9. The if-else Statement • This type of statement is used for two way decisions.The if-else statement evaluates the condition, if it is true then the first block is executed otherwise the first block is ignored and the second block following the else is executed. • Syntax-1 if (condition) statement -1; else statement -2;

  10. Syntax-2 if (condition) { statements; } else { statements; } First Block Second Block

  11. FLOWCHART FALSE TRUE condition Block-2 Block-1 Statements after if structure

  12. # include<iostream.h> void main() { int x,y; cout<<“\nEnter two numbers: ”; cin>>x; cin>>y; if (x == y) cout<<“\nThe numbers are equal”; else cout<<“\nThe numbers are not equal”; } • Output??

  13. The nested-if statement • When an if statement is used within another “if statement”,it is called a nested if statement.It is used for multi-way tasking • Syntax is: if (condition-1) if (condition-2) { statements-1; } statements-2;

  14. FLOWCHART FALSE TRUE Condition-1 FALSE TRUE Condition-2 Statement-2 Statement-1 next statement

  15. # include<iostream.h> void main() { int x,y,z; cout<<“\nEnter three numbers: ”; cin>>x; cin>>y; cin>>z; if (x == y) { if (x == z) cout<<“\nThe numbers are equal”; } else cout<<“\nThe numbers are not equal”; }

  16. The else-if construct • Nested if-else structure is difficult to understand. Another way to make multi way decisions is by using else-if construct • Syntax is: if (condition-1) statements-1; else if (condition-2) statements-2; else if (condition-3) statements-3; . . else statements-n;

  17. FLOWCHART TRUE condition Block-1 FALSE TRUE condition Block-2 FALSE condition Statements after if-else structure

  18. # include<iostream.h> void main() { int x=10; int y=4; char ch; cout<<“\nEnter the Operator: ”; cin>>ch; if (ch == ‘+’) cout<<“\nSum is = ”<< (x+y); else if (ch == ‘-’) cout<<“\nDifference is = ”<< (x-y); else cout<<“\nNot Valid Input”; }

  19. switch Statement • It is a substitution of else-if construct.it evaluates an expression and returns a value. One of the choices or cases in the switch statement is executed depending upon the returned value. If it matches the any case, that particular case is executed. If no case is matched than statement(s) under the default is/are executed.Use of default is optional. If not used then the control exits the body of switch statement and goes to the next statement.

  20. Syntax is: switch (var/expression) { case const-1: statements; break; case const-2: statements; break; . . default: statements; }

  21. # include<iostream.h> void main() { int x=10; int y=4; char ch; cout<<“\nEnter the Operator: ”; cin>>ch; switch(ch) { case ‘+’: cout<<“\nSum is = ”<< (x+y); break; case ‘-’: cout<<“\nDifference is = ”<< (x-y); break; default: cout<<“\nNot Valid Input”; } }

  22. The Conditional Operator • The conditional operator is used as an alternative to simple if-else statement. The conditional operator consists of a “?” and a colon “:”. Syntax is: condition ? Exp1 : Exp2 ; • For example; max=(a>b)? a : b; same as: if(a>b) max=a; else max=b;

  23. Hierarchy • Increment /Decrement (RL) ++,-- • Logical NOT (RL) ! • Arithmetic (LR) *, /,% • Arithmetic (LR) +, - • Relational (LR) >,<,<=,>= • Relational (LR) == , != • Logical AND (LR) && • Logical OR (LR) || • Conditional (RL) ?: • Assignment (RL) =,+=,-=,...

  24. Solve: • 2*3/4+4/4+8-2+5/8 • If x=11,y=6,z=1 • x == 5 || y ! = 3 • 5 && y !=8 || 0 • ! ( x > 9 && y! = 23 ) http://vustudents.ning.com Ans= 8 T T F

More Related