decisions in the name of allah the most merciful the most compassionate decisions

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

Upload: matthew-thornton

Post on 31-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

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

http://vustudents.ning.comhttp://vustudents.ning.com

Page 2: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Relational OperatorsRelational 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

Page 3: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Logical OperatorsLogical 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

Page 4: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

The if StatementThe 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

Page 5: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

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;

Page 6: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Set of Statements

condition

Statements after if statement

TRUEFALSE

FLOWCHARTFLOWCHART

Page 7: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

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

Page 8: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

# 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: 12Number 12 is divisible by 3

Page 9: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

The if-else StatementThe 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;

Page 10: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Syntax-2

if (condition)

{

statements;

}

else

{

statements;

}

First Block

Second Block

Page 11: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Block-1

condition

Statements after if structure

TRUEFALSE

FLOWCHARTFLOWCHART

Block-2

Page 12: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

# 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??

Page 13: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

The nested-if statementThe 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;

Page 14: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Statement-1

Condition-1

next statement

TRUEFALSE

FLOWCHARTFLOWCHART

Statement-2

Condition-2TRUEFALSE

Page 15: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

# 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”;}elsecout<<“\nThe numbers are not equal”;}

Page 16: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

The else-if constructThe 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;

Page 17: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Block-1condition

Statements after if-else structure

TRUE

FALSE

FLOWCHARTFLOWCHART

Block-2

FALSE

condition

condition

TRUE

Page 18: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

# 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);elsecout<<“\nNot Valid Input”;}

Page 19: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

switch Statementswitch 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.

Page 20: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Syntax is:

switch (var/expression)

{

case const-1:

statements;

break;

case const-2:

statements;

break; .

.

default:

statements;

}

Page 21: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

# 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”;

}

}

Page 22: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

The Conditional Operator 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;

Page 23: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

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

Page 24: DECISIONS  In the Name of Allah The Most Merciful The Most Compassionate DECISIONS

Solve: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