Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

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
Avatar of gudii9
gudii9
Flag of United States of America image

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);
		System.out.println(",");
	}

}
}

Open in new window


i wonder above code is not printing anything. There is no error either. please advise
Avatar of chaau
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.
Avatar of gudii9

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
Avatar of gudii9

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(",");
	}

}
}

Open in new window


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 advise
Why 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);
}

Open in new window



i tried that but still inner if is not indenting deep enough to differentiate outer if with iner if
It 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 advise
Of 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.
Avatar of gudii9

ASKER

i see your points. I fixed those } issues as below
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(",");
		}

	}
}

Open in new window


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 advise
Again, there is NO inner if statement in your code so the indenting is correct as it is.
Avatar of gudii9

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");
				}

	}

}

Open in new window


/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...

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");
				}
			}
		}

	}

}

Open in new window


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.
Avatar of gudii9

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 loop
Maybe 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 ");}

Open in new window


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!
Avatar of gudii9

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
Avatar of gudii9

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 ");}??

Open in new window


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
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

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.
Avatar of gudii9

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");
              }

	
}
}

Open in new window


it all worked fine as above
num is between 10 and 13 and odd
Avatar of gudii9

ASKER

looks like inner if i cannot have without brackets having two system out statements as below
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");
              }

	
}
}

Open in new window


please advise
Avatar of gudii9

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 {}
I think you've got it! ;)