Download presentation
Presentation is loading. Please wait.
1
CSCI1402: Lecture 1 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61 email: elizondo@dmu.ac.uk
2
Selection 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**
3
Selection – 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.
4
Selection – if statement Example: if (10 < 11) System.out.println(“10 is less than 11”); Since 10 < 11, the conditional expression is true, and println() will execute. if (10 < 9) System.out.println(“This won’t be displayed”); Since 10 is not < 9, the println will not be executed.
5
Selection – if statement Complete form of the if statement: if(condition) statement; else statement; The targets of the if and else are single statements
6
Selection – 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.
7
Selection – if statement if (number > = 0) System.out.println(“+”);elseSystem.out.println(“-”); write out + write out - true false number >= 0
8
Selection – 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 ***“); }
9
Selection – 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.
10
Selection – 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 KB if (ch == answer) System.out.println(“*** RIGHT ***“); else System.out.println(“... Sorry, you’re wrong.”); }
11
Nested ifs A nested if is an if statement that is the target of another if or else. Nested if s 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.
12
Nested 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.
13
Nested ifs Enhancements to the guess program public 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 KB if (ch == answer) System.out.println(“*** RIGHT ***“); else { System.out.print(“... Sorry, you’re wrong.”); // a nested if if (ch < answer) System.out.println(“too low”); else System.out.println(“too high”); }
14
Nested 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
15
The if-else-if Ladder if-else-if ladder: if (condition) statement; else if (condition) statement; else if (condition) statement;. else statement;
16
The 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.
17
The 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”); else System.out.println(“x is not between 1 and 4”); } Default statement
18
The if-else-if Ladder Program output: x is not between 1 and 4 x is one x is two x is three x is four x is not between 1 and 4 Default else statement is executed only if none of the preceding if statements succeeds.
19
Relational 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
20
Relational operators OperatorMeaning == (!!! Different than =)*Equal to !=Not equal to >Greater than <Less than >=Greater than or equal <=Less than or equal
21
Logical Operators OperatorMeaning &AND |OR ^XOR ||Short circuit OR &&Short circuit AND !NOT
22
Relational 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”); }
23
Relational Operators Example Output of the program: i <j i <=j i !=j
24
Relational Operators Example Output of the program: i <j i <=j i !=j
25
Logical 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”); }
26
Logical Operators Example Output of the program: !(b1 & b2)is true b1 | b2is true b1 ^ b2is true
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.