what does the statement means in Java? [duplicate] - java

This question already has answers here:
Giving name to a loop
(6 answers)
"loop:" in Java code. What is this, and why does it compile?
(12 answers)
Can java labels be used appropriately outside of for loops?
(2 answers)
Closed 3 years ago.
I find a strange statement while reading the openjdk resources.
enter image description here
what does the "found:{}" means?

found:{} is a label in Java mainly used for goto statement but since Java doesn't use goto it can be used for break and continue statement. Here is an example of using a label in Java.
loop:
for (int i = 0; i < 10; i++) {
for(int j=0; j<10; j++)
{
if(j==5)
{
break loop;
}
System.out.println(j);
}
}
When j reach 5 it will trigger break to outside loop label.

Related

Can I use the loop iterator outside the for statement? [duplicate]

This question already has answers here:
Is the scope of a variable initialized in a for loop declaration actually more than just block scope?
(4 answers)
Scope of variable declared inside a for loop
(5 answers)
Misplaced semicolon in for loop [duplicate]
(5 answers)
Closed 11 months ago.
So I have a variable lopvr that I declared inside what I am going to call the "for statement". This is what I am referring to:
for (int lopvr=1;lopvr==1;);
I would like to know if I can use the variable outside the "for statement", like this:
for (int lopvr=1;lopvr==1;);
{
System.out.println("Enter the passcode:");
Scanner sc= new Scanner(System.in);
codeEntered= sc.nextInt();
int passcode = 1409, attempts = 4;
if (passcode == codeEntered) {
System.out.println("Correct");
//other code
lopvr=0;
}
else{//other code
}
For clarification, I am trying to change lopvr, which was declared INSIDE the "for statement", to set the value to 0, OUTSIDE the "for statement", to end the loop when the right conditions are met (You get the passcode right)

What does an for (;;) loop do? [duplicate]

This question already has answers here:
Java: for(;;) vs. while(true)
(9 answers)
Closed 1 year ago.
I have seen a weird version of the for loop where only (;;) is used to write something, for example:
for (;;) {
System.out.println("dabarkai");
}
My guess is that it behaves similarly like a while(true) loop. But if there are some diferences in their actions then your more than welcome to share them here.
It's an "infinite" loop, basically the same as while(true)
If you don't stop it manually (or by throwing Exception) it will run forever.
To stop it you can use return:
int i = 0;
for(;;) {
i++;
if(i == 10) {
return;
}
}

How to translate this java code to flowchart [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have my homework but I have a problem understanding this code because it is new to me, it is called Labeled break statement, I'm having a hard time to translate the code to a flowchart
Code:
1. class LabeledBreak {
2. public static void main(String[] args) {
3.
4. first:
5. for( int i = 1; i < 5; i++) {
6.
7. second:
8. for(int j = 1; j < 3; j ++ ) {
9. System.out.println("i = " + i + "; j = " +j);
10.
11. if ( i == 2)
12. break first;
13. }
14. }
15. }
16.}
I hope someone can help me to translate this into a flowchart and also an explanation will do also so that I can understand how this labeled break statement works.
So basically what this does is print out i and j Here is the flowchart that i created
A break statement bings the control of the program out of the innermost loop in which the break statement is written.
A labeled break is written as break <label>;. Here label is an identifier, generally given to a loop. In the given program, the outer(i) loop is given the label first and the inner(j) loop is given the label second. So when the break first; statement is encountered, the control goes out of the loop labeled as first, i.e., the outer loop.
So in your program, control goes to line number 15.

what code can i use to clear the screen(java)? [duplicate]

This question already has answers here:
commands in java to clear the screen
(11 answers)
Closed 5 years ago.
I'm writing a text-based unit converter and I want to be able to run a clear command so that the window that the program I want to know how I can do it.
I think you can System.exec("clear") but that depends on what operating system the program is running on.
This is my choice for clear:
public static void clearConsole() {
String value = "\n\r";
for (int i = 0; i < 5; ++i) {
value = value + value;
System.out.printf(value);
}
}

The monadic decrement operator --i behaves like i-- [Android Studio (Java)] [duplicate]

This question already has answers here:
How does a for loop work, specifically for(;;)?
(6 answers)
Closed 7 years ago.
After a lot of 'ArrayIndexOutOfBound' messages, this for-loop works for me, but only because I compensated with (i-1).
Doesn't --i(t) means: _before entering the loop, decrement by one?
for (int i = offset; i > 0; --i) {
if (Character.isLetter(lnCmplt.charAt(i - 1))) {
selStart -= 1;
Log.i("1.for (i-1)= ", (i-1)+" char=["+lnCmplt.charAt(i-1)+"] selStart= "+selStart);
}
}
Log.i console output (it is real, only trimmed):
lnStart= 492 lnEnd= 506 offset= 7 // from Log line before
I/1.for (i-1)= 6 char=[e] selStart= 6 //<-- this value has to be 5
(i=7 ; --i (=6) ; i-1 (=5)
I/1.for (i-1)= 5 char=[k] selStart= 5 ...
[Edit] Solution: On first loop the x-crement isn't executed.
It's not a crazy idea, but no, it doesn't.
--i is executed before the rest of the statement, but it doesn't change when the statement is executed.
So it will work here:
int i = 0;
System.out.println(--i);
But in a for loop construct it is completely equivalent to using i--.

Categories

Resources