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)

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

Software Engineering Table-driven methods General control issues.
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
Conditions and Decisions. Assignments Reading – Chapter 4 –
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
E-1 University of Washington Computer Programming I Lecture 6: Conditionals © 2000 UW CSE.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Programming Logic and Design Sixth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
The Selection Control Structure Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.
Decision Structures and Boolean Logic
Chapter 3 Making Decisions
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Chapter 1, Part II: Predicate Logic With Question/Answer Animations.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
EXPRESSIONS. Vocabulary A variable is a symbol, usually a letter, used to represent a number. –Example: 4x (x is the variable) A coefficient is the number.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
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.
Computer Science: A Structured Programming Approach Using C1 5-5 Incremental Development Part II In Chapter 4, we introduced the concept of incremental.
Control Statements: Part1  if, if…else, switch 1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
More on the Selection Structure
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Making Decisions.
Chapter 4 C Program Control Part II
Control Statements: Part 2
Data Types, Identifiers, and Expressions
Topics The if Statement The if-else Statement Comparing Strings
Programming Fundamentals
An Introduction to Programming with C++ Fifth Edition
Topics The if Statement The if-else Statement Comparing Strings
Data Types, Identifiers, and Expressions
And now for something completely different . . .
Chapter 3: Introduction to Problem Solving and Control Statements
Chapter 7 Conditional Statements
Expressions.
Control Statements Paritosh Srivastava.
Controlling Program Flow
Lecture 9: Implementing Complex Logic
Structural Program Development: If, If-Else
Presentation transcript:

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) printf(“You passed”); else printf(“You passed but need a tutor”); printf(“You failed”);

else if Usually you try to nest within the else statement. Note the indentation. if (grade > 70) printf(“You passed”); else if (grade > 60) printf(“You passed but need a tutor”); else printf(“You failed”);

&& - Logical AND (section 4.10) if ((total > 50) && (status == 0)) printf(“Your shipping will be free\n”); When using the and operator (&&), both expressions must be true for the compound statement to be true. truth table

|| - Logical OR (section 4.10) if ((total > 50) || (status == 0)) printf(“Your shipping will be free\n”); When using the or operator (||), at least one expression must be true for the compound statement to be true. truth table

more on && and || Can be used to reduce nesting && has higher precedence evaluation stops once truth or falsehood is known - in the example below, if semesterAverage > 90 finalExam will not get tested if ((semesterAverage > 90) || (finalExam > 90)) printf(“Student gets an a\n”);

logical negation ! (section 4.10) if !(grade == goal) printf(“You missed your goal\n”); ! has high precedence so you must use parenthesis In most cases you can avoid using the logical negation by expressing the condition differently with an appropriate relational operator. Note: its a unary operator

#define The #define preprocessor directive creates symbolic constants (it has another use we may talk about later) #define PI 3.14159 Would replace all subsequent occurrences of the symbolic constant PI with the numeric constant 3.14159, in the code

#define (cont’d) Used to avoid “magic numbers” in code Unlike variables, you should not change the value of a symbolic constant Should be used before your variable declarations Should be in ALL CAPS, except if you use Hungarian Notation e.g. #define iMY_CONSTANT 3600

Avoid magic numbers! Magic numbers are actual numbers you use throughout your code e.g. your divisor of 10000 If you need to change them, at worst it takes time to find all the occurrences, at worst you may miss a few If you use #define, you only need to change the value once at the top of your code E.g #define f_ACCEPTABLE_THRESHOLD .00001

reading We have already covered For next class chapter 1 chapter 2 sections 3.1 - 3.6 section 4.10, 4.11 section 13.3 For next class 4.7