4 Replies - 6269 Views - Last Post: 19 April 2009 - 07:50 AM

#1 confusedbycode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-April 09

how to know when to use which statement

Post icon  Posted 16 April 2009 - 05:39 PM

I get the set as real,
I am lost on when to use Case,
or While, or then.
Can anyone help clear my confusion?
Thanks, :blink:
Is This A Good Question/Topic? 0
  • +

Replies To: how to know when to use which statement

#2 Core   User is offline

  • D.I.C Lover
  • member icon

Reputation: 785
  • View blog
  • Posts: 5,101
  • Joined: 08-December 08

Re: how to know when to use which statement

Posted 16 April 2009 - 06:18 PM

It really depends on the situation. You should see more information on how those statements are used in different situations and pick the most effective one.

I'm going to give a simple example - I was working on an application in which the user picked either a YES or NO response. Therefore, I needed somehow to check which one was selected. I used the IF statement, since there are only two choices. But if I would have to choose between YES, NO, I Don't Know and Ignore Question, then I would use Select Case, since there are more verification parameters.
Was This Post Helpful? 0
  • +
  • -

#3 liyam   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 16-April 09

Re: how to know when to use which statement

Posted 16 April 2009 - 11:49 PM

Actually, it depends on preference.

if you can use nested if else statement then go for it. if you think it's easier to use a select statement then go for it.

for looping statements, on using "for each" or "while" has a significant difference,

If you know how many times you needed to loop the statement, then use a for each loop statement. If you want to loop until a certain condition was met then use a while looping statement.

This post has been edited by liyam: 16 April 2009 - 11:55 PM

Was This Post Helpful? 1
  • +
  • -

#4 confusedbycode   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 16-April 09

Re: how to know when to use which statement

Posted 17 April 2009 - 04:18 PM

Thank you all for you help. Your explained it in so much easier to understand terms then what I have been getting from my instructor (figure it out for your self).
Thanks again,
Was This Post Helpful? 0
  • +
  • -

#5 sudoNut   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 21
  • Joined: 19-April 09

Re: how to know when to use which statement

Posted 19 April 2009 - 07:50 AM

As Core was saying it depends what you are doing.

Think as code as an extension of natural language. Say I want to continue eating until full this could be expressed by:

 while(!full){ eatMore() }


Or say I have multiple choices of food

 
switch(foodType) {
					  
   case "banana": peel();
						  eat();
						  break();

   case "apple": wash()
					   eat();
					   break();

   default: eat();
			   break();

}



These are rather crude examples but you get the picture. Have a look at some Java control examples at http://math.hws.edu/...otes/c3/s3.html

BTW your instructor is not doing his job by the sounds of it! Complain if he continues to fob you off like that

This post has been edited by sudoNut: 19 April 2009 - 07:51 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1