Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
The If/Else Statement, Boolean Flags, and Menus Page 180
Switch Statements. Switch Statement Often you want to do a series of tests –if i==0 … else if i==1 …. else if i==2 … else if i==3 …. C++ provides the.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Completing the Basic (L06)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CONTROLLING PROGRAM FLOW
CHAPTER 4 CONTROL STRUCTURES I Selection. In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
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.
 for loop  while loop  do-while loop for (begin point; end point ; incrementation ) { //statements to be repeated }
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Selection Control Structures 2 (L09) * Nested if Statements * The if-else Chain * Exercise: if, if-else, nested if, and if-else chain Selection Control.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Switch Statements Comparing Exact Values
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,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Rational Expressions relational operators logical operators order of precedence.
A First Book of C++ Chapter 4 Selection.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
The Ohio State University
Chapter 3 Selection Statements
Chapter 3 Control Statements
Rational Expressions. relational operators. logical operators
Relational Operators Operator Meaning < Less than > Greater than
Chapter 7 Conditional Statements
Decision making If statement.
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Chapter 4: Control Structures I (Selection)
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
CS 101 First Exam Review.
HNDIT11034 More Operators.
Presentation transcript:

Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang

Relational Expression * Operand Relational Operator Operand price < * Expression Examples age > 40 length <= 50 3 < 4 flag == done Selection Control Structure Dr. Ming Zhang

Invalid Relational Expression *Operator out of order length =< 50 * Invalid operator 3 >> 4 * Space are not allowed flag = = done * Assignment operator or relations operator ?? flag = done Selection Control Structure Dr. Ming Zhang

Relational Operators Operator Meaning Example < Less thanage < 30 >Greater than hight > 6.2 <=Less than/equal totaxtable<=20 >=Greater than/equal totemp>= 98.6 ==Equal to grade == 100 !=Not equal tonumber !=250 Selection Control Structure Dr. Ming Zhang

Value of Relational Operator Express Expression value Interpretation ‘A’ >’C’ 0 false ‘D’ <=‘Z’1true ‘E’==‘F’0false ‘G’ >=‘M’0 false ‘B’ !=‘C’1 true 0: false condition 1: true condition Selection Control Structure Dr. Ming Zhang

Logical Operators * Logical Operators &&: Logical operator AND ||: Logical operator OR !: Logical operator NOT * Examples - (age > 40) && (term < 10) is true only if age is greater than 40 and term is less (age > 40) || (term < 10) will be true if either age is greater than 40, term is less 10, or both condition are true. Selection Control Structure Dr. Ming Zhang

Logical Operator Expression Let: a=12.0, b=2.0, i=15, j=30, complete=0.0 Expression Value Interpretation a>b1True (i==j) || (a<b) || complete0False (a/b>5) && (i<=20)1True Selection Control Structure Dr. Ming Zhang

Precedence of Logical Operators OperatorAssociativity !Unary Right to left */%Left to right +-Left to right >=Left to right == !=Left to right &&Left to right ||Left to right = += -=*=/=Right to left Selection Control Structure Dr. Ming Zhang

Examples of Logical Operators Let: char key = ‘m’; int i = 5, j =7, k = 12; double x = 22.5 Expression Equivalent Expression Value i+2==k-1(i+2)==(k-1)? i+2*j>k(i+(2*j))>k? ‘a’+1==‘b’(‘a’+1)==‘b’? 25>+x+1.025>+(x+1.0)? Selection Control Structure Dr. Ming Zhang

Exercise: Logical Operators Let: char key = ‘A’; int i = 4, j =6, k = 9; double x = 12.5 Expression Equivalent Expression Value i+2==k-3(i+2)==(k-3)? i+2*j<k(i+(2*j))<k? ‘a’+1!=‘b’(‘a’+1)!=‘b’? 25<+x+1.025<+(x+1.0)? Selection Control Structure Dr. Ming Zhang

Numerical Accuracy Problem *Many decimal numbers, such as 0.1, for example, can not be represented exactly in binary using a finite number of bits. Thus testing for exactly equality for such numbers can be fail. * When equality of noninteger values is desired, it is better to require that the absolute value of the difference between operands be less than some extremely small number. Do not use relational operator ==. Selection Control Structure Dr. Ming Zhang

Examples of Numerical Accuracy * float operand1, operand2; operand1==operand2;/*do not use == here*/ /* using folloing condition to instead of */ fabs(operand1 - operand2) < ; * float x, y; x/y == 0.35; /* do not use == here */ /* using folloing condition to instead of */ fabs(x/y ) < ; Selection Control Structure Dr. Ming Zhang

If Statement * Syntax of if Statement if (condition) statement executed if condition is “true” * General Form of if Statement if (expression) statement; Selection Control Structure Dr. Ming Zhang

Example of If Statement * If student’s grade is greater than or equal to 60 Print “Passed” * if ( grade >= 60) cout << “passed” << endl; * grade>=60 true print “passed” false Selection Control Structure Dr. Ming Zhang

If-else Statement * Syntax of if-else Statement if (condition) statement executed if condition id “true” else statement executed if condition is “false” * General Form of if-else Statement if (expression) statement1; else statement2; Selection Control Structure Dr. Ming Zhang

Flowcharting if/else Structure false grade>= 60 true print “failed” print “passed” Selection Control Structure Dr. Ming Zhang

Exercise: Selection Structure (C++) #include using std::cout; using std::cin;using std::endl; int main( ) {int taxable; float taxes; cout << “Please type in the taxable income:”: cin >> taxable; if(taxable <=20000) taxes = 0.02*taxable; else taxes = 0.025*(taxable-20000) ; cout << “Taxes are $”<< taxes) << endl; return(0); } Selection Control Structure Dr. Ming Zhang