Relational & Logical Operators, if and switch Statements

Slides:



Advertisements
Similar presentations
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Advertisements

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
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C++ for Engineers and Scientists Third Edition
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
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.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Flow of Control Part 1: Selection
1 Loops. 2 Topics The while Loop Program Versatility Sentinel Values and Priming Reads Checking User Input Using a while Loop Counter-Controlled (Definite)
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Branching statements.
CNG 140 C Programming (Lecture set 3)
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 3 Control Statements
Selections Java.
Control Statements: Part 1
CHAPTER 4 Selection CSEG1003 Introduction to Computing
EGR 2261 Unit 4 Control Structures I: Selection
Operator Precedence Operators Precedence Parentheses () unary
CSC113: Computer Programming (Theory = 03, Lab = 01)
Boolean Expressions and If
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Boolean Expressions & the ‘if’ Statement
Programming Fundamentals
Chapter 4: Making Decisions.
Chapter 6: Conditional Statements and Loops
JavaScript: Control Statements I
The C “switch” Statement
Relational & Logical Operators
The C “switch” Statement
Topics The if Statement The if-else Statement Comparing Strings
Relational and Logical Operators
Relational and Logical Operators
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
3 Control Statements:.
Chapter 4: Control Structures I (Selection)
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Relational and Logical Operators
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Relational and Logical Operators
Loops.
More Loops Topics Relational Operators Logical Operators for Loops.
Relational and Logical Operators
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Using C++ Arithmetic Operators and Control Structures
The switch Statement Topics Multiple Selection switch Statement
Relational and Logical Operators
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
UMBC CMSC 104 – Section 01, Fall 2016
Control Structures.
Presentation transcript:

Relational & Logical Operators, if and switch Statements

Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of if-else Statements switch Logical Operators and Expressions Truth Tables

Relational Operators Relational expressions evaluate to true or false. < 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 true or false. All of these operators are called binary operators because they take two expressions as operands.

Practice with Relational Expressions var a = 1, b = 2, c = 3 ; Expression true/false Expression true/false a < c a + b >= c b <= c a + b == c c <= a a != b a > b a + b != c b >= c

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.

Practice with Arithmetic Expressions var a = 1, b = 2, c = 3 ; var x = 3.33, y = 6.66 ; Expression Numeric Value True/False a + b b - 2 * a c - b - a c - a y - x y - 2 * x

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

Selection: the if statement if( condition ) { statement(s) // body of 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.

Examples if(age >= 18) { alert(“Go Vote!”); } if(value == 0) alert(“You entered zero.”);

Alert Screenshot <script type=“text/javascript”> <!-- var age = 18; if(age >= 18) { alert(“Go Vote!”); } //--> </script>

Good Programming Practice Always place braces around the body of an if statement. Advantages: Easier to read Will not forget to add the braces if you go back and add a second statement to the body Less likely to make a semantic error Indent the body of the if statement 2 to 3 spaces -- be consistent!

Selection: the if-else statement if( condition ) { statement(s) /* the if clause */ } else statement(s) /* the else clause */ Note that there is no condition for the else.

Example if ( age >= 18 ) { alert(“Go Vote!”) ; } else alert(“Maybe next time!”) ;

Another Example if(value == 0) { alert(“You entered zero.”); } else alert(“Value = ” + value);

Good Programming Practice Always place braces around the bodies of the if and else clauses of an if-else statement. Advantages: Easier to read Will not forget to add the braces if you go back and add a second statement to the clause Less likely to make a semantic error Indent the bodies of the if and else clauses 2 to 3 spaces -- be consistent!

Nesting of if-else Statements if(condition1) { statement(s) } else if(condition2) . . . /* more else if clauses may be here */ else statement(s) /* the default case */

Another Example if(value == 0) { alert(“You entered zero.”); } else if(value < 0) alert(value + “ is negative.”); else alert(value + “ is positive.”);

Gotcha! = versus == var a = 2; if(a = 1) /* semantic (logic) error! */ { alert(“a is one”); } else if(a == 2) alert(“a is two”); else alert(“a is ” + a);

Gotcha! = versus == The statement if (a = 1) is syntactically correct, so no error message will be produced. 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.

Multiple Selection with if (continued) if (day == 4) { alert (“Thursday”) ; } if (day == 5) { alert (“Friday”) ; if (day == 6) { alert (“Saturday”) ; if ((day < 0) || (day > 6)) { alert(“Error - invalid day.”) ; if (day == 0 ) { alert (“Sunday”) ; } if (day == 1 ) { alert (“Monday”) ; if (day == 2) { alert (“Tuesday”) ; if (day == 3) { alert (“Wednesday”) ;

Multiple Selection with if-else if (day == 0 ) { alert (“Sunday”) ; } else if (day == 1 ) { alert (“Monday”) ; } else if (day == 2) { alert (“Tuesday”) ; } else if (day == 3) { alert (“Wednesday”) ; } else if (day == 4) { alert (“Thursday”) ; } else if (day == 5) { alert (“Friday”) ; } else if (day == 6) { alert (“Saturday”) ; } else { alert (“Error - invalid day.”) ; } This if-else structure is more efficient than the corresponding if structure. Why?

The switch Multiple-Selection Structure switch ( expression ) { case value1 : statement(s) break ; case value2 : . . . default: : }

switch Example switch ( day ) { case 0: alert (“Sunday”) ; break ; case 1: alert (“Monday”) ; case 2: alert (“Tuesday”) ; case 3: alert (“Wednesday”) ; case 4: alert (“Thursday”) ; case 5: alert (“Friday”) ; case 6: alert (“Saturday”) ; default: alert (“Error -- invalid day.”) ; } Is this structure more efficient than the equivalent nested if-else structure?

switch Statement Details The last statement of each case in the switch should almost always be a break. The break causes program control to jump to the closing brace of the switch structure. Without the break, the code flows into the next case. This is almost never what you want. A switch statement will work without a default case, but always consider using one.

Good Programming Practices Include a default case to catch invalid data. Inform the user of the type of error that has occurred (e.g., “Error - invalid day.”). If appropriate, display the invalid value. If appropriate, terminate program execution (discussed in CMSC 201).

Why Use a switch Statement? A switch statement can be more efficient than an if-else. A switch statement may also be easier to read. Also, it is easier to add new cases to a switch statement than to a nested if-else structure.

Logical Operators || is OR if (z == 0 || x > 10) 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 AND if (x > 5 && y < 6) || is OR if (z == 0 || x > 10) ! is NOT if (!(bob > 42))

Example Use of && if(age < 1 && gender == ‘f’) { alert (“You have a baby girl!”); }

Truth Table for && Expression1 Expression2 Expression1 && Expression2 0 0 0 0 nonzero 0 nonzero 0 0 nonzero nonzero 1 Exp1 && Exp2 && … && Expn will evaluate to 1 (true) only if ALL subconditions are true.

Example Use of || if(grade == ‘D’ || grade == ‘F’) { alert (“See you next semester!”); }

Truth Table for || Expression1 Expression2 Expression1 || Expression2 0 0 0 0 nonzero 1 nonzero 0 1 nonzero nonzero 1 Exp1 && Exp2 && … && Expn will evaluate to 1 (true) if only ONE subcondition is true.

Example Use of ! if(!(age >= 18)) /*same as (age < 18)*/ { alert(“Sorry, you can’t vote.”); } else alert(“You can vote.”);

Truth Table for ! Expression ! Expression 0 1 nonzero 0

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 = right to left

Some Practice Expressions var a = 1, b = 0, c = 7; Expression Numeric Value True/False a b a + b a && b a || b !c !!c a && !b a < b && b < c a > b && b < c a >= b || b > c

More Practice Given var a = 3, b = 7, c = 21 ; 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 + 1 - b == 0) || (b = 5)