Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Similar documents
JAVA OPERATORS GENERAL

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Prof. Navrati Saxena TA: Rochak Sachan

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Unit 3. Operators. School of Science and Technology INTRODUCTION

CT 229 Java Syntax Continued

Chapter 3: Operators, Expressions and Type Conversion

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Operators in C. Staff Incharge: S.Sasirekha

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Object Oriented Software Design

Arithmetic Operators. Portability: Printing Numbers

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Operators in java Operator operands.

Lecture Set 4: More About Methods and More About Operators

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Lecture 3 Operators MIT AITI

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

CMPT 125: Lecture 4 Conditionals and Loops

More Programming Constructs -- Introduction

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

3. Java - Language Constructs I

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Operators Questions

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

SECTION II: LANGUAGE BASICS

Introduction to C/C++ Lecture 3 - Program Flow Control

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Operators and Expressions

13 th Windsor Regional Secondary School Computer Programming Competition

SELECTION STATEMENTS:

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Lecture 3. More About C

Operators & Expressions

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Programming Language (2) Lecture (4) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Unit-2 (Operators) ANAND KR.SRIVASTAVA

The Java language has a wide variety of modifiers, including the following:

In this chapter, you will:

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Introduction to Computer Science Midterm 3 Fall, Points

Selection Statement ( if )

Lab1. Introduction to Python. Lab 4: Selection Statement. Eng. Mai Z. Alyazji

CMPT 125: Lecture 3 Data and Expressions

UNIT 3

CS1004: Intro to CS in Java, Spring 2005

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators. Java operators are classified into three categories:

Operators And Expressions

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Lecture Set 4: More About Methods and More About Operators

Chapter 3. Selections

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Following is the general form of a typical decision making structure found in most of the programming languages:

ECE 122 Engineering Problem Solving with Java

Selenium Class 9 - Java Operators

Chapter 4: Basic C Operators

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

CSC 1214: Object-Oriented Programming

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Constants and Variables

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

C++ Programming: From Problem Analysis to Program Design, Third Edition

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Lexical Considerations

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS111: PROGRAMMING LANGUAGE II

CT 229. Java Syntax 26/09/2006 CT229

A variable is a name that represents a value. For

3. Java - Language Constructs I

1 Short Answer (10 Points Each)

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

CA4003 Compiler Construction Assignment Language Definition

Decision Making -Branching. Class Incharge: S. Sasirekha

ANNAMACHARYA INSTITUTE OF TECHNOLOGY AND SCIENCES: : TIRUPATI

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

1.00 Lecture 4. Promotion

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Transcription:

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam ebtsamabd@gmail.com Department of Computer Science Najran University Ebtsam AbdelHakam 1

Outline 2 Operators Arithmetic Operators. Relational Operators. Boolean Logical Operators Assignment Operator The? Operator. Operator Precedence. Control Statements. If statement Nested ifs Ebtsam AbdelHakam

Arithmetic Operators Java operators can be divided into four groups: arithmetic, bitwise, relational, and logical. Ebtsam AbdelHakam 3

Arithmetic Operators Ebtsam AbdelHakam 4

Example 1 } } Ebtsam AbdelHakam 5

Example 1 Ebtsam AbdelHakam 6

Example 2 Ebtsam AbdelHakam 7

Prefix &Postfix operator 8 The increment operators used to adds 1 to previous value. Increment operator is unary operator because only one variable is used. There are 2 type in increment/decrement operator. 1. Postfix increment operator (variable++) 2. Prefix increment operator (++variable) Both prefix and postfix increment operator are same thing when they used independently(i++ or ++i) ie adds 1 to previous value. But they behave differently when they used in expression on the right hand side of assignment statement(num=i++ or num=++i) Ebtsam AbdelHakam

Postfix Example class group{ public static void main(string arng[]){ int num, i; i=0; i++; num=i++; System.out.println("i="+i); System.out.println("num="+num); } } Output:- i=2 num=1 Ebtsam AbdelHakam 9

Prefix Example class group{ public static void main(string arng[]){ int num, i; i=0; ++i; num=++i; System.out.println("i="+i); System.out.println("num="+num); } } Output:- i=2 num=2 Ebtsam AbdelHakam 10

Another Example Prefix: int count=1; System.out.println(++count); displays 2. Postfix: int count=1; System.out.println(count++); displays 1. Ebtsam AbdelHakam 11

Relational Operators The relational operators determine the relationship that one operand has to the other. Ebtsam AbdelHakam 12

Boolean Logical Operators The Boolean logical operators operate only on boolean operands. All of the binary logical operators combine two boolean values to form a resultant boolean value. Ebtsam AbdelHakam 13

Boolean Logical Operators Ebtsam AbdelHakam 14

Example 4 Ebtsam AbdelHakam 15

Example 4 Ebtsam AbdelHakam 16

The Assignment Operator The assignment operator is the single equal sign, =. It has this general form: var = expression; The type of var must be compatible with the type of expression. It allows you to create a chain of assignments. For example, consider this fragment: int x, y, z; x = y = z = 100; // set x, y, and z to 100 This fragment sets the variables x, y, and z to 100 using a single statement. Ebtsam AbdelHakam 17

The? Operator Java includes a special ternary (three-way) operator that can replace certain types of if-then-else statements. The? has this general form: expression1? expression2 : expression3 Here, expression1 can be any expression that evaluates to a boolean value (condition). If expression1 is true, then expression2 is evaluated; otherwise, expression3 is evaluated. The result of the? operation is that of the expression evaluated. Ebtsam AbdelHakam 18

Example 5 Ebtsam AbdelHakam 19

Operator Precedence Table 1 shows the order of precedence for operators, from highest to lowest. Ebtsam AbdelHakam 20

Operator precedence Evaluate the following expressions: 3+5/ 7 answer: 3 3*3 +3%2 answer: 10 3+2/5+ -2*4 answer: -5 3+(2/5)+ (-2*4) answer: -5 3*(3+3)%2 answer: 0 Ebtsam AbdelHakam 21

Control Statments Control statements are used to cause the flow of execution to advance and branch based on changes to the state of a program. Control Statments include: Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable. Iteration statements enable program execution to repeat one or more statements (loops). Jump statements allow your program to execute in a nonlinear fashion. Ebtsam AbdelHakam 22

Java s Selection Statements Java supports two selection statements: if and switch. if (condition) statement1; else statement2; For example: int a, b; if(a < b) a = 0; else b = 0; Ebtsam AbdelHakam 23

Nested ifs When you nest ifs, remember that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else. if(i == 10) { if(j < 20) a = b; } if(k > 100) c = d; // this if is else a = c; // astsociated with this else else a = d; // this else refers to if(i == 10) Ebtsam AbdelHakam 24

The if-else-if Ladder if(condition) statement; else if(condition) statement; else if(condition) statement;... else statement; The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. Ebtsam AbdelHakam 25

Example 6 Program that checks number is zero, positive or negative using if-else-if ladder class CheckSignNumberDemo { public static void main(string args[]) { int x = 10; if(x <= -1) System.out.println("x is a negative number having value " + x); else if(x == 0) System.out.println("x is a zero number having value " + x); else if(x > 0) System.out.println("x is a positive number having value " + x); } } Ebtsam AbdelHakam 26