Programming Languages Part 2. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Size: px
Start display at page:

Download "Programming Languages Part 2. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University"

Transcription

1 Programming Languages Part 2 Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University 1

2 Objectives You will learn/review: In Java and Python... Data types and operators Terminal input/output Statements Multi-file programs 2

3 Circle Programs Illustrate: Primitive data types Operators Terminal I/O 3

4 Circle in Java See Circle.java Data types: int, double Assignment statement Operators: =, *, +, cast Terminal input via Scanner class Exception handling via try...catch statement Generalizing Data types, operators, and terminal I/O... 4

5 Java Primitive Data Types Data Type Size Example Literals boolean 1 bit false, true char 2 bytes '\u0000', 'uffff', 'a', '\n', '\t', '\0' byte 1 byte (byte)-128, (byte)127 short 2 bytes (short)-32768, (short)32767 int 4 bytes , long 8 bytes L, L float 4 bytes F double 8 bytes

6 Java Operators Operators method(params) a[i] obj.field, obj.method() class.field, class.method() i++, ++i, i--, --i +x, -x ~i,!b new Class() (type) x*y, x/y, i%j (Precedence) Meaning (1) Method call (1) Array element selector (1) Object member selector (1) Class member selector (2) Increment, decrement (2) Unary plus, unary minus (2) Bitwise NOT, logical NOT (3) Object creation (3) Typecast (4) Multiplication, division, remainder 6

7 Java Operators Operators x+y, x-y i<<j, i>>j, i>>>j x<y, x>y, x<=y, x>=y obj instanceof Class x==y, x!=y i&j i^j i j b1&&b2 b1 b2 (Precedence) Meaning (5) Addition, subtraction (6) Left shift, right shift, right shift zero fill (7) Relational (7) instanceof (8) relational (9) Bitwise AND (10) Bitwise exclusive OR (11) Bitwise OR (12) Logical AND (13) Logical OR 7

8 Java Operators Operators b?x:y x=y x+=y, x-=y, x*=y, x/=y, x%=y i&=j, i^=j, i =j i<<=j, i>>=j, i>>>=j (Precedence) Meaning (14) Conditional expression (15) Assignment (15) Assignment (15) Assignment (15) Assignment 8

9 Java Terminal I/O Reading from stdin: import java.util.scanner;... Scanner scanner = new Scanner(System.in); int i = scanner.nextint(); double d = scanner.nextdouble(); String s = scanner.nextline(); 9

10 Java Terminal I/O Writing to stdout: System.out.print(i); System.out.println(d); System.out.println(s); Writing to stderr: System.err.print(i); System.err.println(d); System.err.println(s); 10

11 Circle in Python See circle.py No variable declarations Data types: int, float Type conversions via int() and float() functions Assignment statement String literals: ' ' or " " Operators: * (multiplication), % (string formatting) Terminal input via raw_input() Exception handling via try...except statement 11

12 Circle in Python Generalizing Conversion functions: int(obj), float(obj), str(obj), bool(obj) String formatting: conversion specifications are as in C Data types, operators, and terminal I/O... 12

13 Python Built-In Data Types Data Type Example Literals int 1, 23, 3493, 01, 027, 06645, 0x1, 0x17, 0xDA5 long 1L, 23L, L, 01L, 027L, L, 0x1L, 0x17L, 0x17486CBC75L (no theoretical size limit) float 0., 0.0,.0, 1.0, 1e0, 1.0e0 (correspond to C doubles) bool nonetype False, True None 13

14 Python Operators Operators (Priority) Meaning {key:expr,...} (1) Dictionary creation [expr,...] (2) List creation (expr,...) (3) Tuple creation, or just parentheses f(expr,...) (4) Function call x[startindex:stopindex] (5) Slicing (for sequences) x[index] (6) Indexing (for containers) x.attr (7) Attribute reference x**y (8) Exponentiation ~x (9) Bitwise NOT +x, -x (10) Unary plus, unary minus x*y, x/y, x//y, x%y (11) Mult, div, truncating div, remainder x+y, x-y (12) Addition, subtraction 14

15 Python Operators Operator x<<i, x>>i x&y x^y x y x<y, x<=y, x>y, x>=y x==y, x!=y x is y, x is not y x in y, x not in y not x x and y x or y (Priority) Meaning (13) Left-shift, right-shift (14) Bitwise AND (15) Bitwise XOR (16) Bitwise OR (17) Relational (17) Relational (18) Identity tests (19) Membership tests (20) Logical NOT (21) Logical AND (22) Logical OR 15

16 Aside: Object Identity & Equality $ python >>> x = >>> y = >>> x == y True >>> x is y False >>> x = >>> y = x >>> x == y True >>> x is y True >>> In Python, ints are objects In Python, longs, floats, and bools also are objects 16

17 Python Terminal I/O Reading from stdin: str = raw_input() str = raw_input(promptstr) from sys import stdin str = stdin.readline() 17

18 Python Terminal I/O Writing to stdout: print str print str, Writing to stderr: print >>stderr, str print >>stderr, str, 18

19 Euclid Programs Illustrate: Control flow statements 19

20 Euclid in Java See Euclid.java Control flow statements: if, while, return Generalizing Statements... 20

21 Java Statements Expression statement expr; Declaration statement modifiers datatype var [= expr] [, var [= expr]]...; Common modifiers: const, static Compound statement { statement; statement;...} 21

22 Java Statements if statement if (booleanexpr) statement else statement; switch statement switch (intexpr) { case (intvalue1): statement;...; break; case (intvalue2): statement;...; break;... default: statement;...; } 22

23 Java Statements while statement while (booleanexpr)statement; do statement do statement while (booleanexpr); for statement for (initexpr; booleanexpr; increxpr) statement; Foreach statement for (type var : IterableOrArrayObject) statement; 23

24 Java Statements break statement break; continue statement continue; return statement return; return expr; 24

25 Java Statements try...catch statement try { statement; statement; ; } catch (ExceptionType e) { statement; statement; ; } throw statement throw object; 25

26 Try? try { } catch (Exception e) { } Do or do not do; there is no try. 26

27 Euclid in Python See euclid.py Integer division operator Unpacking assignment statement Control flow statements: if, while, return Generalizing Statements... 27

28 Python Statements Assignment statements var = expr var += expr var -= expr var *= expr var /= expr var //= expr var %= expr var **= expr var &= expr var = expr var ^= expr var >>= expr var <<= expr 28

29 Python Statements Unpacking assignment statement var1,var2,... = iterable No-op statement pass assert statement assert expr, message 29

30 Python Statements print statement print expr,... print expr,..., hack Function call statement f(expr, name=expr,...) return statement return return expr 30

31 Python Statements if statement if expr: statement(s) elif expr: statement(s) else: statement(s) False, 0, None, '', "", [], (), and {} indicate logical FALSE Any other value indicates logical TRUE 31

32 Python Statements while statement while expr: statement(s) False, 0, None, '', "", [], (), and {} indicate logical FALSE Any other value indicates logical TRUE 32

33 Python Statements for statements for var in range(startint, stopint): statements for var in range(stopint): statements for var in iterable: statements for key,value in mapping.iteritems(): statements break statement break continue statement continue 33

34 Python Statements try...except statement try: statement(s) except [ExceptionType[, var]]: statement(s) raise statement raise ExceptionType(str) 34

35 IntMath Modules Illustrate: Multi-file programs... Design to facilitate reuse of functions in multiple programs Building and running multi-file programs 35

36 IntMath in Java See IntMath.java, TestIntMath.java TestIntMath.java is a client of the IntMath module 36

37 IntMath in Java Building and running $ javac TestIntMath.java Automatically builds IntMath.java Directory containing IntMath.java must be in CLASSPATH env var "." is in CLASSPATH by default $ java TestIntMath 37

38 IntMath in Python See intmath.py, testintmath.py testintmath.py is a client of the IntMath module testintmath.py must import from intmath.py 38

39 IntMath in Python Building and running $ python testintmath.py Automatically compiles intmath.py Directory containing intmath.py must be in PYTHONPATH env var "." is in PYTHONPATH by default 39

40 IntMath in Python See intmath2.py Parameter type validation Which is better, intmath1.py or intmath2.py? 40

41 Aside: Python Duck Typing Observation: Python doesn t care about the types of objects given to a function/method Python cares only that the given objects respond to the messages sent by the function/ method to the given objects 41

42 Aside: Python Duck Typing Observation: In other words Python uses duck typing When I see a bird that walks like a duck and swims like a duck and quacks like a duck I call that bird a duck. -- J. W. Riley 42

43 Aside: Python Duck Typing Style 1: Don t validate parameter types Rationale: Validating parameter types prohibits fortuitous use of functions/methods on parameters of unanticipated types Rationale: Validating parameter types consumes run-time In our example: intmath2.py works on ints only intmath1.py works on ints, and fortuitously, on longs! So intmath1.py is better 43

44 Aside: Python Duck Typing Style 2: Validate parameter types Rationale: When given a parameter of an unanticipated type, function/method may yield: Errors at low call levels rather than immediately No errors and incorrect results!!! In our example: intmath2.py safeguards against str or float arguments intmath1.py does not So intmath2.py is better 44

45 Aside: Python Duck Typing Controversy! Generally style 1 (do not validate parameter types) is considered more Pythonic So that s what we ll use in COS

46 Aside: Python Duck Typing Commentary Application-dependent code Maybe need not validate parameter types Application-independent code Maybe should validate parameter types The negative of accidental incorrect results outweighs the positive of fortuitous use of duck typing!!! 46

47 Aside: Python Duck Typing Commentary Small projects: Maybe need not validate parameter types Large projects: Maybe should validate parameter types But if you feel the need to validate parameter types, should you use Python anyway??? 47

48 ReadNum Modules The job: Prompt for and read an int/double from stdin Re-prompt and re-read if user enters bad data Illustrate Multi-file programs (as previous example) Unit testing 48

49 ReadNum in Java See ReadNum.java, Circle2.java Consumes entire line Otherwise bad characters would affect subsequent reads Calls Integer.parseInt() and Double.parseDouble() to convert String to int/double Uses exceptions to handle errors Defines main() method in ReadNum.java Common technique for unit testing 49

50 ReadNum in Python See readnum.py, circle2.py Consumes entire line Calls int() and float() to convert string to int/double Uses exceptions to handle errors Defines conditionally-executed test code Common technique for unit testing Uses leading underscore (_test) to suggest private function 50

51 Summary We have covered: In Java and Python... Data types and operators Terminal input/output Statements Multi-file programs 51

52 Appendix: C Programming 52

53 Circle in C See circle.c Data types: int, double Operators: =, *, +, cast Terminal input via scanf() Lack of exception handling Must check function return value instead Generalizing Data types, operators, and terminal I/O... 53

54 C Primitive Data Types Data Type Size Example Literals char 1 byte (char)-128, (char)127, 'a', '\n', '\t', '\0' unsigned char 1 byte (unsigned char) 0, (unsigned char)256 short 2 bytes? (short)-32768, (short)32767 unsigned short 2 bytes? (short)0, (short)65535 int 4 bytes? , unsigned int 4 bytes? 0U, U, 0123, 0x123 long 4 bytes? L, L unsigned long 4 bytes? 0UL, UL, 0123L, 0x123L float 4 bytes? F double 8 bytes? long double 12 bytes? L 54

55 C Operators Operator fun(params) a[i] s.f ps->f (type) *p, &x i++, ++i, i--, --i +x, -x (Precedence) Meaning (1) Function call (1) Array element selector (1) Structure field selector (1) Structure dereference and field selector (2) Typecast (2) Dereference, address of (2) Increment, decrement (2) Unary positive, unary negative ~i (2) Bitwise NOT!i (2) Logical NOT sizeof(type) (2) sizeof 55

56 C Operators Operator x*y, x/y, i%j x+y, x-y i<<j, i>>j x,y, x>y, x<=y, x>=y x==y, x!=y i&j i^j i j i&&j i j i?x:y (Precedence) Meaning (3) Multiplication, division, remainder (4) Addition, subtraction (5) Left-shift, right-shift (6) Relational (7) Relational (8) Bitwise AND (9) Bitwise EXCLUSIVE OR (10) Bitwise OR (11) Logical AND (12) Logical OR (13) Conditional expression 56

57 C Operators Operator x=y x+=y, x-=y, x*=y, x/=y i&=j, i^=j, i =j i<<=j, i>>=j x,y (Precedence) Meaning (14) Assignment (14) Assignment (14) Assignment (14) Assignment (15) Sequence 57

58 C Terminal I/O Reading from stdin: ivaluesread = scanf("%d", &i); ivaluesread = scanf("%lf", &d); s = fgets(s, size, stdin) 58

59 C Terminal I/O Writing to stdout: printf("%d", i); printf("%f", d); fputs(s, stdout); Writing to stderr: fprintf(stderr, "%d", i); fprintf(stderr, "%f", d); fputs(s, stderr); 59

60 Euclid in C See euclid.c Control flow statements: if, while, return Generalizing Statements... 60

61 C Statements Expression statement expr; Declaration statement modifiers datatype var [= expr] [, var [= expr]]...; Common modifiers: const, static Compound statement { statement; statement;...} 61

62 C Statements If statement if (intorptrexpr) statement [else statement] 0 or NULL pointer => false; anything else => true Switch statement switch (intexpr) { case (intvalue1): statement;...; break; case (intvalue2): statement;...; break;... default: statement;...; } 62

63 C Statements While statement while (intorptrexpr) statement; Do...while statement do statement while (intorptrexpr); For statement for (initexpr; intorptrexpr; increxpr) statement; 63

64 C Statements Break statement break; Continue statement continue; Return statement: return; return expr; 64

65 IntMath in C See intmath.h, intmath.c, testintmath.c intmath.h is IntMath module interface Protection against multiple inclusion Function names begin with module name intmath.c is IntMath module implementation #include intmath.h so compiler can enforce consistency testintmath.c is IntMath module client #include intmath.h so compiler can check calls of IntMath_gcd() and IntMath_lcm() 65

66 IntMath in C Building: $ gcc intmath.c testintmath.c -o testintmath Running: $ testintmath 66

67 ReadNum in C See readnum.h, readnum.c, circle2.c Consumes entire line Otherwise bad characters would affect subsequent reads Calls strtol() and strtod() to convert string to int/double Contains lots of code for error handling 67

68 ReadNum in C Conditionally includes main() function in readnum module Common technique for unit testing To build normally: gcc circle2.c readnum.c o circle2 To build for unit testing: gcc -D TEST_READNUM readnum.c o readnum 68

Programming Languages: Part 2. Robert M. Dondero, Ph.D. Princeton University

Programming Languages: Part 2. Robert M. Dondero, Ph.D. Princeton University Programming Languages: Part 2 Robert M. Dondero, Ph.D. Princeton University 1 Objectives You will learn/review: In C, Java, and Python... Terminal input/output Data types, operators, statements Multi-file

More information

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7

Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7 Princeton University COS 333: Advanced Programming Techniques A Subset of Python 2.7 Program Structure # Print "hello world" to stdout. print 'hello, world' # Print "hello world" to stdout. def f(): print

More information

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Princeton University COS 333: Advanced Programming Techniques A Subset of C90 Program Structure /* Print "hello, world" to stdout. Return 0. */ { printf("hello, world\n"); -----------------------------------------------------------------------------------

More information

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP

Princeton University COS 333: Advanced Programming Techniques A Subset of PHP Princeton University COS 333: Advanced Programming Techniques A Subset of PHP Program Structure -----------------------------------------------------------------------------------

More information

Continued from previous lecture

Continued from previous lecture The Design of C: A Rational Reconstruction: Part 2 Jennifer Rexford Continued from previous lecture 2 Agenda Data Types Statements What kinds of operators should C have? Should handle typical operations

More information

The Design of C: A Rational Reconstruction: Part 2

The Design of C: A Rational Reconstruction: Part 2 The Design of C: A Rational Reconstruction: Part 2 1 Continued from previous lecture 2 Agenda Data Types Operators Statements I/O Facilities 3 Operators Issue: What kinds of operators should C have? Thought

More information

Princeton University COS 333: Advanced Programming Techniques A Subset of Java

Princeton University COS 333: Advanced Programming Techniques A Subset of Java Princeton University COS 333: Advanced Programming Techniques A Subset of Java Program Structure public class Hello public static void main(string[] args) // Print "hello, world" to stdout. System.out.println("hello,

More information

SECTION II: LANGUAGE BASICS

SECTION II: LANGUAGE BASICS Chapter 5 SECTION II: LANGUAGE BASICS Operators Chapter 04: Basic Fundamentals demonstrated declaring and initializing variables. This chapter depicts how to do something with them, using operators. Operators

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

Chapter 3: Operators, Expressions and Type Conversion

Chapter 3: Operators, Expressions and Type Conversion 101 Chapter 3 Operators, Expressions and Type Conversion Chapter 3: Operators, Expressions and Type Conversion Objectives To use basic arithmetic operators. To use increment and decrement operators. To

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C Princeton University Computer Science 217: Introduction to Programming Systems The Design of C C is quirky, flawed, and an enormous success. While accidents of history surely helped, it evidently satisfied

More information

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B Java for Python Programmers Comparison of Python and Java Constructs Reading: L&C, App B 1 General Formatting Shebang #!/usr/bin/env python Comments # comments for human readers - not code statement #

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Informatics Ingeniería en Electrónica y Automática Industrial

Informatics Ingeniería en Electrónica y Automática Industrial Informatics Ingeniería en Electrónica y Automática Industrial Operators and expressions in C Operators and expressions in C Numerical expressions and operators Arithmetical operators Relational and logical

More information

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

More information

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course JAC444 - Lecture 1 Introduction to Java Programming Language Segment 4 1 Overview of the Java Language In this segment you will be learning about: Numeric Operators in Java Type Conversion If, For, While,

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University Overview of Source Code Components Comments Library declaration Classes Functions Variables Comments Can

More information

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands Introduction Operators are the symbols which operates on value or a variable. It tells the compiler to perform certain mathematical or logical manipulations. Can be of following categories: Unary requires

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java Electrical and Computer Engineering Object-Oriented Topic 2: Fundamental Structures in Java Maj Joel Young Joel.Young@afit.edu 8-Sep-03 Maj Joel Young Java Identifiers Identifiers Used to name local variables

More information

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp DM550 / DM857 Introduction to Programming Peter Schneider-Kamp petersk@imada.sdu.dk http://imada.sdu.dk/~petersk/dm550/ http://imada.sdu.dk/~petersk/dm857/ OBJECT-ORIENTED PROGRAMMING IN JAVA 2 Programming

More information

Computers Programming Course 6. Iulian Năstac

Computers Programming Course 6. Iulian Năstac Computers Programming Course 6 Iulian Năstac Recap from previous course Data types four basic arithmetic type specifiers: char int float double void optional specifiers: signed, unsigned short long 2 Recap

More information

Chapter 2. Elementary Programming

Chapter 2. Elementary Programming Chapter 2 Elementary Programming 1 Objectives To write Java programs to perform simple calculations To obtain input from the console using the Scanner class To use identifiers to name variables, constants,

More information

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

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Op. Use Description + x + y adds x and y x y

More information

The Arithmetic Operators

The Arithmetic Operators The Arithmetic Operators The arithmetic operators refer to the standard mathematical operators: addition, subtraction, multiplication, division and modulus. Examples: Op. Use Description + x + y adds x

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

Expressions and Precedence. Last updated 12/10/18

Expressions and Precedence. Last updated 12/10/18 Expressions and Precedence Last updated 12/10/18 Expression: Sequence of Operators and Operands that reduce to a single value Simple and Complex Expressions Subject to Precedence and Associativity Six

More information

Java Basic Programming Constructs

Java Basic Programming Constructs Java Basic Programming Constructs /* * This is your first java program. */ class HelloWorld{ public static void main(string[] args){ System.out.println( Hello World! ); A Closer Look at HelloWorld 2 This

More information

Reserved Words and Identifiers

Reserved Words and Identifiers 1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

Writing Program in C Expressions and Control Structures (Selection Statements and Loops) Writing Program in C Expressions and Control Structures (Selection Statements and Loops) Jan Faigl Department of Computer Science Faculty of Electrical Engineering Czech Technical University in Prague

More information

Part I Part 1 Expressions

Part I Part 1 Expressions Writing Program in C Expressions and Control Structures (Selection Statements and Loops) Jan Faigl Department of Computer Science Faculty of Electrical Engineering Czech Technical University in Prague

More information

JAVA OPERATORS GENERAL

JAVA OPERATORS GENERAL JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure C Overview Basic C Program Structure C OVERVIEW BASIC C PROGRAM STRUCTURE Goals The function main( )is found in every C program and is where every C program begins speed execution portability C uses braces

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments Basics Objectives Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments 2 Class Keyword class used to define new type specify

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

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

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

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

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

More information

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

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>= Operators in java Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc. There are many types of operators in java which are given below: Unary Operator, Arithmetic

More information

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed C Overview C OVERVIEW Goals speed portability allow access to features of the architecture speed C fast executables allows high-level structure without losing access to machine features many popular languages

More information

Eng. Mohammed S. Abdualal

Eng. Mohammed S. Abdualal Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 3 Selections

More information

And Parallelism. Parallelism in Prolog. OR Parallelism

And Parallelism. Parallelism in Prolog. OR Parallelism Parallelism in Prolog And Parallelism One reason that Prolog is of interest to computer scientists is that its search mechanism lends itself to parallel evaluation. In fact, it supports two different kinds

More information

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. Today! Build HelloWorld yourself in BlueJ and Eclipse. Look at all the Java keywords. Primitive Types. HelloWorld in BlueJ 1. Find BlueJ in the start menu, but start the Select VM program instead (you

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2 Primitive Data Types and Operations. Objectives Chapter 2 Primitive Data Types and Operations Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs,

More information

GOLD Language Reference Manual

GOLD Language Reference Manual GOLD Language Reference Manual Language Guru: Timothy E. Chung (tec2123) System Architect: Aidan Rivera (ar3441) Manager: Zeke Reyna (eer2138) Tester: Dennis Guzman (drg2156) October 16th, 2017 1 Introduction

More information

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz) SOFTWARE DEVELOPMENT 1 Operators 2018W (Institute of Pervasive Computing, JKU Linz) OPERATORS Operators are required to form expressions. Depending on the number of operands they take, they are called:

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

Lecture 02 C FUNDAMENTALS

Lecture 02 C FUNDAMENTALS Lecture 02 C FUNDAMENTALS 1 Keywords C Fundamentals auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void

More information

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Goals of C  The Goals of C (cont.)  Goals of this Lecture The Design of C: A Rational Reconstruction Goals of this Lecture The Design of C: A Rational Reconstruction Help you learn about: The decisions that were available to the designers of C The decisions that were made by the designers of C Why? Learning

More information

C Programming Language (Chapter 2 of K&R) Variables and Constants

C Programming Language (Chapter 2 of K&R) Variables and Constants C Programming Language (Chapter 2 of K&R) Types, Operators and Expressions Variables and Constants Basic objects manipulated by programs Declare before use: type var1, var2, int x, y, _X, x11, buffer;

More information

A flow chart is a graphical or symbolic representation of a process.

A flow chart is a graphical or symbolic representation of a process. Q1. Define Algorithm with example? Answer:- A sequential solution of any program that written in human language, called algorithm. Algorithm is first step of the solution process, after the analysis of

More information

File Handling in C. EECS 2031 Fall October 27, 2014

File Handling in C. EECS 2031 Fall October 27, 2014 File Handling in C EECS 2031 Fall 2014 October 27, 2014 1 Reading from and writing to files in C l stdio.h contains several functions that allow us to read from and write to files l Their names typically

More information

DM503 Programming B. Peter Schneider-Kamp.

DM503 Programming B. Peter Schneider-Kamp. DM503 Programming B Peter Schneider-Kamp petersk@imada.sdu.dk! http://imada.sdu.dk/~petersk/dm503/! VARIABLES, EXPRESSIONS & STATEMENTS 2 Values and Types Values = basic data objects 42 23.0 "Hello!" Types

More information

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Tools : The Java Compiler. The Java Interpreter. The Java Debugger Tools : The Java Compiler javac [ options ] filename.java... -depend: Causes recompilation of class files on which the source files given as command line arguments recursively depend. -O: Optimizes code,

More information

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below: Q1. Explain the structure of a C program Structure of the C program is shown below: Preprocessor Directives Global Declarations Int main (void) Local Declarations Statements Other functions as required

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All for repetition statement do while repetition statement switch multiple-selection statement break statement continue statement Logical

More information

Operators and Expressions:

Operators and Expressions: Operators and Expressions: Operators and expression using numeric and relational operators, mixed operands, type conversion, logical operators, bit operations, assignment operator, operator precedence

More information

Programming Lecture 3

Programming Lecture 3 Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

More information

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48

2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B. 26 C. 31 D. 48 1. How can you make an infinite loop in C? A. while(1) { } B. loop:... goto loop; C. for(;;) { } D. All answers are right 2. C99 standard guarantees uniqueness of characters for internal names. A. 12 B.

More information

These are reserved words of the C language. For example int, float, if, else, for, while etc.

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

Information Science 1

Information Science 1 Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

More information

Introduction to C++ with content from

Introduction to C++ with content from Introduction to C++ with content from www.cplusplus.com 2 Introduction C++ widely-used general-purpose programming language procedural and object-oriented support strong support created by Bjarne Stroustrup

More information

.Net Technologies. Components of.net Framework

.Net Technologies. Components of.net Framework .Net Technologies Components of.net Framework There are many articles are available in the web on this topic; I just want to add one more article over the web by explaining Components of.net Framework.

More information

Operators in java Operator operands.

Operators in java Operator operands. Operators in java Operator in java is a symbol that is used to perform operations and the objects of operation are referred as operands. There are many types of operators in java such as unary operator,

More information

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/58 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

COMP6700/2140 Operators, Expressions, Statements

COMP6700/2140 Operators, Expressions, Statements COMP6700/2140 Operators, Expressions, Statements Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU 3 March 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Operators,

More information

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D 1/60 Interactive use $ python Python 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information.

More information

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos. Lecture 2: Variables and Operators AITI Nigeria Summer 2012 University of Lagos. Agenda Variables Types Naming Assignment Data Types Type casting Operators Declaring Variables in Java type name; Variables

More information

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

More information

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

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one. http://www.tutorialspoint.com/go/go_operators.htm GO - OPERATORS Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

More information

Prof. Navrati Saxena TA: Rochak Sachan

Prof. Navrati Saxena TA: Rochak Sachan JAVA Prof. Navrati Saxena TA: Rochak Sachan Operators Operator Arithmetic Relational Logical Bitwise 1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6.

More information

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

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators

More information

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

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands Performing Computations C provides operators that can be applied to calculate expressions: tax is 8.5% of the total sale expression: tax = 0.085 * totalsale Need to specify what operations are legal, how

More information

STSCI Python Introduction. Class URL

STSCI Python Introduction. Class URL STSCI Python Introduction Class 2 Jim Hare Class URL www.pst.stsci.edu/~hare Each Class Presentation Homework suggestions Example files to download Links to sites by each class and in general I will try

More information

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++ Crash Course in Java Netprog: Java Intro 1 Why Java? Network Programming in Java is very different than in C/C++ much more language support error handling no pointers! (garbage collection) Threads are

More information

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

Computer Components. Software{ User Programs. Operating System. Hardware

Computer Components. Software{ User Programs. Operating System. Hardware Computer Components Software{ User Programs Operating System Hardware What are Programs? Programs provide instructions for computers Similar to giving directions to a person who is trying to get from point

More information

DM550 Introduction to Programming part 2. Jan Baumbach.

DM550 Introduction to Programming part 2. Jan Baumbach. DM550 Introduction to Programming part 2 Jan Baumbach jan.baumbach@imada.sdu.dk http://www.baumbachlab.net COURSE ORGANIZATION 2 Course Elements Lectures: 10 lectures Find schedule and class rooms in online

More information

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g. 1.3a Expressions Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a syntactical token that requires an action be taken An operand is an object

More information

JAVA Programming Fundamentals

JAVA Programming Fundamentals Chapter 4 JAVA Programming Fundamentals By: Deepak Bhinde PGT Comp.Sc. JAVA character set Character set is a set of valid characters that a language can recognize. It may be any letter, digit or any symbol

More information

Introduction to Programming (Java) 2/12

Introduction to Programming (Java) 2/12 Introduction to Programming (Java) 2/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction

More information

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled Interpreted vs Compiled Python 1 Java Interpreted Easy to run and test Quicker prototyping Program runs slower Compiled Execution time faster Virtual Machine compiled code portable Java Compile > javac

More information

Primitive Data Types: Intro

Primitive Data Types: Intro Primitive Data Types: Intro Primitive data types represent single values and are built into a language Java primitive numeric data types: 1. Integral types (a) byte (b) int (c) short (d) long 2. Real types

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

More information

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static

More information

Expressions & Flow Control

Expressions & Flow Control Objectives Distinguish between instance and local variables 4 Expressions & Flow Control Describe how instance variables are initialized Identify and correct a Possible reference before assignment compiler

More information

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

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

Summary of Go Syntax /

Summary of Go Syntax / Summary of Go Syntax 02-201 / 02-601 Can declare 1 or more variables in same var statement Variables Can optionally provide initial values for all the variables (if omitted, each variable defaults to the

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types

More information