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

Post on 31-Dec-2015

215 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

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

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

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

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

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;

Set of Statements

condition

Statements after if statement

TRUEFALSE

FLOWCHARTFLOWCHART

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

# 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

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;

Syntax-2

if (condition)

{

statements;

}

else

{

statements;

}

First Block

Second Block

Block-1

condition

Statements after if structure

TRUEFALSE

FLOWCHARTFLOWCHART

Block-2

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

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;

Statement-1

Condition-1

next statement

TRUEFALSE

FLOWCHARTFLOWCHART

Statement-2

Condition-2TRUEFALSE

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

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;

Block-1condition

Statements after if-else structure

TRUE

FALSE

FLOWCHARTFLOWCHART

Block-2

FALSE

condition

condition

TRUE

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

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.

Syntax is:

switch (var/expression)

{

case const-1:

statements;

break;

case const-2:

statements;

break; .

.

default:

statements;

}

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

}

}

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;

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) =,+=,-=,...

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

top related