0% found this document useful (0 votes)
13 views2 pages

Week 2 Logical Operators in Java

The document explains logical operators in Java, which are used to manipulate boolean values, including AND (&&), OR (||), and NOT (!). It also covers increment and decrement operators, detailing how they increase or decrease a variable's value by 1, with examples of both prefix and postfix forms. Sample code snippets illustrate the usage of these operators in Java programming.

Uploaded by

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

Week 2 Logical Operators in Java

The document explains logical operators in Java, which are used to manipulate boolean values, including AND (&&), OR (||), and NOT (!). It also covers increment and decrement operators, detailing how they increase or decrease a variable's value by 1, with examples of both prefix and postfix forms. Sample code snippets illustrate the usage of these operators in Java programming.

Uploaded by

gillianeflorendo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Logical Operators in Java

Logical operators are used to combine or manipulate boolean values (true or false). They’re
commonly used in conditional statements and loops.

Types of Logical Operators:

Operator Meaning Example Result

&& (Logical Returns true if both conditions are true (5 > 3) && (8 > 6) true
AND)

|| (OR) Returns true if at least one condition is true (5>3) || (8<6) true

! (Logical NOT) Reverses the boolean value !(5 > 3) false

Usage Example:

public class LogicalOperators {

public static void main(String[] args) {

int x = 10, y = 5;

[Link]((x > y) && (y > 0)); // true

[Link]((x < y) || (y > 0)); // true

[Link](!(x > y)); // false

Increment and Decrement Operators

These operators are used to increase or decrease the value of a variable by 1.

Types:

1. Increment (++) → increases value by 1

2. Decrement (--) → decreases value by 1

Each has two forms:

 Prefix (++x / --x): The variable is updated first, then used.


 Postfix (x++ / x--): The variable is used first, then updated.

Example:

public class IncrementDecrement {

public static void main(String[] args) {

int a = 5;

// Prefix

[Link](++a); // 6 (increment first, then print)

// Postfix

[Link](a++); // 6 (print first, then increment to 7)

// Decrement examples

[Link](--a); // 6 (decrement first, then print)

[Link](a--); // 6 (print first, then decrement to 5)

You might also like