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

Slides:



Advertisements
Similar presentations
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Advertisements

Logic & program control part 3: Compound selection structures.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
True or false A variable of type char can hold the value 301. ( F )
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
JAVA Control Statement.
UNIT II Decision Making And Branching Decision Making And Looping
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Programming Methodology (1). import java.util.*; public class FindCost3 { public static void main(String[] args ) { Scanner sc = new Scanner(System.in);
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Java Programming, Second Edition Chapter Five Input and Selection.
CPS120: Introduction to Computer Science Decision Making in Programs.
Flow of Control Part 1: Selection
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 5.  Conditionals  Booleans  Relational and Logical Operators  Switch statements.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
The for loop.
Statements and Control Flow The programs we have seen so far do exactly the same list of instructions every time What if we want to do different things.
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
The if-else StatementtMyn1 The if-else Statement It might be that we want to execute a particular statement or block of statements only when the condition.
CompSci 230 S Programming Techniques
Chapter 4: Control Structures I
CS0007: Introduction to Computer Programming
Decisions Chapter 4.
Control Structures.
Lecture 3- Decision Structures
Introduction to programming in java
Primitive Data, Variables, Loops (Maybe)
Condition Statements.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Repetition-Sentinel,Flag Loop/Do_While
مفاهیم اولیه زبان جاوا Java Basic Concepts
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Lecture Notes – Week 2 Lecture-2
Conditionals.
Chapter 2 Programming Basics.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Suggested self-checks:
CSC 1051 – Data Structures and Algorithms I
Conditionals and Loops
Presentation transcript:

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

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**

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.

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.

Selection – 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 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 statement if (number > = 0) System.out.println(“+”);elseSystem.out.println(“-”); write out + write out - true false number >= 0

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 ***“); }

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.

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.”); }

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.

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.

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”); }

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

The 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 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 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

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.

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

Relational operators OperatorMeaning == (!!! Different than =)*Equal to !=Not equal to >Greater than <Less than >=Greater than or equal <=Less than or equal

Logical Operators OperatorMeaning &AND |OR ^XOR ||Short circuit OR &&Short circuit AND !NOT

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”); }

Relational Operators Example  Output of the program: i <j i <=j i !=j

Relational Operators Example  Output of the program: i <j i <=j i !=j

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”); }

Logical Operators Example  Output of the program: !(b1 & b2)is true b1 | b2is true b1 ^ b2is true