-1

I am trying to print a pattern through java. I am using for loop and a conditional statement but there is some problem with && operator. I am not getting why it is behaving strange with my code specially. Following is my code.

public class Test{
public static void main(String ar[]){
    for(int i=0;i<5;i++){
        for(int j=0;j<5;j++){
            if((i!=0 && j!=0)){
                System.out.print("*");
            }else{
                System.out.print(" ");
            }
        }
        System.out.println();
    }
}
}

output

unknown@unknown-Lenovo-E40-80:~/Desktop/java_test$ javac Test.java
unknown@unknown-Lenovo-E40-80:~/Desktop/java_test$ java Test

 ****
 ****
 ****
 ****

above output is totally different to my expectation. line 1 is not printed why? i just wanted to skip (i=0 and j=0) location's element.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Ashwini Pandey
  • 115
  • 2
  • 2
  • 9
  • 6
    `line 1 is not printed` What do you mean? Line 1 is full of spaces. – tkausl Oct 16 '18 at 13:25
  • 1
    What exactly is the operation you want && to do? Do you know the difference between & and &&? Take a look at this thread: https://stackoverflow.com/questions/5564410/difference-between-and – Sarriman Oct 16 '18 at 13:29
  • 3
    Because you could easily come up with the solution, if you would swap the space for another char. – mwe Oct 16 '18 at 13:29
  • 1
    What pattern do you expect? – Khalid Shah Oct 16 '18 at 14:12

5 Answers5

3

Let's see what happens:

For line 1 you get i = 0 and j = 0 to 4, thus you'll get 5 spaces (because i = 0 will always end up in the result being false). For all other lines i is != 0 and j is still 0 to 4 so you get 1 space and 4 stars.

If you change the space you print to a underscore you'd get the following output:

_____     
_****
_****
_****
_****

i just wanted to skip (i=0 and j=0) location's element

Currently you are skipping all elements that don't have any of their indices being 0 (i != 0 && j != 0 translates to "i is not 0 and j is not 0").

If you only want to skip that one element you need to either change the expression to if( i == 0 && j == 0) { skip } or if( !(i == 0 && j == 0) ) { do_whatever_you_need } where the second translates to "if not both i and j are 0" (note that this means that either one can be 0, only the combination of both satisfies the condition).

Thomas
  • 87,414
  • 12
  • 119
  • 157
2

Line 1 is getting printed, but it is printed with spaces.

The reason is because you are checking (i != 0 && j != 0). In the first iteration of the outer loop i is 0, hence the control goes to the else block.

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
1

Boolean algebra says that !a && !b == !(a || b). You are trying to say that !a && !b == !(a && b).

Your test of

if((i!=0 && j!=0))

is equivalent to

if(!(i==0 || j == 0))

This is not what you want, according to your final statement: "I just wanted to skip (i=0 and j=0) location's element."

And so, you can alter your test in a few ways. The first is probably easiest - reverse the blocks. This has the fewest operations and is easiest to understand.

if (i==0 && j==0)
    System.out.print(" ");
else
    System.out.print("*");

Or, if you want to maintain the order, alter your if statement. To invert your requirement of (i==0 && j==0): !(a && b) == (!a || !b)

if( (i!=0) || (j!=0) )
    System.out.print("*");
else
    System.out.print(" ");
Jamie
  • 1,888
  • 1
  • 18
  • 21
0

The first line prints whitespaces, as I added in the comment, because, at this point i == 0 and prints it 4 times, but the second, third, and others attempts will print at first because will be equals to zero j == 0 after that four *

for(int i=0;i<5;i++){
    for(int j=0;j<5;j++){
        if((i!=0 && j!=0)){
            System.out.print("*");
        } else {
            System.out.print(" "); //THIS IS PRINTING THE FIRST LINE
        }
    }
    System.out.println();
}
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
0

In first line i=0 so in first iteration of i control goes to else part and in first row spaces are printed instead of asterisks. And on every iteration of i first index j=0. So again else part executes and in first column again spaces are printed instead of asterisks. And on all other places asterisks are printed.

public class Test{

    public static void main(String ar[]){
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                if((i!=0 && j!=0)){
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

}
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39