In "Available" tab, check off "NppExec" and go through the installation process.
Upon coding Java syntax, press the F6 key to bring up the "Execute..." command box.
Change the current directory to D: (or any parent folder that holds your source code)
and then move to programs (or any subfolder that holds your source code).
i.e. cd d:
i.e. cd programs
Now execute the Java file.
i.e. javac HelloWorld.java java HelloWorld
Skip to steps 3 - 5 if "NppExec" is already installed.
Skip to step 5 to execute the Java file that is already in the desired directory.
Module 2: Conditionals and Loops
I. Conditional Statements
Decision Making
Conditional statements are used to perform different actions based on different conditions.
The if statement is one of the most frequently used conditional statements.
If the if statement's condition expression evaluates to true, the block of code inside the if
statement is executed. If the expression is found to be false, the first set of code after the end of the
if statement (after the closing curly brace) is executed. Syntax:
if (condition) {
// Executes when the condition is true
}
Any of the following comparison operators may be used to form the condition:
< less than
> greater than
!= not equal to
== equal to
<= less than or equal to
>= greater than or equal to
For example:
int x = 7; if (x < 42) {
System.out.println("Hi");
}
Remember that you need to use two equal signs (==) to test for equality, since a single equal sign is the assignment operator.
if...else Statements
An if statement can be followed by an optional else statement, which executes when the condition evaluates to false. For example:
int age = 30;
if (age < 16) {
System.out.println("Too Young");
} else {
System.out.println("Welcome!");
}
// Outputs "Welcome!"
As age equals 30, the condition in the if statement evaluates to false and the
else statement is executed.
You can use one if-else statement inside another if or else statement. For example:
int age = 25;
if (age > 0) {
if (age > 16) {
System.out.println("Welcome!");
} else {
System.out.println("Too Young");
}
} else {
System.out.println("Error");
}
// Outputs "Welcome!"
You can nest as many if-else statements as you want.
Logical operators are used to combine multiple conditions.
Let's say you wanted your program to output "Welcome!" only when the variable age is greater
than 18 and the variable money is greater than 500.
One way to accomplish this is to use nested if statements:
if (age > 18) {
if (money > 500) {
System.out.println("Welcome!");
}
}
However, using the AND logical operator (&&) is a better way:
if (age > 18 && money > 500) {
System.out.println("Welcome!");
}
If both operands of the AND operator are true, then the condition becomes true.
The OR Operator
The OR operator (||) checks if any one of the conditions is true.
The condition becomes true, if any one of the operands evaluates to true. For example:
The code above will print "Welcome!" if age is greater than 18 or if money is greater than 500.
The NOT (!) logical operator is used to reverse the logical state of its operand. If a condition is
true, the NOT logical operator will make it false. Example:
int age = 25;
if (!(age > 18)) {
System.out.println("Too Young");
} else {
System.out.println("Welcome!");
}
// Outputs "Welcome!"
!(age > 18) reads as "if age is NOT greater than 18".
A switch statement tests a variable for equality against a list of values. Each value is called a
case, and the variable being switched on is checked for each case. Syntax:
switch (expression) { case value1:
// Statements break; // optional case value2:
// Statements break; // optional
// You can have any number of case statements. default: // Optional
// Statements
}
When the variable being switched on is equal to a case, the statements following that case will
execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the
next line after the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will
fall through to subsequent cases until a break is reached.
The example below tests a day against a set of values and prints a corresponding message.
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;
}
// Outputs "Wednesday"
You can have any number of case statements within a switch. Each case is followed
by the comparison value and a colon.
The default Statement
A switch statement can have an optional default case.
The default case can be used for performing a task when none of the cases are matched.
For example:
int day = 3;
switch (day) {
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break; default:
System.out.println("Weekday");
}
// Outputs "Weekday"
No break is needed in the default case, as it is always the last statement in the switch.
The while loops check for the condition x > 0. If it evaluates to true, it executes the statements within
its body. Then it checks for the statement again and repeats.
Notice the statement x--. This decrements x each time the loop runs, and makes the loop
stop when x reaches 0.
Without the statement, the loop would run forever.
When the expression is tested and the result is false, the loop body is skipped and the first statement after
the while loop is executed.
Example:
int x = 6;
while (x < 10)
{
System.out.println(x);
x++;
}
System.out.println("Loop ended");
/*
6
7
8
9
Loop ended
*/
Notice that the last print
method
is out of the while scope.
Another loop structure is the for loop. A for loop allows you to efficiently write a loop that needs to
execute a specific number of times. Syntax:
for (initialization; condition; increment/decrement) {
statement(s)
}
Initialization: Expression executes only once during the beginning of loop.
Condition: Is evaluated each time the loop iterates. The loop executes the statement repeatedly, until
this condition returns false.
Increment/Decrement: Executes after each iteration of the loop.
The following example prints the numbers 1 through 5.
for (int x = 1; x <= 5; x++) {
System.out.println(x);
}
/* Outputs
1
2
3
4
5
*/
This initializes x to the value 1, and repeatedly prints the value of x, until the condition x <= 5 becomes false.
On each iteration, the statement x++ is executed, incrementing x by one.
Notice the semicolon (;) after initialization and condition in the syntax.
You can have any type of condition and any type of increment statements in the for loop.
The example below prints only the even values between 0 and 10:
for (int x = 0; x <= 10; x = x + 2) {
System.out.println(x);
}
/*
0
2
4
6
8
10
*/
A for loop is best when the starting and ending numbers are known.
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed
to execute at least one time. Example:
int x = 1; do {
System.out.println(x);
x++;
} while(x < 5);
/*
1
2
3
4
*/
Notice that the condition appears at the end of the loop, so the statements in the loop execute
once before it is tested.
Even with a false condition, the code will run once. Example:
int x = 1; do {
System.out.println(x);
x++;
} while(x < 0);
// Outputs 1
Notice that in do...while loops, the while is just condition and doesn't have a body itself.
Loop Control Statements
The break and continue statements change the loop's execution flow.
The break statement terminates the loop and transfers execution to the statement immediately following the loop. Example:int x = 1;
while (x > 0) {
System.out.println(x);
if (x == 4) { break;
}
x++;
}
/* Outputs
1
2
3
4
*/
The continue statement causes the loop to skip the remainder of its body and then immediately retest
its condition prior to reiterating. In other words, it makes the loop skip to its next iteration. Example:
for (int x = 10; x <= 40; x = x + 10) {
if (x == 30) { continue;
}
System.out.println(x);
}
/* Outputs
10
20
40
*/
As you can see, the above code skips the value of 30, as directed by the continue statement.
So the code will be passed to get executed after receiving true from both conditions in the && operator.
|| is the "or" operator.
T&&T = T
T&&F = T
F&&T = T
F&&F = F
Any one right condition will let you execute the code.
Boolean Operators
There is another operator called "XOR" (A.K.A Exclusive OR). It functions like a normal OR except when both conditions are true:
boolean a = true;
boolean b = true;
if (a ^ b) {
System.out.println("A single line");
} // it won't print a line since both conditions equals false
"or", "not", "and" are fundamental
There are some more ::: NAND, NOR, XOR
// these two are universal because "or", "not", "and" can be made using them
NAND means opposite of boolean value which comes after "&&".
NOR means opposite of boolean value which comes after "OR".
Explanation for XOR:
Suppose there are 2 boolean values (a, b), then XOR will return {(!a&&b)or(a&&!b)}
Rules for switch statements
The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums.
You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch.
The default case can be used for performing a task when none of the cases are matched.
Loops and Statements
If: A statement than runs whatever is in the block if the condition is true.
While: A loop that will continue to run the block of code and repeat, until the condition is evaluated as being false.
For: A loop that is used when the starting and ending values are known.
Do While: A loop that is used when you need to tell the program: do something, and if it works, then keep doing it until it doesn't work.
Steps on how the "for" loop works
First, initialization is performed (i = 0)
The check is performed (i < n)
THE CODE IN THE LOOP IS EXECUTED
The value is incremented
Repeat step 2- 4
Difference between "while" and "for" loops
Use "while" when you have a condition in mind, but don't know the number of iterations to meet that condition.
Use "for" when you know the number of iterations, and/or you know the starting and ending points of the loop.
Before writing your "while" loop, you must have your variable (index) declared.
When using the "for" loop, the variable (index) is declared within the "for" statement, not prior.