In challenge 1 part 2(inverted hour glass), if inside for loop

I got the code but when I use my if conditions for Numbers Work 2 inside for loop for numbers (2) I get the wrong code but I don t get why that should happen as I am simply specifying condition for when to use condition 1 and when to use condition 2 in for loop.

When I write the same if condition before for loop it works perfectly fine .

I have commented my if condition before for loop and written it inside for loop so you get what I m asking…

Scanner scn = new Scanner(System.in);
int n = scn.nextInt();

	int nst = 1;
	int nsp = 2*n - 1;
	int row = 1;
	int rows = 2*n + 1; 

	while(row <= rows){

		int val = n;

		// Numbers Work
		for(int cst = 1; cst <= nst; cst++){
			System.out.print(val + " ");
			val--;
		}

		// Spaces Work
		for(int csp = 1; csp <= nsp; csp++){
			System.out.print("  ");
		}

// int cst = 1;
// if(row == n + 1) {
// val+=2;
// cst = 2;
// } else {
// val++;
// cst = 1;
// }

		// Numbers Work 2
		for(int cst = 1; cst <= nst; cst++) {
			if(row == n + 1) {
			val+=2;
			cst = 2;
			} else {
				val++;
				cst = 1;
			}
		System.out.print(val + " ");
		val++;
		}

		// criteria
		if(row <= n){
			nst++;
			nsp-=2;
		} else {
			nst--;
			nsp+=2;
		}
		
		row++;
		System.out.println();
	}

see for the case when row =2 after this loop, your val = 4 and cst =1
after printing 4 this fir loop will keep running indefinitely due to cst<=nst and cst=1 and nst=1 for all cases in this overall row=2 loop
so, you dont need another for loop ,this ll be covered in the while(row <= rows){ loop on its own

if this solves your doubt please mark it as resolved :slight_smile: