26/10/20151 2.3 Selection Nested If structures & Complex Multiple Conditions.

Slides:



Advertisements
Similar presentations
5.04 Apply Decision Making Structures
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
30/04/ Selection Nested If structures & Complex Multiple Conditions.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
06 Testing selection1June Testing selection CE : Fundamental Programming Techniques.
CP1020 Week 5 Selection Continued. CP1020 University of Wolverhampton - Steve Garner and Ian Coulson if then else zWe can use if then else statements.
Making Decisions In Python
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Do it now activity Correct the 8 syntax errors: Age = input(“How old are you?” If age = 10 Print(You are 10”) else: print(“You are not 10”)
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
Fundamental Programming Fundamental Programming More on Selection.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
30/10/ Iteration Loops Do While (condition is true) … Loop.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
22/11/ Selection If selection construct.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
31/01/ Selection If selection construct.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Controlling Program Flow with Decision Structures.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
02/03/ Strings Left, Right and Trim. 202/03/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
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.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
Conditional or Decision Logic
VB.Net Programming Console Application
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Javascript Conditionals.
JavaScript: Control Statements I
CSC115 Introduction to Computer Programming
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Control Statement Examples
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
Chapter 3: Introduction to Problem Solving and Control Statements
If selection construct
Do While (condition is true) … Loop
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
If selection construct
Do … Loop Until (condition is true)
Text / Serial / Sequential Files
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
The Selection Structure
Review of Previous Lesson
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

26/10/ Selection Nested If structures & Complex Multiple Conditions

2 Learning Objectives State what is needed for each If statement in Nested If Structures. State which has precedence: And / Or Boolean statements and note that this can cause problems in complex multiple conditions.

3 Nested If Structures An alternative to using multiple And conditions.

4 Multiple And Boolean Statement So instead of using a multiple And condition like:

5 Multiple And Boolean Statement Dim Age As Integer Dim Gender As String Age = Console.ReadLine Gender = Console.ReadLine If (Age >= 18) And (Gender = “F”) Then ‘ Female ‘ 18 and over? Console.WriteLine(“Allow into nightclub.”) Console.WriteLine(“Allow into nightclub.”) Else ‘ Everybody else Console.WriteLine(“Do not allow into nightclub.”) Console.WriteLine(“Do not allow into nightclub.”) End If

6 Nested If Structure We could use:

7 Nested If Structure Dim Age As Integer Dim Gender As String Age = Console.ReadLine Gender = Console.ReadLine If Age >= 18 Then ‘Aged 18 or over? If Gender = “F” Then ‘and female? If Gender = “F” Then ‘and female? Console.WriteLine(“Allow into nightclub.”) Else ‘All other people. Else ‘All other people. Console.WriteLine(“Do not allow into nightclub!”) End If End If End If 12

8 Nested If Structures We can also replace ElseIf structures with nested If structures.

9 ElseIf structures So instead of:

10 ElseIf structures Dim Mark As Integer Mark = Console.ReadLine If Mark >=60 Then ‘Mark 60 or more? Console.WriteLine(“Merit”) Console.WriteLine(“Merit”) ElseIf Mark >= 40 Then ‘Mark ? Console.WriteLine(“Pass”) Console.WriteLine(“Pass”) Else ‘Mark under 40. Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) End If

11 Nested If Structures We can use:

12 Nested If Structures If Mark >=60 Then ‘Mark 60 or more? Console.WriteLine(“Merit”) Console.WriteLine(“Merit”)Else If Mark >= 40 Then ‘Mark ? If Mark >= 40 Then ‘Mark ?Console.WriteLine(“Pass”) Else ‘Mark under 40. Else ‘Mark under 40. Console.WriteLine(“A mark of “ & Mark & “ is a fail.”) End If End If End If 1 2

13 Nested If Structures Note: If you use nested If structures you need to remember to use an End If statement for each If statement. If you use nested If structures you need to remember to use an End If statement for each If statement.

14 Program 2.3a Rent a property Specification: Illustrate complex multiple conditions: Illustrate complex multiple conditions: More specifically to illustrate the order of precedence of the And & Or logical operators. A customer wants to rent a holiday property which has 4 or more bedrooms, and it must be a cottage or a detached house. A customer wants to rent a holiday property which has 4 or more bedrooms, and it must be a cottage or a detached house.

15 Program 2.3a Rent a property Dim Type As String Dim Bedrooms As Integer Console.WriteLine(“Please enter the property type.”) Type = Console.ReadLine Console.WriteLine(“Please enter the number of bedrooms.”) Bedrooms = Console.ReadLine If Type = "Cottage" Or Type = "Detached" And Bedrooms >= 4 Then Console.WriteLine("Rent it!") Console.WriteLine("Rent it!")Else Console.WriteLine("Don’t rent it!") Console.WriteLine("Don’t rent it!") End If

16 Program 2.3a Rent a property Run the program and test all possibilities. You should find a problem! What is it?

17 Program 2.3a Rent a property You should have found that if it is a cottage and the number of bedrooms is lower than 4 the program still says ‘Rent it!’.

18 Program 2.3a Rent a property This is because And is done before Or. So the program interprets the code like this: Type = "Cottage" Type = "Cottage" Or Or Type = "Detached" And Bedrooms >= 4 Type = "Detached" And Bedrooms >= 4 So if it is Detached then this works as both the type and the number of bedrooms has to be true but if it is a cottage then the type or the number of bedrooms is required to be true; so it doesn’t work.

19 Program 2.3a Rent a property Replace the previous code with: If (Type = "Cottage" And Bedrooms >= 4) Or (Type = "Detached" And Bedrooms >= 4) Then If (Type = "Cottage" And Bedrooms >= 4) Or (Type = "Detached" And Bedrooms >= 4) Then Console.WriteLine ("Rent it!") Else Else Console.WriteLine ("Don’t rent it!") End If End If

20 Program 2.3a Rent a property Run the program and test all possibilities. It should work perfectly now.

21 Extension “Deciding Exam Grades” Program 2.3b Change the “Deciding Exam Grades” Program written in 2.1 / 2.2 Selection Use a logical expression that is true when the mark is within the range and false when the mark is outside the range of 0 – 100 and. Use a logical expression that is true when the mark is within the range and false when the mark is outside the range of 0 – 100 and. Therefore use one IF statement with an initial IF test with And, to test if the mark is valid proceeded with a nested IF statement to produce the appropriate valid messages: Merit (60 or more), Pass (40 – 59), Fail (under 40). With a final “Else” to produce the general error message: With a final “Else” to produce the general error message: “You have entered an invalid mark!” Note: Note: Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Nested If Version 2.3b).

22 Plenary What is needed for each If statement in Nested If Structures? End If End If Which has precedence: And / Or? And And