What is the Difference Between i++ and ++i in Java?
++i and i++ both increment the value of i by 1 but in a different way. If ++ precedes the variable, it is called pre-increment operator and it comes after a variable, it is called post-increment operator.
Increment in java is performed in two ways,
1) Post-Increment (i++): we use i++ in our statement if we want to use the current value, and then we want to increment the value of i by 1.
2) Pre-Increment(++i): We use ++i in our statement if we want to increment the value of i by 1 and then use it in our statement.
REFERENCE:https://www.geeksforgeeks.org/what-is-the-difference-between-i-and-i-in-java/
Example
int i = 3;
int a = i++; // a = 3, i = 4
int b = ++a; // b = 4, a = 4
Java For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop:
Syntax
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1: is executed (one time) before the execution of the code block. (Statement 1 (int i = 1
)* is executed once before the loop starts. It initializes the loop counter i
to 1.)
Statement 2: defines the condition for executing the code block.
(Condition Check*: After the initialization, the condition i <= 5
is checked. If the condition is true, the code block inside the loop executes.)
Statement 3 :is executed (every time) after the code block has been executed.(Increment/Decrement: After each iteration of the loop, the statement i++
(incrementing the value of i
) is executed.)
Flow chat
The example below will print the numbers 0 to 4:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
output:01234
Reference:https://www.w3schools.com/java/java_for_loop.asp
package Afterfeb4;
public class dowhile {
public static void main(String[] args) {
for (int no1 = 1; no1 <= 4; no1++)// column--vertical--->4column
{
for (int no2 = 1; no2 <= 5; no2++)// row 1 2 3 4 5--->5row
{
System.out.print(no2 + " ");
}
System.out.println();
}
}
}
Output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
LCM
package Afterfeb4;
public class LCMnumbe {
public static void main(String[] args) {
int a = 10;
int b = 40;
int big = 0;
if (a > b) {
big = a;
} else {
big = b;
}
for (int limit=400; big < limit; big += 1) {
if (big % a == 0 && big % b == 0) {
System.out.println(big);
// break;
}
}
}
}
Output:
40
80
120
160
200
240
280
320
360
400
GCD
package B14;
public class num36 {
static int gcd;
public static void main(String[] args) {
int a = 400;
int b = 200;
int small=0;
if(a<b)
small=a;
else
small=b;
for(int count = 2;count<=small;count++) // count =2,3,4,5,6 to 200
{
if (a % count == 0 && b % count == 0) // WHy %by count mean count % by1,2,3,4,5,6to 100,120
{
System.out.println(count);
gcd = count; // 600Greatest common divisor
}
}
// System.out.println("GCD of 100 & 120 = " + gcd);
// int lcm = (a * b) / gcd;
// System.out.println("LCM : " + lcm);
}
}
Output:
2
4
5
8
10
20
25
40
50
100
200
emirp number using for:
package Afterfeb4;
public class emirpusingfor {
public static void main(String[] args) {
int a = 11;
boolean prime = findprime(a);
if (prime == true) {
int number = findreverse(a);
findprime(number);
}
}
private static int findreverse(int a) {
int num = 0;
for (; a > 0;) // we can give condition only in for ; a>0 ;
{
num = (num * 10) + a % 10;// num 0 then num will 10 then 10+1=11
a = a / 10;// sep 9 & 1
}
System.out.println("reverse num = " + num);
return num;
}
private static boolean findprime(int a) {
boolean prime = true;
for (int div = 2; a > div; div++)
{
if (a % div == 0) {
{
System.out.println("not prime");
System.out.println("Given num is not prime because it divided by = " + div);
prime = false;
break;
}
}
}
System.out.println(prime);
if (prime == true) {
System.out.println("prime");
}
return prime;
}
}
output:
If given number is prime:
true
prime
reverse num = 11
true
prime
If given number is even output will be:
not prime
Given num is not prime because it divided by = 2
false
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:
Example
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
output:01234
package Afterfeb4;
public class dowhile {
public static void main(String[] args) {
int no = 0;
do {
System.out.println(no);
no = no + 1;
} while (no <= 5);
}
}
Output:
0
1
2
3
4
5
Reference:https://www.w3schools.com/java/java_while_loop_do.asp
Top comments (0)