Presentation is loading. Please wait.

Presentation is loading. Please wait.

DECISIONS In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Similar presentations


Presentation on theme: "DECISIONS In the Name of Allah The Most Merciful The Most Compassionate DECISIONS"— Presentation transcript:

1 DECISIONS http://vustudents.ning.com In the Name of Allah The Most Merciful The Most Compassionate DECISIONS http://vustudents.ning.com

2 Relational Operators n 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!= n 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 n Logical operators are used to form compound statements. The result of a logical expression is true or False. The operators include: èAND&& èOR || èNOT ! n 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 n 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 n The syntax is: 1)if (condition*) statement -1; statement -2; * specifies a condition or a relational expression

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

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

7 n In C++ any non-zero value is TRUE. Similarly, all zero values are FALSE. # include 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 void main() { int a; cout<<“\nEnter a number: ”; cin>>a; if (a%3 == 0) cout<<“\nNumber ”<<a<<” is divisible by 3”; } n Output?? Enter a number: 12 Number 12 is divisible by 3

9 The if-else Statement n 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. n Syntax-1 if (condition) statement -1; else statement -2;

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

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

12 # include 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”; } n Output??

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

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

15 # include 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 n Nested if-else structure is difficult to understand. Another way to make multi way decisions is by using else-if construct n Syntax is: if (condition-1) statements-1; elseif (condition-2) statements-2; elseif (condition-3) statements-3;. else statements-n;

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

18 # include 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 n 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 n Syntax is: switch (var/expression) { case const-1: statements; break; case const-2: statements; break;. default: statements; }

21 # include 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 n 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 ; n For example; max=(a>b)? a : b; same as: if(a>b) max=a; else max=b;

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

24 Solve: n 2*3/4+4/4+8-2+5/8 n 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 TTFTTF


Download ppt "DECISIONS In the Name of Allah The Most Merciful The Most Compassionate DECISIONS"

Similar presentations


Ads by Google