Control Flow

13
Control Flow Lecture 6

Transcript of Control Flow

Control Flow

Lecture 6

Comparison Operators • Controlling the flow of execution is perhaps the most important

aspects of any programming language. It allows you to direct the computer in different directions depending on conditions.

• The outcome of the operators is a Boolean value (true or false). • The comparison operators are normally used in the expression that

control the if statement and the various loops statements. Example : int a = 4;

int b = 5; boolean c = a<b;

In this case the result of a < b (which is false) is stored in the variable c. This can be used in a conditional statement such as if (c == 0) { execute }

Lecture 6

x and y are boolean types

x and y can be expressions that result in a boolean value.

Result is a boolean true or false value.

x && y Conditional

AND

If both x and y are true, result is true.

If either x or y are false, the result is false

If x is false, y is not evaluated.

x & y Boolean

AND

If both x and y are true,the result is true.

If either x or y are false, the result is false

Both x and y are evaluated before the test.

x || y Conditional

OR

If either x or y are true, the result is true.

If x is true, y is not evaluated.

x | y Boolean

OR

If either x or y are true, the result is true.

Both x & y are evaluated before the test.

!x Boolean

NOT

If x is true, the result is false.

If x is false, the result is true.

x ^ y Boolean

XOR

If x is true and y is false, the result is true.

If x is false and y is true, the result is true.

Otherwise, the result is false.

Both x and y are evaluated before the test.

Lecture 6

= Assignment As in if = true

== Equality This produces a true if the two boolean operands

have the same value (true or false). It produces

false otherwise. Thisis equivalent to NXOR

!= Inequality This produces true if the two boolean operands have

different values (one true, the other false). It

produces false otherwise. This is equivalent to XOR

Relational Operators

< Less than

>= Less than or equal to

Greater than

> = Greater than or equal to

Example: public static void main(String[] args) throws NumberFormatException, IOException { // TODO code application logic here boolean a = false; boolean b = true; boolean c = a | b; boolean d = a & b; boolean e = a ^ b; boolean f = (!a & b) | (a & !b); boolean g = !a; System.out.println("a is: " + a); System.out.println("b is: " + b); System.out.println("a ! b is: " + c); System.out.println("a & b is: " + d); System.out.println("a ^ b is: " + e); System.out.println("(!a & b) | (a |b) is: " + f); System.out.println("!a is: " + g); }

Lecture 6

Operator Precedence

Operator Precedence

(), [ ], high

*, /, % medium

+, -

low

= very low

Lecture 6

The sequence in which an operator is executed can be confusing –Consider the following example –What does this equal?

int y = 10 + 6 / 3– 1; A) 8

B) 11 We have 2 answers. So we use Operator Precedence

Operators with Higher precedence are executed before those of a lower precedence

Now, int y = 10 + (6/ 3) – 1;

If statement

Lecture 8

Conditional constructs

• Provide – Ability to control whether a statement list is executed

• Two constructs

– If statement

• if

• if-else

• if-else-if {nested if statement}

– Switch statement

Lecture 8

Basic if statement • Syntax

if (Expression)

Action

• If the Expression is true then execute Action

• Action is – a single statement

– or a group of statements within braces {block}

Expression

Action

true false

Lecture 8

If statement Example

if (result < 0) {

result = -result;

}

result < 0

result = -result

true false

Is our result

< 0 ?

If result is not

Less than 0

then result is

fine as is

If result is less than

0 then we need to update

result to its additive inverse

Our number is now

definitely

Positive

Lecture 8

The if-else statement • Syntax

if (Expression)

Action1 else Action2

• If Expression is true then execute Action1 otherwise execute Action2

• The actions are either a single statement or a list of statements within braces

Expression

Action1 Action2

true false

If

Else

Lecture 8

If-else-if

• Consider this

if (number == 0) {

System.out.println("zero");

}

else {

if (number > 0) {

System.out.println("positive");

}

else {

System.out.println("negative");

}

}

Lecture 9

If-else-if

• Better to write like this

if (number == 0) {

System.out.println("zero");

}

else if (number > 0) {

System.out.println("positive");

}

else {

System.out.println("negative");

}

• Same results as previous segment – but this segment better expresses the meaning of what is going on

Lecture 9