nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:12 PM |
I'm a beginner have a question
which line is invalid
if (x < 5) x++; if (x < 5) { x++; } if x < 5 x++; if (x < 5) if (x > 0) x++;
I want to say the last one cause it says if twice and doesn't say else if
but the brackets around x++; in b are making me question it. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2015 11:13 PM |
| Shouldnt C need brackets around x<5 to specify the inequality |
|
|
| Report Abuse |
|
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:14 PM |
| seems too obvious to me, I'm also learning about if and else if right now so it's probably throwing me off. |
|
|
| Report Abuse |
|
|
stal477
|
  |
| Joined: 15 Jun 2009 |
| Total Posts: 15729 |
|
|
| 07 Oct 2015 11:15 PM |
wtf r u talkin about
- Known as Stal447 - // Add 11k posts to my count // 788,753 // - RSTF NFT - |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2015 11:15 PM |
| Are u learning from codecademy? |
|
|
| Report Abuse |
|
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:16 PM |
@stal Java I'm learning programming and need some help on one question |
|
|
| Report Abuse |
|
|
Xaleo
|
  |
| Joined: 13 Apr 2009 |
| Total Posts: 11864 |
|
|
| 07 Oct 2015 11:16 PM |
I think its the second one because the ; should always come at the end right? I'm not sure really. |
|
|
| Report Abuse |
|
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:16 PM |
No I'm taking a class with my online school Connections. It's a Pearson ebook. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2015 11:17 PM |
| Yeah its either B or C but i personally think C |
|
|
| Report Abuse |
|
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:17 PM |
@Xal That's why I was trying to decide between b and c. c should have extra brackets if it wants to state if twice in a row like that |
|
|
| Report Abuse |
|
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
| |
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:19 PM |
| gonna say b because I really don't think the bracket should be on the inside, I know it shouldn't, I just don't know if I missed something somewhere hence why I'm asking |
|
|
| Report Abuse |
|
|
L2000
|
  |
| Joined: 03 Apr 2008 |
| Total Posts: 77448 |
|
|
| 07 Oct 2015 11:19 PM |
It's a lot easier when u have line breaks
But c wouldn't work D works if 0 < x < 5
If you have the lines, you'll probably see it better
Checks first of If true, goes in, checks second If true, goes in and outputs |
|
|
| Report Abuse |
|
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:20 PM |
@L2 that's all I have. But yeah the parenthesis makes sense too. Explain how B can work then, since the bracket is inside the parenthesis? |
|
|
| Report Abuse |
|
|
| |
|
L2000
|
  |
| Joined: 03 Apr 2008 |
| Total Posts: 77448 |
|
|
| 07 Oct 2015 11:21 PM |
And basically, the brackets mean you're in an if, loop, function, ... If it's just one line you don't need them, but you can still put them in and it'll work |
|
|
| Report Abuse |
|
|
Axarthian
|
  |
| Joined: 31 Jul 2011 |
| Total Posts: 41927 |
|
| |
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:22 PM |
| alright, sounds good thank you |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2015 11:23 PM |
| U can also eliminate C since A and C are the same, but one has parenthesis and one doesn't, meaning one of them cant be right |
|
|
| Report Abuse |
|
|
L2000
|
  |
| Joined: 03 Apr 2008 |
| Total Posts: 77448 |
|
|
| 07 Oct 2015 11:25 PM |
Think of it like a is b, but b is safer If you add more later you'll need those brackets Otherwise you can come into problems
A lot of ppl just put them in go be safe Doesnt matter either way
D is a different condition C needs () |
|
|
| Report Abuse |
|
|
nuke98
|
  |
| Joined: 16 Jul 2008 |
| Total Posts: 25506 |
|
|
| 07 Oct 2015 11:27 PM |
| Okie dokie, makes sense now kitty. Thanks much you guys :) |
|
|
| Report Abuse |
|
|
Cizox
|
  |
| Joined: 25 Dec 2009 |
| Total Posts: 42220 |
|
| |
|
Cizox
|
  |
| Joined: 25 Dec 2009 |
| Total Posts: 42220 |
|
|
| 07 Oct 2015 11:31 PM |
| wouldnt d just error since its trying to say if both x < 5 and x > 0 but instead if makes 2 if statements |
|
|
| Report Abuse |
|
|
jrjojo
|
  |
| Joined: 14 Sep 2008 |
| Total Posts: 30402 |
|
| |
|
L2000
|
  |
| Joined: 03 Apr 2008 |
| Total Posts: 77448 |
|
|
| 07 Oct 2015 11:35 PM |
It's the same thing, just && looks better and is faster to write If you wanted an else on the second if tho u can split it up like that |
|
|
| Report Abuse |
|
|