hi guys i am new to java and i barly learning i got some slides that teachs java from the start i started good at declaring values like ingers, strings, etc.. and made simple programs that counts sum of two numbers and the avarge and now i am stuck at the if statments with no success.. i tried understanding it by my self by apply the codes in this exercise but no use if someone can answer this with or without explaining i would appreciate it very much atleast i will got the ideas


1. Write the if-statements(s) to perform the following:
If x is even and y is odd then print their product, otherwise if x is odd print the sum of x and y, and assign x to y.

2. Find the syntax and logical errors in the following statements and correct them:
a. The intended action is: increment x if a is less than 3
if a < 3 x = x+1;
b. The intended action is: increment a and b if a is neither 3 nor 4, otherwise decrement a and b
if (a != 3 || 4)
a = a+1
b = b+1
else a = a-1
b = b-1


3. The following code is supposed to increment a and b if a is greater or equal to 3 and less or equal to 5, otherwise a has to be decremented. Find the logical errors and correct them.
if ((a < 3) && a > 5)
a = a-1;
else a++;
b++;

4. What would be output of the following statements:
if (x < 6 || y > 4) System.out.println(“A”);
if ( x >= 8 && y < 6) System.out.println(“B”);
else
if (x < 10 ) System.out.println(“C”);
else System.out.println(“D”);
if the following assignments are in effect:
a. int x = 4, y = 7;
b. int x = 9, y = 3;
c. int x = 9, y = 10;
d. int x = 10, y = 6;

Thanks
Jeff

Jeff - pick one of those problems that you'd like to work on, and give us some idea of how you'd start approaching it. It's usually best to break a problem down into simple steps. For example, the first one:

If x is even and y is odd then print their product, otherwise if x is odd print the sum of x and y, and assign x to y.

would naturally break down into

[If [x is even] and [y is odd]] then [print [their product]],
[otherwise [if x is odd] [[print the [sum of x and y]] and assign x to y]]

which almost looks like pseudocode.

That can be simplified a little, since "x is odd" is the converse of "x is even" - if "x is even" is false, then "x is odd" must be true. Try translating the pseudocode into java, and then see if you can make the simplified version happen.

Question 1:

if(x%2 == 0 && y%2 !=0)
{
	System.out.println(x*y);
}
else
{
	System.out.println(x+y);
	x = y;
}

Question 2.a:

if(a < 3)
{
	x++;//Or x = x + 1
}

Question 2.b:

if(a == 3 || a == 4)
{
	a = a - 1;
	b = b - 1;
}
else
{
	a = a + 1;
	b = b + 1;
}

Question 3:

if(a >= 3 && a <=5)
{
	a = a + 1;
	b = b + 1;
}
else
{
	a = a - 1;
}

Question 4:
Tell me, do the variables under question 4 correspond to each of the statements the way you have arranged them?

commented: You have to write code to learn it - solving the problems for him won't help! +0
commented: best programmer +0

Cossay - I'm sure this is well-meant, but someone who's trying to learn has to solve their own problems. Let him catch his own fish.

Okay. Thanks.

first thing i wanna say Thank you so much Jon and cossay you are the best i did reviewed the answers it makes sense now really thanks cossay!!!!


About question 4
4. What would be output of the following statements:

if (x < 6 || y > 4) System.out.println(“A”);
if ( x >= 8 && y < 6) System.out.println(“B”);
else
if (x < 10 ) System.out.println(“C”);
else System.out.println(“D”);


means each time apply this values to x and y
a. int x = 4, y = 7; means what would the output of the above program if X and Y was declared as int x = 4 and int y = 7 (same goes to b,c and d) what he needs is the output in each case .

b. int x = 9, y = 3;
c. int x = 9, y = 10;
d. int x = 10, y = 6;

Thanks to cossay answers and Jon.Kiparsky explanation i managed to catch the fish on how the if rule works i answered question 4 can someone
check the answers if its correct i am kind of confused but i hope i got it right!
question 4 Answer
a. the output will be
A
C
b. the out put will be
B

c. the out put will be
A
C

d. the out put will be
A
D

..

So what are the answers?

In (a), if x is 4 and y is seven, what does x<6 evaluate to? How about y>4 ? Those will be boolean values, so you then can ask yourself what the || comes out to. Remember, A || B is true if A is true, or if B is true, or if both are true - it is only false if both A and B are false. Finally, of course you know that the statement following the if will print if the condition is true, so you know what the output will be.
Same logic works for each of these. You want to be able to just look at these and know what they mean, but in order to get there you need to start by unpacking them and working them out from their components. That's how you get the reflex for it.


The else clauses here are a little tricky. Here's how that would look properly blocked:

if (x < 6 || y > 4) 
  {  System.out.println(“A”);  }

if ( x >= 8 && y < 6) 
  {  System.out.println(“B”);  }
else
  if (x < 10 ) 
  {  System.out.println(“C”);  }
  else 
  {  System.out.println(“D”);  }

So you won't get to the first else unless the second if is false. You won't get to the second else unless the third if is false, but you won't even see that one unless you get to the first else.

Good, you beat me to it. I see you got caught up on the else clauses - try looking at them again, with my last post in mind.

Oops, my bad, let me look at that again.

Sorry, I misread. Good on three of the four, but take another look at B.

Many Thanks Jon.kiparsky and cossay i will look at it again and do more exercises in the if conditional statements ant the else too i am glad that i registered in this forum .

ops i just didnt see the second page of the thread

hmmmm let me think for a min about B

Here's something that you might like to know about the logical operators: java requires that && and || are "lazy" in their evaluation. That means that when you have a statement like (A||B) that statement is evaluated from left to right, until its value is known. In this case, if A is true, B is not evaluated, because if A is true than the whole or clause is necessarily true. Likewise, if you have (A&&B) and A is found to be false, the whole and clause is false, so B is never evaluated.

Right now, this is just a neat little fun fact to file away in your mind. Later on, it may come in handy to know this.

ahaaaaaa so the program skip this if clause and continue to the next one because its false! so u mean && is logical and combines two boolean values and returns a boolean which is true if and ONLYif both of its operands are true.. right?

so in case b. it would be like that?

if (x < 6 || y > 4) // Skipped because its both FALSE
{ System.out.println(“A”); }

if ( x >= 8 && y < 6) // Skipped because one is TRUE and the other FALSE so
{ System.out.println(“B”); } (the whole and clause is FALSE)
else
if (x < 10 ) // processed because its TRUE!!!
{ System.out.println(“C”); }
else
{ System.out.println(“D”); }


So the Final output will be
C

so u mean && combines two boolean values and returns a boolean which is true if and ONLYif both of its operands are true.. right?

Exactly that, and your answer is correct.

&& is logical

We say "logical and" and "logical or" to distinguise && and || from "bitwise and" (&)and "bitwise or" (|) - these are another kettle of fish entirely, save those for another day. Actually, it's very likely that you'll never need to use those operators. Depends what sort of programming you end up doing.


If you're curious, the wikipedia article is as good a place to start as any. At least, I didn't see any howlers when I skimmed over it just now.

Sir i got to admit...you are the best teacher i had in my life !
thank you so much Mr.Jon.Kiparsky learned with you very much Sir!
Thnk you again.

Glad to be of help. Now go learn some more stuff, and come back with more questions. There's a lot of folks here who know more than I do...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.