0% found this document useful (0 votes)
1 views

1.4. Java & Python Basics (1)

The document covers the fundamentals of programming syntax, keywords, and operators in Java and Python. It explains the importance of syntax in programming languages, the reserved keywords specific to each language, and the various types of operators used for mathematical, relational, and logical operations. Additionally, it includes exercises to reinforce understanding of keywords and operator evaluations.

Uploaded by

Alistair Frame
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

1.4. Java & Python Basics (1)

The document covers the fundamentals of programming syntax, keywords, and operators in Java and Python. It explains the importance of syntax in programming languages, the reserved keywords specific to each language, and the various types of operators used for mathematical, relational, and logical operations. Additionally, it includes exercises to reinforce understanding of keywords and operator evaluations.

Uploaded by

Alistair Frame
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

FOUNDATIONS

OF
COMPUTATIONAL
THINKING AND PROGRAMMING
Java & Python – The basics
Learning Objectives
This session will cover:
1. Syntax
2. Keywords
3. Operators
Syntax

When we want to communicate with a human being, we make use of


a natural language such as English.

If the communication happens in a written format, we must pay


particular attention to our punctuation, grammar and spelling in order
to enable the person understand what we want to communicate.

Punctuations, grammar and spelling make up a set of rules that


must be followed and can collectively be referred as the syntax of
the natural language.

The syntax of a programming language is similar to that of a natural


language.
Syntax

The syntax of a programming language consists of rules that define


the structure of that language.

Without syntax the meaning of the language can be hard to


understand.

Since computers need precise instructions, any syntactical


mistakes can lead to programming error.

For example, in case sensitive programming languages, the


following will be treated as different variables:
• myvar
• myVar
• MyVar
Java Syntax

public class Syntax { A Java program begins with a


public static void main (String[] args){ class statement.
System.out.println(“Java has its own
syntax”); It is imperative for the
} program to contain one and
} only one main method.

Produces the following result: The word System starts with


capital s.
Java has its own syntax.
Each statement is terminated
by a semicolon.
Python Syntax

Unlike Java, Python does not


print (“Python has a simpler syntax need a class and main
compared to Java.”) method to execute a
statement.
Produces the following result:
It further does not need curly
brackets to enclose different
Python has a simpler syntax compared to parts of the program.
Java.
In addition, Python
statements do not end with a
semicolon.
Keywords

Keywords are reserved words


specific to the programming
language and have special
meanings.

Each programming language has


its own set of keywords.

Keywords cannot be used as


identifiers, such as variable or
class names.
Keywords in Python

The following are reserved words in Python 3.

False await else import pass

None break except in raise

True class finally is return

and continue for lambda try

as def from nonlocal while

assert del global not with

async elif if or yield


Keywords in Java

The following are reserved words in Java.

abstract continue for new switch


assert default goto package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while
Exercise 1

Research the keywords in C and C++ programming


languages.

Are there common keywords in C, C++ and Java? If so,


why do you think that is the case?
Operators

An operator in a programming
language is a symbol which can
be used to perform either a
mathematical, relational or
logical operation.

The use of an operator instructs


the compiler or interpreter to
perform certain tasks and
produce the final result.
Arithmetic Operators

The following table shows fundamental arithmetic operators that


are supported both in Python and Java.
Operator Description Example
+ Adds two operands 14 + 23 = 37
- Subtracts the second operand 61 – 19 = 42
from the first
* Multiplies multiple operands 2 * 4 * 5 = 40

/ Divides numerator by 70/14 = 5


denominator
% Gives remainder of an integer 22 % 6 = 4
division
Relational Operators

Operator Description Example


== Checks if the values of two operands are equal 4 == 6 is false
10 == 2*5 is true
!= Checks if the values of two operands are not equal 12 != 15 is true
2+7 != 9 is false
> Checks if the value of the left operand is greater 30 > 25 is true
than the value of the right operand 27 > 34 is false
< Checks if the value of the left operand is less than 41 < 46 is true
the value of the right operand 50 < 33 is false
>= Checks if the value of the left operand is greater 60 >= 60 is true
than or equal to the value of the right operand 71 >= 75 is false
<= Checks if the value of the left operand is less than 86 <= 2*43 is true
or equal to the value of the right operand 90 <= 88 is false
Logical Operators
Operator Description Example
In Java In Python

&& and Logical AND 4 > 2 && 8 <= 15 evaluates to true in Java
operator 21 == 25 and 40 == 40 evaluates to false in
Python
|| or Logical OR 27 > 32 || 14 <= 8 evaluates to false in Java
operator 6 <= 15 or 70 != 70 evaluates to true in
Python
! not Logical NOT !(9 > 6) && 5 >= 3 evaluates to false in Java
operator 10 == 10 or not(12 > 11) evaluates to true in
Python
Exercise 2

Decide whether each of the followings evaluates to true or false.

Expression True/False
30 % 4 <= 2
15 < 10 || 21 != 11
14 > 5 && 8 == 2*4
True && False || 17 >= 17
Summary

• The syntax of a programming language consists of rules


that define the structure of that language.

• Without syntax the meaning of the language can be hard


to understand.
• Keywords are reserved words specific to the programming
language and have special meanings.
• An operator in a programming language is a symbol which
can be used to perform either a mathematical, relational or
logical operation.
Further reading

Scott, M. L. (2009) Programming Language Pragmatics (4th


edn.) Waltham, MA: Morgan Kaufmann

You might also like