computer science department relational operators and decisions

31
Computer Science Department Relational Operators And Decisions

Upload: doris-hunt

Post on 31-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science Department Relational Operators And Decisions

Computer Science Department

Relational OperatorsAnd

Decisions

Page 2: Computer Science Department Relational Operators And Decisions

Computer Science Department

Relational Operators

• A Relational Operator compares two values.

• Values can be any built-in C++ data type.

• The comparison involves such relationship as equal to, less than and greater than.

• The result of the comparison is either true or false.

Page 3: Computer Science Department Relational Operators And Decisions

Computer Science Department

Relational Operator

OperatorMeaning

>Greater than

<Less than

==Equal To

!=Not Equal To

>=Greater Than or equal to

<=Less Than or equal to

Page 4: Computer Science Department Relational Operators And Decisions

Computer Science Department

Decisions

• In a program a decision causes a one-time jump to a different part of the program, depending on the values of an expression.

• Decision can be made in C++ in several ways.– IF Statement.– IF and ELSE Statement.– Switch Statement.

Page 5: Computer Science Department Relational Operators And Decisions

Computer Science Department

IF Statement

• C++ uses keyword if to implement the decision control instruction.

if ( this condition is true )

execute this statement;• The if keyword if followed by a test expression in

parenthesis. • If the condition is true than compiler will execute the

immediate statement. But if the condition is false than the immediate statement will not execute, and the compiler will skip the immediate statement and execute the next statement.

Page 6: Computer Science Department Relational Operators And Decisions

Computer Science Department

IF Statement Example

void main ( )

{

int x;

cout<<“Enter a number”;

cin>> x;

if ( x > 100 )

cout<<“the number is greater than 100”;

getche( );

}

Page 7: Computer Science Department Relational Operators And Decisions

Computer Science Department

Multiple Statements in the IF body

if ( condition )

{

statement 1;

statement 2

.

.

statement n;

}

Page 8: Computer Science Department Relational Operators And Decisions

Computer Science Department

If … else statement• Sometimes we want to do one thing if the condition is

true and other if the condition is false. That’s where the if.. else statement is used.

if ( condition )

{

statement 1;

statement 2;

}

else

{

statement 1;

statement 2;

}

Page 9: Computer Science Department Relational Operators And Decisions

Computer Science Department

If..else Examplevoid main ( )

{

float b_salary, m_allowance, h_allowance;

cout<<“Enter Basic Salary”;

cin>>b_salary ;

if ( b_salary >= 50000 )

{

m_allowance = 40.0 / 100 * b_salary;

h_allowance = 30.0 / 100 * b_salary;

}

else

{

m_allowance = 30.0 / 100 * b_salary;

h_allowance = 20.0 / 100 * b_salary;

}

}

Page 10: Computer Science Department Relational Operators And Decisions

Computer Science Department

Nested if..else Statements• If we write the entire if..else statement within the body of if

statement or else statement. This is called nesting of ifs.

if ( condition )

{

if ( condition )

statement 1;

else

statement 2;

}

else

{

statement 3;

}

Page 11: Computer Science Department Relational Operators And Decisions

Computer Science Department

The else..if Constructionif (condition1)

statement 1;

else if (condition2 )

statement 2;

else if (condition3)

statement 3;

else if (condition4)

statement 4;

else

statement 5;

Page 12: Computer Science Department Relational Operators And Decisions

Computer Science Department

Else..if examplevoid main ( ){

int month;cout<<“ Enter Month in Number= ”cin>>month;if ( month == 1 )

cout<<“ January”;else if ( month == 2 )

cout << “ February ”;else if ( month == 3 )

cout<< “ March ”;..else

cout << “ December ”;}

Page 13: Computer Science Department Relational Operators And Decisions

Computer Science Department

LOGICAL OPERATORS

Page 14: Computer Science Department Relational Operators And Decisions

Computer Science Department

Logical Operators• These operator allows you to logically combines

variables or expressions.

• There are three logical operators– && (for AND Operation)

– || ( for OR Operation)

– ! ( for NOT Operation)

Page 15: Computer Science Department Relational Operators And Decisions

Computer Science Department

Logical AND Operator

• The AND operator ( && ) requires both relationships to be true in order for the overall result to be true. If either or both of the relationships are false, the entire result is false.

• Example

cout<<“Enter Marks”;

cin>> marks;

if ( ( marks > 100 ) && ( marks < 0 ) )

cout<< “ Marks are invalid”;

Page 16: Computer Science Department Relational Operators And Decisions

Computer Science Department

Logical OR Operator• The OR Operator ( || ) takes two logical expressions and

combines them. If either or both are true, the result is true. Both values must be false for the result to be false.

• Examplechar t_grade, p_grade;

cout << “Enter the Theory Grade: P for pass, F for Fail”;

cin>> t_grade;

cout<< “Enter the Practical Grade: P for pass, F for Fail”;

cin>> p_grade;

if ( ( t_grade == ‘F’ ) || ( p_grade == ‘F’ ) )

cout<<“Overall Fail”;

else

cout<<“Overall Pass”;

Page 17: Computer Science Department Relational Operators And Decisions

Computer Science Department

Logical NOT Operator

• The ( && ) and ( II ) operators always appears between two expressions. Also known as binary operators.

• The NOT operator ( ! ) is a unary operator.• It precedes a single logical expression and gives its

opposite as a result. • Example

if !( marks > 50)

is same as

if (marks <= 50)

Page 18: Computer Science Department Relational Operators And Decisions

Computer Science Department

SWITCH STATEMENT

Page 19: Computer Science Department Relational Operators And Decisions

Computer Science Department

Switch Statement• If you have a large decision tree, and all the decisions depend on

the value of the same variable, switch statement is suitable instead of if..else or else..if statement.

switch ( integer expression )

{

case constant 1:

statement 1;

case constant 2:

statement 2;

case constant 3:

statement 3;

default:

statement 4;

}

Page 20: Computer Science Department Relational Operators And Decisions

Computer Science Department

Switch Statement

• The integer expression following the keyword switch is an expression that will yield an integer value. It could be an integer constant like 1, 2,or 3, or an expression that evaluates to an integer.

• The keyword case is followed by an integer or a character constant. The constant in each case must be different from other.

Page 21: Computer Science Department Relational Operators And Decisions

Computer Science Department

Examplevoid main ( )

{

int i = 2;

switch (i)

{

case 1:

cout<<“case 1 \n”;

case 2:

cout<<“case 2 \n”;

case 3:

cout<<“case 3 \n”;

default:

cout<<“default \n”;

}

}

Output of the program

Case 2Case 3default

Page 22: Computer Science Department Relational Operators And Decisions

Computer Science Department

Break Statement• If we want that only particular case should be executed than break

statement should be used to get out of the control structure. int i = 2;switch (i){

case 1:cout<<“case 1 \n”;break;

case 2:cout<<“case 2 \n”;break;

case 3:cout<<“case 3 \n”;break;

default:cout<<“default \n”;

}

Page 23: Computer Science Department Relational Operators And Decisions

Computer Science Department

Tips of using Switch Statement

a) As in the previous examples the cases are arranged in ascending order 1,2,3 and default. You can in fact put the cases in any order you want.

b) You are also allowed to use char values in case and switch.

c) You can write a case in switch, which is not followed by any statement. As shown in the next example

Page 24: Computer Science Department Relational Operators And Decisions

Computer Science Department

Tips of using Switch Statementchar ch;cout<<“enter any of the alphabet a, b, or c”;cin>>chswitch(ch){

case ‘a’:case ‘A’:

cout<<“you entered a”;break;

case ‘b’:case ‘B’:

cout<<“you entered b”;break;

case ‘c’:case ‘C’:

cout<<“you entered c”;}

Page 25: Computer Science Department Relational Operators And Decisions

Computer Science Department

Tips of using Switch Statementd) Even if there are multiple statements to be executed in

each case there is no need to enclose these within a pair of braces.

e) If we have no default case, then the program simply falls through the entire switch and continues with the next instruction that follows the control structure.

f) The disadvantage of switch is that one cannot have a case in a switch which looks like

case i<=20;

All that we can have after the case is an int constant or a char constant. Even a float is not allowed.

Page 26: Computer Science Department Relational Operators And Decisions

Computer Science Department

Tips of using Switch Statementg) The break statement when used in a switch takes the

control outside the switch.

h) In principle, a switch may occur within another, but in practice it is rarely done.

i) The switch statement is very useful while writing menu driven programs.

Page 27: Computer Science Department Relational Operators And Decisions

Computer Science Department

The Conditional Operator

Page 28: Computer Science Department Relational Operators And Decisions

Computer Science Department

Conditional Operator

• The conditional operator ? and : are sometime called as ternary operators since they take three arguments

expression1 ? expression2 : expression3

• The above statement means that “ if expression1 is true (i.e. if its value is non-zero),

then the value returned will be expression2 otherwise the value returned will be expression3”

Page 29: Computer Science Department Relational Operators And Decisions

Computer Science Department

Example

int x, y;

cin>>x;

y = (x>5 ? 3 : 4);

cout<< y;

Page 30: Computer Science Department Relational Operators And Decisions

Computer Science Department

Points to be noted about Conditional Operators

• It is not necessary that the conditional operators should be used only in arithmetic statements

int i;

cin>> i;

(i==1 ? cout<<“ hello” : cout<<“helloworld”);

Page 31: Computer Science Department Relational Operators And Decisions

Computer Science Department

Points to be noted about Conditional Operators

• The conditional operators can be nested as shown below

int k, num = 30;

k=(num>5 ? (num <=10 ? 100 : 200) : 500);

cout<< k;

• The limitation of the conditional operator is that after the ? Or after the : only one C++ statement can occur.