# Thread: nested if/else; can't dertermine logical mistake

1. Member
Join Date
Jan 2015
Posts
6
Rep Power
0

## nested if/else; can't dertermine logical mistake

Hello,

the title says already where my difficulties are. thanks a lot for any help :3

EDIT: i forgot fo say, the "S" printing part works, but i have just no idea why the others doesn't.
EDIT 2: to make it more clear:
java Monoton 1 3 3 4 & java Monoton 1 3 4 1
=> nothing (my output)

EDIT 3: i forgot to notice, with 1 3 3 4 as parameters it jumps out at if (b < c), which is expected. but it jumps out of the whole if instead just run the else part. thats the essence of my problem.

The Execise:
=> The program should print the following letters;
S, if every number is true bigger than the before,
M, if every number is bigger or equal than the before,
N in the other cases

Examples (from the exercise)
=> java Monoton 0 1 2 4
S
=> java Monoton 1 3 3 4
M
=> java Monoton 1 3 4 1
N

The Execise should done without the use of logical operators or other combined requirements.

Java Code:
```public class Main {

public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
int d = Integer.parseInt(args[3]);

if (a < b) {
if (b < c) {
if (c < d) {
System.out.println("S");
}
}
}
else {
if (a > b) {
if (b > c) {
if (c > d) {
System.out.println("S");
}
}
}
else{
if (a <= b) {
if (b <= c) {
if (c <= d) {
System.out.println("M");
}
}
else if (a >= b) {
if (b >= c) {
if (c >= d) {
System.out.println("M");
}
}
}
}else {
System.out.println("N");
}
}
}
// Ende
}
}```
Last edited by Monte; 01-05-2015 at 02:45 PM. Reason: fixed typo

2. ## Re: nested if/else; can't dertermine logical mistake

How are you debugging the code to see what it is doing?

Can you post what the expected output should be?

Also for debugging add some more else statements to print out messages when the if is false.
Last edited by Norm; 01-05-2015 at 02:19 PM.

3. Member
Join Date
Jan 2015
Posts
6
Rep Power
0

## Re: nested if/else; can't dertermine logical mistake

Im using IntelliJ IDEA for coding & debugging

Im not sure what exactly you mean by "what the expected output should be". The letters are the beginning letters of the mathematical meaning ( i think so at least^^)
=> S: strong monotone increasing
=> M: monotone increasing
=> N: not monotone increasing

Java Code:
```public class Main {

public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
int d = Integer.parseInt(args[3]);

// test one of the two orders of true increasing numbers;
// if it fails at any point, it should jump to the next
// order (else)
if (a < b) {
// when e.g. parameters are 1 3 3 4, it should at the first equal
// number jump to the else part & step forward, but at this point, it
// ends the __whole__ programm, which is not expected.
if (b < c) {
if (c < d) {
System.out.println("S");
}
}
}
else {
//test for the other order of true increasing numbersM
// if it fails, at any point, it should jump to the next else
if (a > b) {
if (b > c) {
if (c > d) {
System.out.println("S");
}
}
}
else{
// test whether any of the previous error is caused by
// two following equal numbers;
if (a <= b) {
if (b <= c) {
if (c <= d) {
System.out.println("M");
}
}
// both directions
else if (a >= b) {
if (b >= c) {
if (c >= d) {
System.out.println("M");
}
}
}
}else {
// if the order is decreasing in an end(=direction), or if
// two (or more) equal numbers are separated with a not
// equal number (which should be all the rest situations)
// => execute
System.out.println("N");
}
}
}
// End
}
}```
Last edited by Monte; 01-05-2015 at 02:54 PM.

4. ## Re: nested if/else; can't dertermine logical mistake

Im using IntelliJ IDEA for coding & debugging
I'm not familiar with that program as a debugger.
Does it allow you to step through the execution of the statements one at a time?
If so, follow the program's execution for a line of data where the results are not as desired. That should show you where the logic is wrong.

Can you post a line of input where the program's output is wrong and add comments to show what the output should be?

nothing (my output)
That can be changed by adding else statements that print messages showing what the code is doing and the values of the variables that caused it to happen.
Last edited by Norm; 01-05-2015 at 03:09 PM.

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: nested if/else; can't dertermine logical mistake

You need to understand if/else blocks. Do you understand why Foo or Bar will never be printed?

Java Code:
```if (true) {
if (false) {
System.out.println("Bar");
}
} else {
if (true) {
System.out.println("Foo");
}
}```
Regards,
Jim

6. Member
Join Date
Jan 2015
Posts
6
Rep Power
0

## Re: nested if/else; can't dertermine logical mistake

@jim829: "Foo" will not be printed, because the if condition was true, but it will only execute (as else) when the condition is false.
"Bar" will not be printed, because the inner condition is false.

=> is my program in this status(a=1, b=3, c=3, d=4) failing, because the first if (a < b) is true, and therefore it isnt necessary to execute the else part? If so, i think i understand now, where my mistake is.

both of you, Norm and jim829, thanks a lot for your great help. :3

7. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: nested if/else; can't dertermine logical mistake

Originally Posted by Monte
=> is my program in this status(a=1, b=3, c=3, d=4) failing, because the first if (a < b) is true, and therefore it isnt necessary to execute the else part?
Correct!

Regards,
Jim

8. Member
Join Date
Jan 2015
Posts
6
Rep Power
0

## Re: nested if/else; can't dertermine logical mistake

It works :)

Thanks again for your great help :)

Java Code:
```public class Main {

public static void main(String[] args) {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
int d = Integer.parseInt(args[3]);
boolean WriteS = false;
boolean WriteM = false;
boolean WriteN = false;

// test one of the two orders of true increasing numbers;
// if it fails at any point, it should jump to the next
// order (else)
if (a < b) {
// when e.g. parameters are 1 3 3 4, it should at the first equal
// number jump to the else part & step forward, but at this point, it
// ends the __whole__ programm, which is not expected.
if (b < c) {
if (c < d) {
WriteS = true;
}
}
}else{
//test for the other order of true increasing numbersM
// if it fails, at any poinyt, it should jump to the next else
if (a > b) {
if (b > c) {
if (c > d) {
WriteS = true;
}
}
}
}

// test whether any of the previous error is caused by
// two following equal numbers;
if (a <= b) {
if (b <= c) {
if (c <= d) {
WriteM = true;
}
}
// both directions
else if (a >= b) {
if (b >= c) {
if (c >= d) {
WriteM = true;
}
}
}
}
else {
// if the order is decreasing in an end(=direction), or if
// two (or more) equal numbers are separated with a not
// equal number (which should be all the rest situations)
// => execute
WriteN = true;
}

if (WriteM) {
if (WriteS) {
System.out.println("S");
}else{
System.out.println("M");
}
}else{
System.out.println("N");
}

// End
}
}```

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
12

## Re: nested if/else; can't dertermine logical mistake

Originally Posted by Monte
=> is my program in this status(a=1, b=3, c=3, d=4) failing, because the first if (a < b) is true, and therefore it isnt necessary to execute the else part?
Bingo.

If so, i think i understand now, where my mistake is.
But at this point it would greatly benefit you to learn how to verify what you think is the truth, which is what Norm is trying to teach you. In this case a debugger can be a little confusing as it doesn't go into secret 'else' clauses that are not actually in the code because they would simply be empty. If you have trouble understanding what flow through the code the program is taking, add dumb System.out.println statements to see it happening.

Ex:

Java Code:
```if(a < b){
// bla bla bla
}```
This code is functionally the same as this:

[CODE]
Java Code:
```if(a < b){
// bla bla bla
} else {
// secret missing else which does nothing; because it does nothing, you don't add it to your code.
}```

You might temporarily alter this code to be like this:

Java Code:
```if(a < b){
System.out.println("IN A < B");
// bla bla bla
} else {
System.out.println("NOT A < B");
}```
See how I temporarily added the secret missing else to effectively print out that the code did not in fact go into the a < b branch, which would in other circumstances have resulted in nothing being printed. This takes away any guess-work, you KNOW what the code is doing. And you can then easily see if that matches with what you expect it to do. If not, you can see exactly where the code is taking a wrong turn.

That's the trick to debugging code: if with a quick guess you cannot figure out what is wrong, make any and all effort to learn what the truth is.

10. Member
Join Date
Jan 2015
Posts
6
Rep Power
0

## Re: nested if/else; can't dertermine logical mistake

seems to be a wise advice, thanks. I will try to consider that in my further coding sessions.

Again, thanks for the kind help from you guys, thats just lovely :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•