CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.

Slides:



Advertisements
Similar presentations
CMSC 104, Version 9/011 The while Looping Structure Topics The while Loop Program Versatility o Sentinel Values and Priming Reads Checking User Input Using.
Advertisements

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
true (any other value but zero) false (zero) expression Statement 2
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Decision Structures and Boolean Logic
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
CPS120: Introduction to Computer Science Decision Making in Programs.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Exam 2 – Nov 18th Room ACIV 008. Project 2 Update  Your code needs to use loops to create the multiplication table. Hint: use nested for loop (Lecture.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Flow of Control Part 1: Selection
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Decision making If.. else statement.
UMBC CMSC 104 – Section 01, Fall 2016
- Standard C Statements
CSC113: Computer Programming (Theory = 03, Lab = 01)
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
Relational & Logical Operators
The while Looping Structure
Topics The if Statement The if-else Statement Comparing Strings
Relational and Logical Operators
Structured Program
Relational and Logical Operators
3 Control Statements:.
The while Looping Structure
Relational & Logical Operators, if and switch Statements
Relational and Logical Operators
Relational and Logical Operators
More Loops Topics Relational Operators Logical Operators for Loops.
Relational and Logical Operators
CprE 185: Intro to Problem Solving (using C)
Dale Roberts, Lecturer IUPUI
The while Looping Structure
Relational and Logical Operators
The while Looping Structure
Lecture 9: Implementing Complex Logic
Presentation transcript:

CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of if-else Statements Logical Operators and Expressions Truth Tables Reading Sections

CMSC 104, Version 8/062L11Relational&LogicalOps.ppt Relational Operators <less than >greater than <=less than or equal to >=greater than or equal to ==is equal to !=is not equal to Relational expressions evaluate to the integer values 1 (true) or 0 (false). All of these operators are called binary operators because they take two expressions as operands.

CMSC 104, Version 8/063L11Relational&LogicalOps.ppt Example if ( value == 0 ) { printf (“The value you entered was zero.\n”) ; printf(“Please try again.\n”) ; } else { printf (“Value = %d.\n”, value) ; }

CMSC 104, Version 8/064L11Relational&LogicalOps.ppt Operator Precedence and Associativity Precedence Associativity ( ) left to right/inside-out * / % left to right + (addition) - (subtraction) left to right >= left to right == != left to right

CMSC 104, Version 8/065L11Relational&LogicalOps.ppt Practice with Relational Expressions int a = 1, b = 2, c = 3 ; Expression Value Expression Value a = c b <= c a + b == c c <= a a != b a > b a + b != c b >= c

CMSC 104, Version 8/066L11Relational&LogicalOps.ppt Arithmetic Expressions: True or False Arithmetic expressions evaluate to numeric values. An arithmetic expression that has a value of zero is false. An arithmetic expression that has a value other than zero is true.

CMSC 104, Version 8/067L11Relational&LogicalOps.ppt Practice with Arithmetic Expressions int a = 1, b = 2, c = 3 ; float x = 3.33, y = 6.66 ; ExpressionNumeric Value True/False a + b b - 2 * a c - b - a c - a y - x y - 2 * x

CMSC 104, Version 8/068L11Relational&LogicalOps.ppt Review: Structured Programming All programs can be written in terms of only three control structures o The sequence structure –Unless otherwise directed, the statements are executed in the order in which they are written. o The selection structure –Used to choose among alternative courses of action. o The repetition structure –Allows an action to be repeated while some condition remains true.

CMSC 104, Version 8/069L11Relational&LogicalOps.ppt Selection: the if statement if ( condition ) { statement(s) /* body of the if statement */ } The braces are not required if the body contains only a single statement. However, they are a good idea and are required by the 104 C Coding Standards.

CMSC 104, Version 8/0610L11Relational&LogicalOps.ppt Examples if ( age >= 18 ) { printf(“Vote!\n”) ; } if ( value == 0 ) { printf (“The value you entered was zero.\n”) ; printf (“Please try again.\n”) ; }

CMSC 104, Version 8/0611L11Relational&LogicalOps.ppt Good Programming Practice Always place braces around the body of an if statement. Advantages: o Easier to read o Will not forget to add the braces if you go back and add a second statement to the body o Less likely to make a semantic error Indent the body of the if statement 3 to 5 spaces -- be consistent!

CMSC 104, Version 8/0612L11Relational&LogicalOps.ppt Selection: the if-else statement if ( condition ) { statement(s) /* the if clause */ } else { statement(s)/* the else clause */ }

CMSC 104, Version 8/0613L11Relational&LogicalOps.ppt Example if ( age >= 18 ) { printf(“Vote!\n”) ; } else { printf(“Maybe next time!\n”) ; }

CMSC 104, Version 8/0614L11Relational&LogicalOps.ppt Good Programming Practice Always place braces around the bodies of the if and else clauses of an if-else statement. Advantages: o Easier to read o Will not forget to add the braces if you go back and add a second statement to the clause o Less likely to make a semantic error Indent the bodies of the if and else clauses 3 to 5 spaces -- be consistent!

CMSC 104, Version 8/0615L11Relational&LogicalOps.ppt Nesting of if-else Statements if ( condition 1 ) { statement(s) } else if ( condition 2 ) { statement(s) }... /* more else clauses may be here */ else { statement(s) /* the default case */ }

CMSC 104, Version 8/0616L11Relational&LogicalOps.ppt Example if ( value == 0 ) { printf (“The value you entered was zero.\n”) ; } else if ( value < 0 ) { printf (“%d is negative.\n”, value) ; } else { printf (“%d is positive.\n”, value) ; }

CMSC 104, Version 8/0617L11Relational&LogicalOps.ppt Gotcha! = versus == int a = 2 ; if ( a = 1 ) /* semantic (logic) error! */ { printf (“a is one\n”) ; } else if ( a == 2 ) { printf (“a is two\n”) ; } else { printf (“a is %d\n”, a) ; }

CMSC 104, Version 8/0618L11Relational&LogicalOps.ppt Gotcha (con’t) The statement if (a = 1) is syntactically correct, so no error message will be produced. (Some compilers will produce a warning.) However, a semantic (logic) error will occur. An assignment expression has a value -- the value being assigned. In this case the value being assigned is 1, which is true. If the value being assigned was 0, then the expression would evaluate to 0, which is false. This is a VERY common error. So, if your if-else structure always executes the same, look for this typographical error.

CMSC 104, Version 8/0619L11Relational&LogicalOps.ppt Logical Operators So far we have seen only simple conditions. if ( count > 10 )... Sometimes we need to test multiple conditions in order to make a decision. Logical operators are used for combining simple conditions to make complex conditions. && is ANDif ( x > 5 && y < 6 ) || is ORif ( z == 0 || x > 10 ) ! is NOTif (! (bob > 42) )

CMSC 104, Version 8/0620L11Relational&LogicalOps.ppt Example Use of && if ( ( age < 1 ) && ( gender == ‘m’) ) { printf (“Infant boy\n”) ; }

CMSC 104, Version 8/0621L11Relational&LogicalOps.ppt Good Programming Practices Always use parenthesis when you have more than two operators!

CMSC 104, Version 8/0622L11Relational&LogicalOps.ppt Truth Table for && Expression 1 Expression 2 Expression 1 && Expression nonzero 0 nonzero 0 0 nonzero nonzero 1 Exp 1 && Exp 2 && … && Exp n will evaluate to 1 (true) only if ALL sub conditions are true.

CMSC 104, Version 8/0623L11Relational&LogicalOps.ppt Example Use of || if ( ( grade == ‘D’ ) || ( grade == ‘F’) ) { printf (“See you next semester!\n”) ; }

CMSC 104, Version 8/0624L11Relational&LogicalOps.ppt Truth Table for || Expression 1 Expression 2 Expression 1 || Expression nonzero 1 nonzero 0 1 nonzero nonzero 1 Exp 1 && Exp 2 && … && Exp n will evaluate to 1 (true) if only ONE sub condition is true.

CMSC 104, Version 8/0625L11Relational&LogicalOps.ppt Example Use of ! if ( ! (x == 2) ) /* same as (x != 2) */ { printf(“x is not equal to 2.\n”) ; }

CMSC 104, Version 8/0626L11Relational&LogicalOps.ppt Example #include int main( void ) { int x = 3; if ( ! (x == 2) ) /* same as (x != 2) */ { printf("1: x is not equal to 2.\n") ; } if ( x != 2 ) { printf("2: x is not equal to 2.\n") ; } if ( !x == 2 ) /* same as (x != 2) */ { printf("3: !x is equal to 2.\n") ; /* This is different! */ } return 0; }

CMSC 104, Version 8/0627L11Relational&LogicalOps.ppt Truth Table for ! Expression ! Expression 0 1 nonzero 0

CMSC 104, Version 8/0628L11Relational&LogicalOps.ppt Operator Precedence and Associativity Precedence Associativity ( ) left to right/inside-out * / % left to right + (addition) - (subtraction) left to right >= left to right == != left to right && left to right || left to right

CMSC 104, Version 8/0629L11Relational&LogicalOps.ppt Some Practice Expressions int a = 1, b = 0, c = 7; ExpressionNumeric ValueTrue/False a b c a + b a && b a || b !c !!c a && !b a < b && b < c a > b && b < c a >= b || b > c

CMSC 104, Version 8/0630L11Relational&LogicalOps.ppt More Practice Given int a = 5, b = 7, c = 17 ; evaluate each expression as True or False. 1. c / b == 2 2. c % b <= a % b 3. b + c / a != c - a 4. (b < c) && (c == 7) 5. (c b == 0) || (b = 5)