The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

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.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
C++ for Engineers and Scientists Third Edition
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
The Selection Control Structure 1. 1.Module charts. 2. Flow-charts. 3. Selection. 4. Conditions.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 2. Program Construction in Java. 2.4 Selection (decisions)
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Decision Making and Branching (cont.)
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.
Decision Statements, Short- Circuit Evaluation, Errors.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Chapter 3 Control Statements
Making Choices with if Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
JavaScript: Control Statements.
JavaScript: Control Statements I
IF if (condition) { Process… }
Control Structures: Selection Statement
During the last lecture we had a discussion on Data Types, Variables & Operators
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 7 Conditional Statements
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Control Structures: Selection Statement
CSC215 Lecture Control Flow.
Presentation transcript:

The Selection Control Structure Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.

0. Questions on Program 3? §1) Note: you do not have to enter the date as a string—you can use separate, numeric, variables. §2) To check if something is evenly divisible, you can use modulus e.g. § if ((X % 100) == 0) Console.Write (X + “ is evenly divisible by 100”); 3) Check your answer for “normal” and “unusual” cases. Normal: 1996 (leap year) 1999 (Not a leap year). Unusual: 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years

1. Review. §The selection or branching code is implemented by the if statement. §if (condition) //antecedent § statement1; // consequent §else statement2; // optional alternate consequent §From last time: §Why is testing more complex once ifs are used? §How many times will we need to run the program?

Review (cont.). §What are conditions? §A condition is an expression which…. §Types. §The 3 types of condition are: §1. Relational (meaning?) §2. Logical (meaning?) §3. Boolean (meaning?)

2.Truth-tables. §How do we evaluate conditions? That is, how do we tell when they will be true, when false? §We need a method which shows every possible combination of T’s and F’s: Truth tables. §Let X, Y, Z stand for conditions. §E.g. X could be (Age >= 18) §Y could be (Count > 0), etc.

Truth-tables (cont.). §A.Negation (!). ! X § F T § T F §B.Conjunction (&&). X && Y § T T T § T F F § F F T § F F F

Truth-tables (cont.). §C.Disjunction (| |). X | | Y § T T T § T T F § F T T § F F F §What if we had: X && (Y | | Z)? §How many rows on the truth-table? §How do we know? Formula.

Truth-tables (cont.). §Equivalencies. §2 conditions are equivalent if, and only if: §They have the same pattern of T’s and F’s under their main connective (logical operator). §But what is “the main connective”? §Examples: §What is !(X | | Y) equivalent to? §What is !(X && Y) equivalent to?

Truth-tables (cont.). §Why does correct logic matter to the programmer? §Consider the following coding error: §if ((Grade != ‘A’) | | (Grade != ‘B’) | | (Grade != ‘C’) | | (Grade != ‘D’) ) Console.Write(“Fail”); Who fails? Why?

Truth-tables (cont.). §Therefore, we should code: §if ((Grade != ‘A’) && (Grade != ‘B’) && (Grade != ‘C’) && (Grade != ‘D’) ) Console.Write (“Fail”); §Precedence of logical operators: §1) ! §2) && §3) | |

Full precedence. §1) ! + - (as one-place operators) §2) *, /, % §3) +, - (as binary operators) §4), >= §5) ==, != §6) && §7) || §8) = (assignment)

Truth-tables (cont.). §Short-circuit evaluation. §When the compiler evaluates A | | B it can stop with A if……. §But it will have to check both if….. §When the compiler evaluates A && B it can stop with A if……. §But it will have to check both if…..

3. Forms of the if statement. §A.Simple if (no else, no blocks). §if (Month_Number = = 1) Console.Write (“January”); B. if…else. §if (Count ! = 0) Average = Total / Count; else Average = 0;

Forms of the if statement (cont.). §C.Compound/block ifs. §These allow the use of compound statements = a series of statements enclosed in { } §Can be used in antecedent/consequent. §if (Gender = = ‘F’) { // begin female § Female++; § Console.Write ( “Female”); §} // end female

Forms of the if statement (cont.). §Compound/block ifs (cont.). §Likewise §else {// begin male § Male++; § Console.Write (“Male”); §} // end male §Note use of commenting

Forms of the if statement (cont.). §D.Nested ifs. §An if inside an if (the guard in Monty Python and the Holy Grail: if,if, if,…er..) §if (Age >5) if (Age <= 18) Console.Write (“School”); else Console.Write (“College or job”);

Forms of the if statement (cont.). §Nested ifs (cont.). §What does the else match with? Why? §E.The problem of the dangling else. §if (Grade != ‘F’) if (Grade == ‘D’) Console.Write( “Academic probation”); else Console.Write(“Failing.”);

Forms of the if statement (cont.). §Who will fail? §Why? §Solution? Terminate nested if. §if (Grade != ‘F’) { if (Grade == ‘D’) Console.Write( “Academic probation”); } //end else Console.Write(“Failing.”);

4. The switch statement. §See example: Switch.cs §Note 1: switch condition and case label list §Note 2: break. What does it do? §Note 3: default. Why? §Limitation of switch? Only designed for ordinal values. Meaning?