0% found this document useful (0 votes)
3 views41 pages

Lecture 4 - Elementary Programming (Variables)

java

Uploaded by

jafarhossaini238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views41 pages

Lecture 4 - Elementary Programming (Variables)

java

Uploaded by

jafarhossaini238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Islamic Emirate of Afghanistan

Ministry of Higher Education


Herat University
Computer Science Faculty

Introduction to Practical Computer Science 1 (Semester 1)


Elementary Programming (Variables)
Lecture 04

Lecturer: Mohammed Hamed Amiry


[email protected]

1
Content…
• What is Error? Which type of error do you know?
• What is different between runtime and logical error?
• What are differences between Identifiers and Keywords?
• What is comment and how many type of comment do we have?
• What are differences between Primitive type and Non-Primitive
type?

2
Programming Elements
• There are the most programming element, which also use in java.

• Keywords • Variables
• Identifiers • Constants
• Separators • Literals
• White Space • Operators
• Comments • Expression
• Statements
• Data Types
• Blocks
• Scope

3
Java Programming Elements
Variables
• A variable represents a value stored in the computer’s memory.

• A variable is a storage location in memory that a programmer


can use to store data.
• Variables are used to represent values that may be changed in the
program.
• All programming languages use the concept of variables to reference data in
memory.
• In Java, a variable can hold
1.primitive data types
2.Arrays
3.Non- primitive Data types

4
Java Programming Elements
variables

• With variables you can:


1.Store data
2.Read the data
3.Perform a lot of calculations
• In Java, with variables you can not:
1.Store data of a different type in a while.

5
Java Programming Elements
Variables

• In Java, a variable has three parts:


1.Type
2.Name
3.Value
• The name can be used to get access to the variable
• Store a value

• Read the value

6
Java Programming Elements
Declaration

• Before a variable can be used, it has to be declared first.


• To decelerate a variable, its type and name are written in the source
code
• This is followed by the symbol ‘ ; ’

DataType VariableName;
• Names of variables should always start with a small letter.
• Examples:
• int age;
• String name;

7
Java Programming Elements
Assignment

• To store a value into a variable, we must use the symbol ‘ = ’


into the source code and followed by the symbol ‘ ; ’

Name = Value ;
• Examples:
age = 25;
name = “Mohammad”;

8
Java Programming Elements
Initialization

• Declaration and Assignment can do in one step:

DataType VariableName = Value ;


• Examples:
int age = 25;
String name = “Mohammad”;

9
Java Programming Elements
Initialization: Example

10
Java Programming Elements
Hello World with Variable

public class HelloWorld {


public static void main (String args [ ]) {
String myFirstString = "Hello World";
System.out.println(myFirstString);
}
}

11
Java Programming Elements
Reading Input from the Console

• Reading input from the console enables the program to accept input from
the user.
• Scanner class for console input.
• Java uses System.out to refer to the standard output device, and
System.in to the standard input device.
• To perform console input, you need to use the Scanner class to
create an object to read input from System.in, as follows:
• Scanner input = new Scanner(System.in);

12
Java Programming Elements
Reading Input from the Console

13
Java Programming Elements
Constant
• Constants are variables with values that can’t be changed.
• The value is assigned to a constant when it is declared.
• Use the word final in front of a normal variable declaration to make it a
constant.

public class HelloWorld{


public static void main (String args [ ]){
final double PI = 3.14;
}
}

14
Java Programming Elements
Literals

• A literal is not a name – it is the value itself.


• A literal can be a number, a boolean or a character.
• In the expression, x = 3;
x is a variable 3 is a literal

15
Java Programming Elements
Operators

• It is a symbol that operates on one or more operands to produce a result.


• Operators works with variables and make an operation.
• In the example below x and y are operands and + is the operator
int result = x+y;

16
Java Programming Elements
Operators
• These operators are divided in 4 categories :
• Arithmetic operators
• Relational and conditional operators
• logical operators (binary)
• Assignment operators

17
Operators
Arithmetic Operators
• Arithmetic operators refer to the standard mathematical operators
you learned in elementary school.

• Addition: operand1 + operand2;


• Subtraction: operand1 - operand2;
• Multiplication: operand1 * operand2;
• Division: operand1 / operand2;
• Modulus: operand1 % operand2;

18
Operators
Arithmetic Operators

19
Operators
Arithmetic Operators

• Let’s see them in java

20
Operators
Relational and conditional operators
• A relational operator compares two values and returns true or false
depending on the relationship between them.
• They are commonly used in Conditions statements.
• Java provides six relational operators

21
Operators
Relational and conditional operators

22
Operators
Relational and conditional operators

23
Operators
logical operators
• Logical operators return a true or false value based on the state of the
Variables.
• logical operand must be a boolean data type, and the result is always a
boolean data type.

24
Operators
logical operators

25
Operators
Assignment operators

• The Java assignment operator has the following syntax:

variable = expression
int result = 10+20;
puts the value 30 into the variable called result.
• When using assignment statements, the value to the right-hand side of the =
is assigned to the variable on the left-hand side.

26
Operators
Assignment operators

27
Operators
Operators Example

• Let’s see them in java

28
Operators
Precedence revisited
• Operator precedence determines the grouping of terms in an expression.
• This affects how an expression is evaluated.
• Certain operators have higher precedence than others.
• What would be the result of the following expression:

int x = 7 + 3 * 2;
• Here x is assigned 13, not 20
• The operator * has higher precedence than + so it first get multiplied with
3*2 and then adds into 7

29
Operators
Precedence revisited

30
Java Programming Elements
Expression
• Series of variables, operators, and method calls that evaluates to a single value.
• Expressions are the simplest form of statements in Java.
• Job of expression:
• Perform computation.
• Return value, result of computation.

31
Java Programming Elements
Expression

• Example:
• x = 3, y = 2, z = 1;

int result = x * y * z; //result = 6;

• int result = x * y - z;
int result = (x * y) - z; / / result = 5
int result = x * (y - z); / / result = 3

32
Java Programming Elements
Statements
There are different kind of statements in java:
• Expression Statements
• aValue = 42; //assignment statement
• aValue++; //increment statement
• Declaration Statements
• int aValue;
• Object creation statement
• Student stObject = new Student();
• Control Flow Statements
• while, for
• if, switch

33
Data Types
Blocks

• A collection of statements that is bounded by the braces { }.


• They bound the class definitions, method definitions and other statement
that should be executed in a group.

34
Data Types
Blocks

35
Data Types
Scope

• Blocks divide a program into smaller parts.


• Variables are only valid in the block they have been created in.
• The area where a variable is created and run in is called scope.

36
Data Types
Scope: Example

37
Summary
• There are some other programming element, such as:
Literal, Operators, Expression, Statements, Blocks and Scope.
• Literal is the value itself.
• Operators are symbol that operates on one or more operands to
produce a result.
• Expression are series of variables, operators, and method calls that
evaluates to a single value.
• Block is a collection of statements that is bounded by the braces .
• The area where a variable is created in is called scope.

38
For more study
• Chapter 2 • Chapter 2

39
Next Lecture On
• Selections

40
Q &A SECTION

41

You might also like