school of computing science cmt1000 ed currie © middlesex university 1 cmt1000: introduction to...

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

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

School of Computing Science CMT1000Ed Currie

© Middlesex University

1

CMT1000: Introduction to Programming

Ed Currie

Lecture 5B:

Branch Statements -

Making Choices

School of Computing Science CMT1000Ed Currie

© Middlesex University

2

Overview

Making Choices Conditional expressions 2-branch conditionals

School of Computing Science CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed Currie

© Middlesex University

6

Semantics

• Pass mark?

School of Computing Science CMT1000Ed Currie

© Middlesex University

7

Exercise

• Write a program that inputs 2 values from the user and prints out the largest.

School of Computing Science CMT1000Ed 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 CMT1000Ed Currie

© Middlesex University

9

Boolean Expressions

We previously saw integer expressionse.g., (a + 5) * 7

They evaluate to an integer valuee.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 CMT1000Ed 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 CMT1000Ed 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. * / %

2. + -

3. < <= = > >=

e.g.

count + 6 < 8*b is evaluated as(count +6)< (8*b)

School of Computing Science CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed Currie

© Middlesex University

14

A PlanGet 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 CMT1000Ed Currie

© Middlesex University

15

int mark;char grade;

<CODE TO READ IN A MARK>

if (mark > 80) grade = ‘A’; else if (mark > 60) grade = ‘B’; else if (mark > 40) grade = ‘C’; else grade = ‘F’;

School of Computing Science CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed 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 CMT1000Ed Currie

© Middlesex University

24

A plan

Input the 2 marksIf (mark1 >= 40)

If mark2 >= 40Pass

OtherwiseFail

OtherwiseFail

School of Computing Science CMT1000Ed 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 CMT1000Ed Currie

© Middlesex University

26

In Java

if (mark1 >= 40) && (mark2 >= 40)

System.out.println(”Passed”);

else

System.out.println(”Failed”);

School of Computing Science CMT1000Ed Currie

© Middlesex University

27

The Logical Connectives

• Used to combine boolean expressions into larger boolean expressions

&& AND (a<=l) && (b>3)

|| OR (a==l) || (b==5)

! NOT !(a==l)&&(b==2)

School of Computing Science CMT1000Ed Currie

© Middlesex University

28

Precedence

1. ()

2. !

3. * / %

4. + -

5. < <= > >=

6. ++ !=

7. &&

8. ||

School of Computing Science CMT1000Ed 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 CMT1000Ed 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.