2. Java language basics (2)

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

Control Structures.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Java Syntax Primitive data types Operators Control statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
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.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Control statements Mostafa Abdallah
Application development with Java Lecture 6 Rina Zviel-Girshin.
Decision Statements, Short- Circuit Evaluation, Errors.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Learning Javascript From Mr Saem
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
2. Java language basics (1)
G. Pullaiah College of Engineering and Technology
If/Else Statements.
Lecture 3 Java Operators.
Selection (also known as Branching) Jumail Bin Taliba by
Object Oriented Programming
Control Statements: Part 2
Assignment and Arithmetic expressions
Lecturer CS & IT Department UOS MBDIN
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Type Conversion, Constants, and the String Object
Incrementing ITP © Ron Poet Lecture 8.
Chapter 2.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Programming in Java
MSIS 655 Advanced Business Applications Programming
The University of Texas – Pan American
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises UTPA – Fall 2012 This set of slides is revised from.
Chapter 6 Control Statements: Part 2
Differences between Java and C
Expressions and Assignment
SELECTIONS STATEMENTS
Conditionals.
The Java switch Statement
Loops CGS3416 Spring 2019 Lecture 7.
Controlling Program Flow
Problem 1 Given n, calculate 2n
Presentation transcript:

2. Java language basics (2) Minhaeng Lee

Contents Casting and conversion Conditional Statement (if) Loop Switch Practice (Multiplication Table!) Homework

Casting Data transfer between two different types E.g. from integer to float / double Use “(<type>)” before source variable E.g. double target = (double) iAmInteger; Be careful when bigger one to smaller one Data loss expected Float (3.4) to Integer (3) E.g. casting from double to short 3.141592 (double)  3 (int)

Casting (practice) int a; float b; double c; boolean d; int to double : (double) a double to float : (float)c float to int : (int) b int to boolean : (boolean) a (not possible)

Useful Class Types String From other types to String Use double quotation “” Can store sequence of characters Concatenation using + LONG = “small” + “small”; (LONG = “smallsmall”) From other types to String Use toString Every class have Every class can be printed as String type

Conversion Inbuilt explicit method in Java From String to number Integer to String String “1000” to Integer 1000 From String to number <Integer/Float/Double>.parse<Int/Float/Double>(); e.g. double d = Double.parseDouble(stringValue); From number to String Use toString() that every class have

Conversion (practice) int to String Integer.toString(value) float to String Float.toString(value) double to String Double.toString(value) String to int Integer.parseInt(stringValue) String to float Float.parseInt(floatValue) String to double Double.parseInt(doubleValue) Integer to int (int)IntegerValue Float to float (float)FloatValue Double to double (double)DoubleValue

Math Notation Operator Math functions + : plus - : minus * : multiplication / : division % : remaining values (4%3 : 1, 10%2 : 0) Math functions Math.<something>(); Math.sqrt(var); Math.round(var); Math.ceil(var); Math.floor(var); Math.pow(var, var); Etc.

Short notation ++, -- +=, *=, /=, -= Used in C, C++, Java a++ : a = a + 1 a-- : a = a - 1 +=, *=, /=, -= a+=1 : a = a + 1 A*=10 : A = A * 10 …

Quick practice

Boolean type Should be true or false 0 = false, otherwise = true but specific boolean result is suggested Can compare only two compatible variables

Boolean Operators

http://faculty. uoit. ca/sartipi/courses/OO/f12/2 http://faculty.uoit.ca/sartipi/courses/OO/f12/2.slides/ch05-decisions/ch05-HTML/ch05/images/boolean_operators.png

If, If else, and else If(<condition>) <statement>; If given <condition> is true, then execute only one <statement> if (<condition>) { <statements> } If given <condition> is true, then execute <statements> if (<condition>){ <statement1> } else{ <statements2> } If given <condition1> is true, then execute <statements1>, otherwise execute only <statements2> if (<condition1>){<statements1>} else if(<conditions2>){<statements2>}else{<statements3>} If given <condition1> is true, then execute <statements1>, otherwise execute <statements2> if <condition2> is true, otherwise execute <statements3>

If, If else, and else

If, if else, and else (practice!) Print grade from “A” … to “F” From one single integer value (0 – 100) Use if, if else, and else. 90 – 100 : A 80 – 90 : B 70 – 80 : C 60 – 70 : D 0 – 60 : F age Practice 2 100 People can drink beer when they are over 20 People can drive when they are over 14 Write a code that print what people can do. E.g. 15 -> “can drive but cannot drinking”, 21 ->”can drive and drinking”, 10 -> “cannot neither drive nor drinking” Use nested if 20 14

Loop While Do-While For Break needed While (<condition>){<statement>} Execute <statement> while <condition> is true Sequence : <condition> - <statement> - <condition> - <statement> … Do-While Do { <statement> } While( <condition> ) Similar to While but execute <statement> at least one time. Sequence : <statement> - <condition - <statement> - <condition> … For For ([init] ; <condition> ; [incremental]) {<statement>} Sequence : [init] - <condition> - <statement> - [incremental] - <condition> - <statement> - [incremental] - …. Break needed

Loop : while 1 2 3 1 2 3 2 3

Loop : Do-while 1 2 3 1 2 3 2 3

Loop : for 1 2 3 4 1 2 4 3 2 4 3

Let’s practice: Times table Times table for one number (ex. 2) Using one loop Expected output: 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 … Times table for multiple number (ex 2 – 5) Using nested loop 2 x 1 = 2, 3 x 1 = 3, 4 x 1 = 4, 5 x 1 = 5 2 x 2 = 4, 3 x 2 = 6, 4 x 2 = 8, 5 x 2 = 10 ….

break and continue Break Continue Used in loop Stop current loop Skip rest of statement and go next iteration

Example

Switch Similar to multiple if else statement Only possible when we use int variable and conditional value check whether it is equal Execute from certain case to the end Switch <variable> { Case <case1>: <statement1>; break; Case <case2>: <statement2>; … Default: <default statement>; } If <variable> is equal to <case1> then execute <statement1> Else If <variable> is equal to <case2> then execute <statement2> … Execute <default statement>

Switch Exactly Same?

Homework 1. Multi-line times table (2 – 9) Using triple nested loops 2 to 5 for top row 6 to 9 for lower row Omit similar number multiplication Using “if” and “else” on top of previous one E.g. 1x1, 2x2, 3x3 … so on.