School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.

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

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.
CS0007: Introduction to Computer Programming
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
1 Week 4 Selection (if..then..else), and Compound Conditions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 6: 1 Progress By now you should have: Worked through units 1 to 5 of the learning.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
The switch Statement, DecimalFormat, and Introduction to Looping
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
Computer Science Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
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.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Control Statements: Part1  if, if…else, switch 1.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Decision making If.. else statement.
Chapter 4: Control Structures I
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
Introduction to programming in java
Flow of Control.
SELECTION STATEMENTS (1)
Chapter 4: Control Structures I
OPERATORS (2) CSC 111.
Chapter 4: Control Structures I (Selection)
And now for something completely different . . .
If statement.
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CHAPTER 5: Control Flow Tools (if statement)
Life is Full of Alternatives
Lecture 4: Selection Control Structure
Presentation transcript:

School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making Choices

School of Computing Science CMT1000 Ed Currie © Middlesex University 2 Overview  Making Choices  Conditional expressions  2-branch conditionals

School of Computing Science CMT1000 Ed Currie © Middlesex University 3 A Problem Students have been given their grades (out of 100) and need to know whether they have passed (scored more than 40). Write a program that asks them for their mark and then tells them whether they have passed.

School of Computing Science CMT1000 Ed Currie © Middlesex University 4 A Plan 1. Ask the student for their mark 2. If it is greater than the pass mark then tell them they have passed. Otherwise tell them they have failed. 3. Thank them for using the system

School of Computing Science CMT1000 Ed Currie © Middlesex University 5 public static void passOrFail () { int mark; String numStr; numStr = JOptionPane.showInputDialog( "What is the mark?: " ); // convert from String to int mark = Integer.parseInt(numStr); if (mark > 40) JOptionPane.showMessageDialog(null, "Passed"); else JOptionPane.showMessageDialog(null, "Failed"); JOptionPane.showMessageDialog(null, "Bye"); }

School of Computing Science CMT1000 Ed Currie © Middlesex University 6 Semantics Pass mark?

School of Computing Science CMT1000 Ed Currie © Middlesex University 7 Exercise Write a program that inputs 2 values from the user and prints out the largest.

School of Computing Science CMT1000 Ed Currie © Middlesex University 8 Plan Input 2 values from the user if the first is largest then Print it. Otherwise Print the second.

School of Computing Science CMT1000 Ed Currie © Middlesex University 9 Boolean Expressions  We previously saw integer expressions e.g.,(a + 5) * 7  They evaluate to an integer value e.g.,-5, 0, 1, 42 …  A boolean expression is one which evaluates to a boolean value  true or false eg mark>40 count<100 m>(n+5)*2

School of Computing Science CMT1000 Ed Currie © Middlesex University 10 Relational Operators  Boolean expressions are made from the relational operators < less than 5<6 > greater than m>n <= less than or equal 5<=(a+1) >= greater than or equal a>=7 == equals (a+1)==(b+1) != not equals 5 !=6  Their operands are integer values

School of Computing Science CMT1000 Ed Currie © Middlesex University 11 Operator Precedence  Precedence determines the order that operations are evaluated  Like the “School” rule evaluate times before plus  Where are the invisible brackets?  The order is as below 0. () 1. * / % >= e.g. count + 6 < 8*b is evaluated as (count +6)< (8*b)

School of Computing Science CMT1000 Ed Currie © Middlesex University 12 If either branch of an if..then..else contains more than one statement then curly brackets are needed on that branch. (Compound statement) char result; if (mark>40) result = ‘P’; else { result = ‘F’; System.out.println(”Try again!"); }

School of Computing Science CMT1000 Ed Currie © Middlesex University 13 Layout What does this program fragment do? mark = 25; b = 4; c = 5; if (mark>40) a = b; else a = c; b = c;

School of Computing Science CMT1000 Ed Currie © Middlesex University 14 A Plan Get a mark from the user. If the mark is greater than 80 then The grade is A. Otherwise If the mark is greater than 60 then The grade is B. Otherwise If the mark is greater than 40 then The grade is C. Otherwise The grade is FAIL

School of Computing Science CMT1000 Ed Currie © Middlesex University 15 int mark; char grade; if (mark > 80) grade = ‘A’; else if (mark > 60) grade = ‘B’; else if (mark > 40) grade = ‘C’; else grade = ‘F’;

School of Computing Science CMT1000 Ed Currie © Middlesex University 16 Nesting Layout Note how this layout ‘creeps’ across the screen! An alternative way of laying out nested if statements which is sometimes clearer is as follows: if (mark > 80) grade = ‘A’; else if (mark > 60) grade = ‘B’; else if (mark > 40) grade = ‘C’; else grade = ‘F’;

School of Computing Science CMT1000 Ed Currie © Middlesex University 17 Layout Indent each branch of an if....then by the same amount With nested ifs, be consistent with indentation style. Put a blank line before and after each if statement to separate it out.

School of Computing Science CMT1000 Ed Currie © Middlesex University 18 Problem Write a program which inputs 2 values, storing the larger in variable a and the smaller in variable b.

School of Computing Science CMT1000 Ed Currie © Middlesex University 19 Solution Plan 1. Input value 1 into a. 2. Input value 2 into b. 3. if a is the largest then leave them alone else swap them round

School of Computing Science CMT1000 Ed Currie © Middlesex University 20 public static void largeSmall () { int a, b; String numStr; // Get the numbers numStr = JOptionPane.showInputDialog( ”Integer to go in a?: " ); a = Integer.parseInt(numStr); numStr = JOptionPane.showInputDialog( ”Integer to go in b?: " ); b = Integer.parseInt(numStr);

School of Computing Science CMT1000 Ed Currie © Middlesex University 21 if (a > b) JOptionPane.showMessageDialog(null, "a is already larger than b”); else //swap them round { int t; t = a; a = b; b = t; JOptionPane.showMessageDialog(null, "a is now larger than b”); }

School of Computing Science CMT1000 Ed Currie © Middlesex University 22 Local Declarations Wherever you put {} you can make local declarations of variables. The variables will only exist within the {....} –You cannot refer to them outside This helps avoid mistakes –changing a value unintentionally at the wrong point in the program. Local Declarations are GOOD; always declare variables as locally as possible

School of Computing Science CMT1000 Ed Currie © Middlesex University 23 Problem A student must pass both the coursework and exam to pass a module. Write a program to input the 2 marks and say if the student passed.

School of Computing Science CMT1000 Ed Currie © Middlesex University 24 A plan Input the 2 marks If (mark1 >= 40) If mark2 >= 40 Pass Otherwise Fail Otherwise Fail

School of Computing Science CMT1000 Ed Currie © Middlesex University 25 A different plan Input the 2 marks If mark1 >= 40 AND mark2 >= 40 then PASS otherwise FAIL

School of Computing Science CMT1000 Ed Currie © Middlesex University 26 In Java if (mark1 >= 40) && (mark2 >= 40) System.out.println(”Passed”); else System.out.println(”Failed”);

School of Computing Science CMT1000 Ed Currie © Middlesex University 27 The Logical Connectives Used to combine boolean expressions into larger boolean expressions &&AND(a 3) ||OR(a==l) || (b==5) !NOT!(a==l)&&(b==2)

School of Computing Science CMT1000 Ed Currie © Middlesex University 28 Precedence 1. () 2. ! 3.* / % >= != 7. && 8. ||

School of Computing Science CMT1000 Ed Currie © Middlesex University 29 Summary Decisions are made using if..else Either the true branch or the false branch is executed –depending on the value of a test –a boolean expression Boolean expressions are made from relational operators

School of Computing Science CMT1000 Ed Currie © Middlesex University 30 Summary (continued) If a branch has a single statement –then {} can be omitted –but be careful! Ifs can be nested –layout is then very important Local declarations can be made within compound statements Logical connectives often remove the need for multiple ifs.