26
CSCI1402: Lecture 1 Week 6 CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61 email: [email protected]

CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61 email: [email protected]

  • View
    218

  • Download
    1

Embed Size (px)

Citation preview

CSCI1402: Lecture 1 Week 6CSCI1402: Lecture 1 Week 6

Dr. David A. Elizondo

Centre for Computational Intelligence

School of Computing

Office: Gateway 6.61

email: [email protected]

SelectionSelection

Sometimes we may want an instruction, or set of instructions, to be executed only if a particular condition is true.

An example is a simple computerized guessing game that would be suitable for small children. In the first version of the game, the program asks the player for a letter between A and Z. If the player chooses the same letter as the one stored in the program, a message “**Right** is displayed on the screen.

The logic of this program is as follows: Get a letter from the player at the keyboard IF this letter is equal to the letter stored in the

program THEN display **Right**

Selection – if statementSelection – if statement

The if conditional statement can execute selectively a part of a program.

if (condition) statement;

Here condition is a Boolean expression (true or false).

If condition is true, then the statement is executed.

If condition is false, then the statement is bypassed.

Selection – if statementSelection – if statement

Example:

if (10 < 11) System.out.println(“10 is less than 11”);

Since 10 < 11, the conditional expression is true, andprintln() will execute.

if (10 < 9) System.out.println(“This won’t be displayed”);

Since 10 is not < 9, the println will not be executed.

Selection – if statementSelection – if statement

Complete form of the if statement:

if(condition) statement;else statement;

The targets of the if and else are single statements

Selection – if statementSelection – if statement

General form of the if statement:

if(condition) {statement;

}else {

statement;}

The targets of both if and else are blocks of statements

If the conditional expression is true, the target of the if will be executed; otherwise, if it exists, the target of the else will be executed.

Selection – if statementSelection – if statement

if (number > = 0)if (number > = 0)

System.out.println(“+”);System.out.println(“+”);

else else

System.out.println(“-”);System.out.println(“-”);

write out +

write out -

true

false

number >= 0

Selection – if statementSelection – if statement

Example:public class Guess {

public static void main(String args[]) throws java.io.IOException {char ch, answer = ‘K’;System.out.println(“I’m thinking of a letter between A

and Z.”);

System.out.println(“Can you guess it: ”);ch = (char) System.in.read(); // read a character from the

KB

if (ch == answer) System.out.println(“*** RIGHT ***“);}

}

Selection – if statementSelection – if statement

This program prompts the player and then reads a character from the KB.

Using an if statement, it then checks that character against the answer, which is K in this case.

If K was entered, the message is displayed. When you try this program K must be entered in uppercase.

Selection – if statementSelection – if statement

Enhancing the if statement:public class Guess2 {

public static void main(String args[]) throws java.io.IOException {char ch, answer = ‘K’;System.out.println(“I’m thinking of a letter between

A and Z.”);

System.out.println(“Can you guess it: ”);ch = (char) System.in.read(); // read a character from

the KBif (ch == answer) System.out.println(“*** RIGHT ***“); else System.out.println(“... Sorry, you’re wrong.”);

}}

Nested ifsNested ifs

A nested if is an if statement that is the target of another if or else.

Nested ifs are very common in programming.

IMPORTANT:IMPORTANT: an else statement always refers to the nearest if statement that is within the same block as the else, and not already associated with an else.

Nested ifsNested ifs

Example:

if (i == 10) {if(j < 20)

a = b;if(k > 100)

c = d;else

a = c; // this else refers to if (k < 100)}else

a = d; // this else refers to if(i == 10) The final else is not associated with if (j < 20), because it is not

in the same block.

Nested ifsNested ifs Enhancements to the guess programpublic class Guess3 {

public static void main(String args[]) throws java.io.IOException {char ch, answer = ‘K’;System.out.println(“I’m thinking of a letter between A and

Z.”);System.out.println(“Can you guess it: ”);ch = (char) System.in.read(); // read a character from the KBif (ch == answer) System.out.println(“*** RIGHT ***“);

else {System.out.print(“... Sorry, you’re wrong.”);// a nested ifif (ch < answer) System.out.println(“too low”);

else System.out.println(“too high”);}

}}

Nested ifsNested ifs

Sample run of enhanced guess program:

I’m thinking of a letter between A and Z.Can you guess it: Z...Sorry, you’re too high

The if-else-if LadderThe if-else-if Ladder if-else-if ladder:

if (condition)statement;

else if (condition)statement;

else if (condition)statement;

.

.

.else

statement;

The if-else-if LadderThe if-else-if Ladder

The conditional expressions are evaluated from top downward.

As soon as a true condition is found, the statement associated with it is executed, and the rest of the ladder is bypassed.

If none of the conditions is true, the final else statement will be executed.

The final else often acts as the default condition: if all conditional tests fail, the last else statement is performed.

The if-else-if LadderThe if-else-if Ladder Example:public class Ladder {

public static void main(String args[]) {int x;for(x=0;x<6;x++) {

if(x==1)System.out.println(“x is one”);

else if(x==2)System.out.println(“x is two”);

else if(x==3)System.out.println(“x is three”);

else if(x==4)System.out.println(“x is four”);

elseSystem.out.println(“x is not between 1 and 4”);

}}

} Default statement

The if-else-if LadderThe if-else-if Ladder

Program output:

x is not between 1 and 4x is onex is twox is threex is fourx is not between 1 and 4

Default else statement is executed only if none of the preceding if statements succeeds.

Relational and Logical OperatorsRelational and Logical Operators

Relational operators: relationships than values can have with one another.

Logical operators: ways in which true and false values can be connected together.

Relational operators produce true and false results

Relational operatorsRelational operators

Operator Meaning

== (!!! Different than =)*

Equal to

!= Not equal to

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

Logical OperatorsLogical Operators

Operator Meaning

& AND

| OR

^ XOR

|| Short circuit OR

&& Short circuit AND

! NOT

Relational Operators ExampleRelational Operators Example

public class RelOps{public static void main(String args []) {

int i, j;i = 10;j = 11;if (i < j) System.out.println(“i < j”);if (i <= j) System.out.println(“i <= j”);if (i != j) System.out.println(“i != j”);if (i == j ) System.out.println(“this won’t execute”);if (i >= j) System.out.println(“this won’t execute”);if (i > j) System.out.println(“this won’t execute”);

}}

Relational Operators ExampleRelational Operators Example

Output of the program:

i < ji <= ji != j

Relational Operators ExampleRelational Operators Example

Output of the program:

i < ji <= ji != j

Logical Operators ExampleLogical Operators Example

public class LogOps{

public static void main(String args []) {

boolean b1, b2;

b1 = true;

b2 = false;

if (b1 & b2) System.out.println(“this won’t execute”);

if (!(b1 & b2)) System.out.println(“!(b1 & b2) is true”);

if (b1 | b2) System.out.println(“b1 | b2 is true”);

if (b1 ^ b2) System.out.println(“b1 ^ b2 is true”);

}

}

Logical Operators ExampleLogical Operators Example

Output of the program:

!(b1 & b2) is trueb1 | b2 is trueb1 ^ b2 is true