gudii9
asked on
if , for loop auto populate in eclipse
if loop auto populate in eclipse i thought i have to click space after typing if. But i found i have to press control space. I woder when to type press to see available options when to type control space. also how to format code in eclipse when we have inner if loops as attached. As of now i clicked source and format option of eclipse which did not indent clearly.
Also for loop same control space approach did not show any for(i=0;i<n;i++) kind of regular for loop syntax to select.
please advise
ifLoop.jpg
ifLoop2.jpg
ForLoop.jpg
Also for loop same control space approach did not show any for(i=0;i<n;i++) kind of regular for loop syntax to select.
please advise
ifLoop.jpg
ifLoop2.jpg
ForLoop.jpg
Do you pass any arguments to the main()? I mean, when you start your application do you start it with any arguments?
when to type press to see available options when to type control space.It is only ever control+space to see content assist. I don't know what you are thinking of with just space, if you can describe where you have seen this that would help.
As of now i clicked source and format option of eclipse which did not indent clearly.You could try "selecting all" first before trying the format command.
Also for loop same control space approach did not show any for(i=0;i<n;i++) kind of regular for loop syntax to select.From the screenshot it looks like you typed for OUTSIDE the main method. Code is not allowed outside of a method and so Eclipse didn't suggest to auto-complete code because it would be in the wrong location. Most of these control+space content assist things are context sensitive, you have to type it in the right place. So try typing "for" inside the main method body and then press control+space and you should get the content assist.
ASKER
From the screenshot it looks like you typed for OUTSIDE the main method. Code is not allowed outside of a method and so Eclipse didn't suggest to auto-complete code because it would be in the wrong location. Most of these control+space content assist things are context sensitive, you have to type it in the right place. So try typing "for" inside the main method body and then press control+space and you should get the content assist.
when i commented above if block cod i see content assist for for loop which is bit weird. I still do not see output for for loop printing numbers till 20. Not sure why . please advise
You could try "selecting all" first before trying the format command.
i tried that but still inner if is not indenting deep enough to differentiate outer if with iner if
ASKER
public class Test56 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 100;
if (a < 200) {
System.out.println(" true part");
}
if (a < 150) {
System.out.println("level1");
}
if (a < 125) {
System.out.println("level2");
}
else {
System.out.println("faslse");
}
}
for (int i = 0; i < args.length; i++) {
System.out.println("i values is"+i);
System.out.println(",");
}
}
}
when i keep both if and for loop getting compilation error saying
Syntax error on token "}", { expected after this token
i wonder what that means. Cannot i have if and for together. Please advise
when i commented above if block cod i see content assist for for loop which is bit weird. I still do not see output for for loop printing numbers till 20. Not sure why . please adviseWhy would you be expecting 20? What that loop is doing is executing once through the loop for every command line argument that you pass to your program. And if you are not passing any arguments you will get no output. If you just want to see the numbers up to 20, try this...
for (int i = 1; i <= 20; i++) {
System.out.println("Number: " + i);
}
i tried that but still inner if is not indenting deep enough to differentiate outer if with iner ifIt would, it's just that you don't have any inner loops to be further indented! Each of those if statements is at the same level, there is no inner/outer with what you have posted.
i wonder what that means. Cannot i have if and for together. Please adviseOf course you can have them both together. The problem is that you have an extra } at the very end of your code. If you remove that, then you will find that you still have the same problem where the for loop is NOT inside the main method. Your main method starts on line 6 and finished on line 23. You can see this easily due to the (correct) indentation that you have. Your for loop comes after line 23 and so is not within any method, and you can't have code that is NOT part of a method.
ASKER
i see your points. I fixed those } issues as below
I still not sure how can i modify my code to see and indent inner if loop. please advise
public class Test56 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 139;
if (a < 200) {
System.out.println(" true part");
}
if (a < 150) {
System.out.println("level1");
}
if (a < 140) {
System.out.println("level1.1");
}
if (a < 125) {
System.out.println("level2");
}
else {
System.out.println("faslse");
}
for (int i = 0; i < 20; i++) {
System.out.println("i values is" + i);
System.out.println(",");
}
}
}
I still not sure how can i modify my code to see and indent inner if loop. please advise
I still not sure how can i modify my code to see and indent inner if loop. please adviseAgain, there is NO inner if statement in your code so the indenting is correct as it is.
ASKER
public class qq111 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 9;
if (num > 0)
if (num < 10)
if (num % 2 == 0)
System.out.println("num is between 0 and 10 and even");
else {
System.out.println("num is between 0 and 10 and odd");
}
}
}
what is difference between above withouth {} in outer if and with {} in outer else as below.
In above case how it is working without {} and how to put outer else in this case.
public class qq111 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 9;
if (num > 0)
if (num < 10)
if (num % 2 == 0)
System.out.println("num is between 0 and 10 and even");
else {
System.out.println("num is between 0 and 10 and odd");
}
}
}
/now i see it is indenting properly. please advise
The two pieces of code appear exactly the same to me but I guess you meant one of them to look like the below...
Well, there is NO difference in this case. In this case you can do it without the braces because there is only one statement inside the if statement. However, I would suggest that it is good practise to always use { and } in your code. You can easily get into trouble if you leave them out.
And yes, now you have inner if statements and so Eclipse is indenting the code as you were expecting.
public class qq111 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = 9;
if (num > 0) {
if (num < 10) {
if (num % 2 == 0) {
System.out.println("num is between 0 and 10 and even");
} else {
System.out.println("num is between 0 and 10 and odd");
}
}
}
}
}
Well, there is NO difference in this case. In this case you can do it without the braces because there is only one statement inside the if statement. However, I would suggest that it is good practise to always use { and } in your code. You can easily get into trouble if you leave them out.
And yes, now you have inner if statements and so Eclipse is indenting the code as you were expecting.
ASKER
Well, there is NO difference in this case. In this case you can do it without the braces because there is only one statement inside the if statement.
i thought there are more than one statement as bolded below right within outer if loop
if (num < 10) {
if (num % 2 == 0) {
System.out.println("num is between 0 and 10 and even");
} else {
System.out.println("num is between 0 and 10 and odd");
}
}//else{ System.out.println("num is more than 10 ");}??
How to put outer else (else{ System.out.println("num is more than 10 ");}??) as above if i do not have those {} in outer if loop? please advise
i thought there are more than one statement as bolded below right within outer if loopMaybe I should have said statement block. The bolded part is ONE statement block, even though it may contain may individual statements.
How to put outer else (else{ System.out.println("num is more than 10 ");}??) as above if i do not have those {} in outer if loop?You can do this if only for your learning about the subject, like below...
if (num < 10)
if (num % 2 == 0) {
System.out.println("num is between 0 and 10 and even");
} else {
System.out.println("num is between 0 and 10 and odd");
}
else{ System.out.println("num is more than 10 ");}
But as I explained before, if I were you I would not concentrate on this at all. Just use { and } everywhere and you don't have to even think about these things!
ASKER
Maybe I should have said statement block. The bolded part is ONE statement block, even though it may contain may individual statements.
i see your point now. bolded one is outer if statement block right more specifically(if (num < 10) statement block). please advise
ASKER
if (num < 10)
[b] if (num % 2 == 0) {
System.out.println("num is between 0 and 10 and even");
} else {
System.out.println("num is between 0 and 10 and odd");
}[/b]
//else{ System.out.println("num is more than 10 ");}??
can i write outer if block as above without openeing and closing { }
if Yes how can put else loop to it.
I understand putting { } is best thing but just wondering if we can write if block with multiple statements without { }. Does compiler know where block starting and ending withouth
{}
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
: you can only leave the { and } out, in either the if part or the else part, if they contain only 1 statement.
else also can have inner if-else?
inner if-else counts as only one statement in terms of what the outer if statement sees.
ASKER
public class TEst111 {
/**
* @param args
*/
public static void main(String[] args) {
int num=11;
// TODO Auto-generated method stub
if (num < 10)
if (num % 2 == 0) {
System.out.println("num is between 0 and 10 and even");
System.out.println("test inner if else without brakets ");
} else {
System.out.println("num is between 0 and 10 and odd");
}
else
if (num < 13)
if (num % 2 == 0) {
System.out.println("num is between 10 and 13 and even");
} else {
System.out.println("num is between 10 and 13 and odd");
}
}
}
it all worked fine as above
num is between 10 and 13 and odd
ASKER
looks like inner if i cannot have without brackets having two system out statements as below
please advise
public class TEst111 {
/**
* @param args
*/
public static void main(String[] args) {
int num=11;
// TODO Auto-generated method stub
if (num < 10)
if (num % 2 == 0)
System.out.println("num is between 0 and 10 and even");
System.out.println("test inner if else without brakets ");
else {
System.out.println("num is between 0 and 10 and odd");
}
else
if (num < 13)
if (num % 2 == 0) {
System.out.println("num is between 10 and 13 and even");
} else {
System.out.println("num is between 10 and 13 and odd");
}
}
}
please advise
ASKER
An if statement with its optional else statement is being treated by the compiler as one statement. so uter loop it does not matter if { } are there or not.
where as inner if loop if i have two lines of sysouts they are consider as separate two statements which makes comiler to complain saying need {}
where as inner if loop if i have two lines of sysouts they are consider as separate two statements which makes comiler to complain saying need {}
I think you've got it! ;)
ASKER
Open in new window
i wonder above code is not printing anything. There is no error either. please advise