Computer Science Department Relational Operators And Decisions.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
True or false A variable of type char can hold the value 301. ( F )
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
C++ for Engineers and Scientists Third Edition
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection Structures (if & switch statements) (CS1123)
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CHAPTER 4 CONTROL STRUCTURES I Selection. In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Chapter 05 (Part III) Control Statements: Part II.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
TK 1914 : C++ Programming Control Structures I (Selection)
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Chapter 3 Selection Statements
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Programming Fundamentals
Selection Control Structure: Switch Case Statement
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Selection Control Structure
Presentation transcript:

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.

Computer Science Department Relational Operator MeaningOperator Greater than> Less than< Equal To== Not Equal To!= Greater Than or equal to>= Less Than or equal to<=

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.

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.

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( ); }

Computer Science Department Multiple Statements in the IF body if ( condition ) { statement 1; statement 2. statement n; }

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

Computer Science Department If..else Example void main ( ) { float b_salary, m_allowance, h_allowance; cout<<“Enter Basic Salary”; cin>>b_salary ; if ( b_salary >= ) { 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; }

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

Computer Science Department The else..if Construction if (condition1) statement 1; else if (condition2 ) statement 2; else if (condition3) statement 3; else if (condition4) statement 4; else statement 5;

Computer Science Department Else..if example void 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 ”; }

Computer Science Department LOGICAL OPERATORS

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)

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

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. Example char 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”;

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)

Computer Science Department SWITCH STATEMENT

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

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.

Computer Science Department Example void 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 2 Case 3 default

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

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

Computer Science Department Tips of using Switch Statement char ch; cout<<“enter any of the alphabet a, b, or c”; cin>>ch switch(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”; }

Computer Science Department Tips of using Switch Statement d)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.

Computer Science Department Tips of using Switch Statement g)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.

Computer Science Department The Conditional Operator

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”

Computer Science Department Example int x, y; cin>>x; y = (x>5 ? 3 : 4); cout<< y;

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

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.