03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
“Introduction to Programming With Java”
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CONTROLLING PROGRAM FLOW
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
1 2. Program Construction in Java. 2.4 Selection (decisions)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
Primitive variables Android Club types of variables: Primitive single value (starts with uppercase letter) Complex multiple value (starts with.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Programming in java Lecture-3 (a). Java Data Type TypeDescription byte8 bit signed integer short16 but signed integer int32 bit signed integer long64.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Operators.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Solution of Mid. Term 2009 Consider the following C++ declarations and assignments. int i, j, k ; float x, y ; char c ; bool test ; i = 35 ; j= 5 ; k =
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Relational Operator and Operations
Lecture 3 Java Operators.
CiS 260: App Dev I Chapter 4: Control Structures II.
Learning About the Loop Structure (continued)
OPERATORS (2) CSC 111.
Lecture Notes – Week 3 Lecture-2
Pages:51-59 Section: Control1 : decisions
Building Java Programs
Building Java Programs
CS2011 Introduction to Programming I Selections (I)
Building Java Programs
Building Java Programs
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Building Java Programs
Pages:51-59 Section: Control1 : decisions
Chap 7. Advanced Control Statements in Java
HNDIT11034 More Operators.
Presentation transcript:

03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe

03 August 2004 Increment Operator Decrement Operator Boolean Data Type Relational Operators Equality Operators Conditional Operators Selectional Constructs Contents

03 August 2004 i + + first use the value of i and then increment it by 1. ‘i + +’ equivalent to ‘i = (i)+1’. // postfix increment + + i first increment the value of i by 1 and then use it. ‘+ + i’ equivalent to ‘i = (i+1)’. // prefix increment int i=2; System.out.print(“ i = ”+ i++);//print 2, then i becomes 3 System.out.print(“ i = ”+ ++i);//add 1 to i, then print 4 Refer to incre.java Increment Operators

03 August 2004 Decrement Operators i - - first use the i ’s value and then decrement it by one i - - equivalent to (i)-1. // postfix decrement - - i first decrement i ’s value by one and then use it. - - i equivalent to (i-1). // prefix decrement int i=5; System.out.print(“i = ” + i--);//print 5, then i becomes 4 System.out.print(“i = ” + --i);//subtract 1 from i, then print 3 Refer to decre.java

03 August 2004 This data type can store only two values; true and false. Declaring a boolean variable is the same as declaring any other primitive data type like int, float, char. boolean response = false; //Valid boolean answer = true; //Valid boolean answer = 9943; //Invalid, boolean response = “false”; // Invalid, This is return type for relational & conditional operators. Refer to bool_op.java Boolean Data Type

03 August 2004 Relational Operators a < b a less than b. (true/false) a <= b a less than or equal b. (true/false) a > b a greater than b. (true/false) a >= b a greater than or equal to b. (true/false) These operations always return a boolean value. System.out.println(“23 is less than 65 ” +23<65); // true System.out.println(“5 is greater than or equal to 25.00?” + 5>=25.00); // false Refer to relate.java

03 August 2004 a = = b a equal to b. (true/false) a ! = b a not equal to b. (true/false) boolean equal = 12 = = 150; // false boolean again_equal = ‘r’= = ‘r’); // true boolean not_equal = 53!=90); // true Refer: equa.java Equality Operators

03 August 2004 A conditional operator is used to handle only two boolean expressions. Boolean expression always returns ‘true’ or ‘false’. Conditional AND ‘&&’ Return value is ‘true’ if both, x and y are true, else it is ‘false’. System.out.println(“x&&y ” + x&&y); // Refer cond_and.java Conditional OR ‘||’ return value is true if any one of x or y, is true else it is false. System.out.println(“x||y ” + x||y); // Refer cond_or.java Conditional Operators

03 August 2004 The ‘ if ’ construct It is used to select a certain set of instructions. It is used to decide whether this set is to be carried out, based on the condition in the parenthesis. Its syntax is: if ( ){ //body starts //body ends } The is evaluated first. If its value is true, then the statement(s) are executed. And then the rest of the program. Refer if_cond.java, if_cond1.java

03 August 2004 The ‘if else’ construct It is used to provide an alternative when the expression in if is false. Its syntax is: if( ){ } else{ } The if construct is the same. But when the expression inside if is false then else part is executed. Refer ifelse_cond.java

03 August 2004 Assignments int a_number=1; // (range: 1 to 5 including both) Print the value of a_number in word. For example, it should print “Four” if a_number contains 4. 1.Use equality ‘= =’ operator. 2.Do not use equality ‘= =’ operator.

03 August 2004 Thank You! End