Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Introduction to Computers and Programming Lecture 7:
Introduction to Computers and Programming Lecture 5 New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Control structures: if-else statements and switch statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction.
Nested if statements When one if/else structure is contained inside another if/else structure is called a nested if/else. if (grade > 60) if (grade > 70)
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Logical Operators and Conditional statements
Chapter 4 Making Decisions
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Chapter 8 Conditionals. Learning Java through Alice © Daly and Wrigley Objectives List relational operators. List logical operators. Use the hierarchy.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Introduction to Computers and Programming Lecture 10: For Loops Professor: Evan Korth New York University.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
More on the Selection Structure
Selections Java.
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
Control Statements: Part 2
Control Structures: Part 2
Chapter 4: Making Decisions.
Chapter 3:Decision Structures
PROGRAM FLOWCHART Selection Statements.
Chap 7. Advanced Control Statements in Java
Controlling Program Flow
Presentation transcript:

Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University

Road Map if / else continued –Nested if / else statements Logical operators –&&, ||, ^ –! –&, | switch statement Reading: –Liang 5: chapter 2: 2.10;chapter 3: 3.2 –Liang 6: chapter 3: 3.3, 3.5

3 review What is the output of the following code fragment? int a = 100; if (a = 100) System.out.println (" a is equal to " + a); What is the output of the following code fragment? int a = 100, b = 1; if (a < b) System.out.println ("a is less than b");

4 review What is the output of the following code fragment? int a = 100; if (a != 100); System.out.println (" a is equal to " + a); What is the output of the following code fragment? int a = 100, b = 1; if (a < b) System.out.println ("a is less than b"); System.out.println ("Thank you");

Java Provides a shortcut if/else operator: This is Java’s only ternary operator (i.e. it takes three operands) System.out.println ( (sales > 100) ? "Federal Express" : "US Mail"); The conditional Statement. True Option False Option ? short- cut operator colon

nested if statements When one if/else structure is contained inside another if/else structure it is called a nested if/else. if (grade > 60) { if (grade > 70) System.out.println("You passed"); else System.out.println("You passed but need a tutor"); } else System.out.println("You failed");

else if Usually you try to nest within the else statement. Note the indentation. if (grade > 70) System.out.println("You passed"); else if (grade > 60) System.out.println("You passed but need a tutor"); else System.out.println("You failed");

8 Choosing the flavor of your if When you have multiple possible branches, you can have any of the following situations: 1.You want to execute exactly one of the branches 2.You want to execute zero or one of the branches 3.You want to execute zero or more branches Which would be appropriate for each of the situations above: a)A series of if s b)A series of if / else if s ending with an else if c)A series of if / else if s ending with an else 1 – c 2 – b 3 – a

9 Using boolean variables as flags You may want to use boolean variables to hold the value of a boolean expression. For example: boolean passed = (grade >= 65) Then you can use the variable later in a conditional statement: if (passed) System.out.println ("You passed"); else System.out.println ("You failed");

&& - Logical AND ((boolean exp a) && (boolean exp b)) When using the and operator (&&), both expression a and b must be true for the compound statement to be true. truth table For example: ((total > 50) && (status == 0))

|| - Logical OR ((boolean exp a) || (boolean exp b)) When using the or operator (||), at least one expression a or b must be true for the compound statement to be true. truth table For example: ((total > 50) || (status == 0))

^ - Exclusive OR ((boolean exp a) ^ (boolean exp b)) When using the exclusive or operator (^), at least one expression a or b must have opposite Boolean values for the compound statement to be true. truth table For example: ((person1 == 1) ^ (person2 == 1))

logical negation ! !(a) Reverses the truth or falsity of expression a Boolean expression ! has high precedence so you must use parenthesis You can avoid using the logical negation by expressing the condition differently with an appropriate relational operator. However, in the case of complex expressions, it is sometimes easier to use negation. Note: its a unary operator

14 Unconditional vs. Conditional Boolean Operators Java provides us with a second “and” operator and a second “or” operator. The unconditional operators guarantee that both expressions will be evaluated In this class, you should just use the conditional operators.

switch Multiple-Selection Structure Used when testing a variable or expression for EQUALITY (ie no >, =, <= tests) separately for each of the constant integral values it may assume. Preferred over if else in situations where you are testing the same expressions for equality with many different values. Allows you to perform different actions for each value.

switch Multiple-Selection Structure switch (expression){ case value1: action(s); break; case value2: action(s); break; … default: actions(s); break; } keyword switch could use more than one case; if the same actions are required expression can be a variable or a more complicated expression actions within a single case do not need brackets the default case will be executed in the event that no other case is

The switch Multiple-Selection Structure Flowchart of the switch structure true false case a case a action(s)break case b case b action(s)break false case z case z action(s)break true default action(s)  2000 Prentice Hall, Inc. All rights reserved.

beware of “fall through” If you forget to use the break keyword between cases, unexpected things may happen. Once a case tests true, all the statements following that case, will be executed until the next break. Experienced programmers may use this on purpose. For this class we will rarely use fall though.