Controlling Program Execution
Course: 4343203 - Java Programming
Arithmetic operators perform mathematical operations
| Operator | Name | Description | Example |
|---|---|---|---|
+ |
Addition | Adds two values | x + y |
- |
Subtraction | Subtracts one value from another | x - y |
* |
Multiplication | Multiplies two values | x * y |
/ |
Division | Divides one value by another | x / y |
% |
Modulus | Returns division remainder | x % y |
++ |
Increment | Increases value by 1 | ++x, x++ |
-- |
Decrement | Decreases value by 1 | --x, x-- |
int a = 10, b = 3;
int sum = a + b; // 13
int difference = a - b; // 7
int product = a * b; // 30
int quotient = a / b; // 3 (integer division)
int remainder = a % b; // 1
int x = 5;
int y = ++x; // Pre-increment: x=6, y=6
int z = x++; // Post-increment: x=7, z=6
int p = 10;
int q = --p; // Pre-decrement: p=9, q=9
int r = p--; // Post-decrement: p=8, r=9
Assignment operators assign values to variables
| Operator | Example | Same As | Description |
|---|---|---|---|
= |
x = 5 |
x = 5 |
Simple assignment |
+= |
x += 3 |
x = x + 3 |
Add and assign |
-= |
x -= 3 |
x = x - 3 |
Subtract and assign |
*= |
x *= 3 |
x = x * 3 |
Multiply and assign |
/= |
x /= 3 |
x = x / 3 |
Divide and assign |
%= |
x %= 3 |
x = x % 3 |
Modulus and assign |
int score = 100;
score += 50; // score = 150
score -= 20; // score = 130
score *= 2; // score = 260
score /= 4; // score = 65
score %= 10; // score = 5
int a, b, c;
a = b = c = 10; // All variables get 10
// Chain assignment
int x = 5;
int y = (x += 3); // x = 8, y = 8
Relational operators compare values and return boolean results
| Operator | Name | Description | Example |
|---|---|---|---|
== |
Equal to | Returns true if values are equal | x == y |
!= |
Not equal | Returns true if values are different | x != y |
> |
Greater than | Returns true if left > right | x > y |
< |
Less than | Returns true if left < right | x < y |
>= |
Greater than or equal | Returns true if left >= right | x >= y |
<= |
Less than or equal | Returns true if left <= right | x <= y |
public class RelationalExample {
public static void main(String[] args) {
int a = 10, b = 5, c = 10;
System.out.println(a == b); // false
System.out.println(a != b); // true
System.out.println(a > b); // true
System.out.println(a < b); // false
System.out.println(a >= c); // true
System.out.println(b <= a); // true
// Common usage in conditions
if (a > b) {
System.out.println("a is greater than b");
}
}
}
Logical operators work with boolean values
| Operator | Name | Description | Example |
|---|---|---|---|
&& |
Logical AND | True if both operands are true | x && y |
|| |
Logical OR | True if at least one operand is true | x || y |
! |
Logical NOT | Reverses the logical state | !x |
boolean a = true, b = false;
boolean result1 = a && b; // false
boolean result2 = a || b; // true
boolean result3 = !a; // false
// Practical usage
int age = 20;
boolean hasLicense = true;
boolean canDrive = (age >= 18) && hasLicense;
// Short-circuit evaluation
if (age > 0 && (100/age) > 5) {
System.out.println("Valid");
}
| A | B | A&&B | A||B | !A |
|---|---|---|---|---|
| T | T | T | T | F |
| T | F | F | T | F |
| F | T | F | T | T |
| F | F | F | F | T |
Short-circuit: && stops at first false, || stops at first true
Bitwise operators perform operations on individual bits
| Operator | Name | Description |
|---|---|---|
& | AND | Bitwise AND |
| | OR | Bitwise OR |
^ | XOR | Bitwise XOR |
~ | Complement | Bitwise NOT |
<< | Left Shift | Shift bits left |
>> | Right Shift | Shift bits right |
int a = 5; // Binary: 101
int b = 3; // Binary: 011
int and = a & b; // 001 = 1
int or = a | b; // 111 = 7
int xor = a ^ b; // 110 = 6
int not = ~a; // ...11111010
int left = a << 1; // 1010 = 10
int right = a >> 1; // 10 = 2
Ternary operator provides a compact if-else alternative
variable = (condition) ? expressionTrue : expressionFalse;
int time = 20;
String greeting;
if (time < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
int time = 20;
String greeting = (time < 18) ?
"Good day" : "Good evening";
// More examples
int max = (a > b) ? a : b;
String status = (score >= 60) ?
"Pass" : "Fail";
Precedence determines evaluation order in expressions
| Level | Operators | Associativity |
|---|---|---|
| 1 | () [] . | Left to Right |
| 2 | ++ -- ! ~ | Right to Left |
| 3 | * / % | Left to Right |
| 4 | + - | Left to Right |
| 5 | << >> >>> | Left to Right |
| 6 | < <= > >= | Left to Right |
| 7 | == != | Left to Right |
| 8 | & | Left to Right |
| 9 | ^ | Left to Right |
| 10 | | | Left to Right |
| 11 | && | Left to Right |
| 12 | || | Left to Right |
| 13 | ?: | Right to Left |
| 14 | = += -= *= | Right to Left |
int result = 5 + 3 * 2; // 11 (not 16)
int result2 = (5 + 3) * 2; // 16
boolean check = 10 > 5 && 3 < 7; // true
boolean check2 = !false || true && false;
// true (! has higher precedence)
int x = 10;
int y = ++x * 2; // 22 (++x evaluated first)
Tip: Use parentheses () to make intentions clear
Selection statements execute different code based on conditions
Single condition
Two-way selection
Multi-way selection
if (condition) {
// code to execute if true
}
int age = 20;
if (age >= 18) {
System.out.println("You can vote!");
}
if (condition) {
// code if true
} else {
// code if false
}
if (grade >= 90) {
System.out.println("A");
} else if (grade >= 80) {
System.out.println("B");
} else {
System.out.println("C");
}
switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other day");
}
Important: Use break to prevent fall-through. default is optional.
Loops execute code repeatedly based on conditions
Entry-controlled
Exit-controlled
Counter-controlled
// Syntax
while (condition) {
// code to repeat
}
// Example
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
// Output: 1 2 3 4 5
// Syntax
do {
// code to repeat
} while (condition);
// Example
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
// Output: 1 2 3 4 5
Difference: do-while executes at least once, while may not execute at all
// Syntax
for (initialization; condition; update) {
// code to repeat
}
// Example
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
// Nested loops
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(i + "," + j + " ");
}
System.out.println();
}
// Syntax
for (dataType variable : collection) {
// code using variable
}
// Array example
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
// String array example
String[] names = {"Alice", "Bob", "Charlie"};
for (String name : names) {
System.out.println("Hello " + name);
}
Jump statements transfer control to different parts of the program
Exit loop/switch
Skip iteration
Exit method
// Exit loop when condition met
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit loop
}
System.out.println(i);
}
// Output: 1 2 3 4
// In switch statement
switch (day) {
case 1:
System.out.println("Monday");
break; // Exit switch
case 2:
System.out.println("Tuesday");
break;
}
// Skip current iteration
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip rest of iteration
}
System.out.println(i);
}
// Output: 1 2 4 5
// Skip even numbers
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i); // Only odd numbers
}
public static int add(int a, int b) {
return a + b; // Return sum
}
public static boolean isEven(int num) {
return num % 2 == 0; // Return boolean
}
public static String getGrade(int score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
return "C";
}
public static void printNumbers(int max) {
for (int i = 1; i <= max; i++) {
if (i > 100) {
return; // Exit method early
}
System.out.println(i);
}
// Implicit return here
}
public static void main(String[] args) {
printNumbers(5);
return; // Optional in main
}
Next: Object-Oriented Programming Fundamentals
Ready to explore Object-Oriented Programming!