while loop in if statement in java - java

while loop is not working in the if statement, why its not working... and how can while loop can work in if curly braces.
public static void main(String[] args) {
int x = 30;
if (x < 20) {
System.out.print("This is if statement");
int a = 10;
while(a < 20) {
System.out.print("value of x : " + a );
a++;
System.out.print("\n");
}
} else {
System.out.print("This is else statement");
}
}

You declared x as 30, the if loop executes if(x<20) which is always false, so its performing the else statement immediately without going thru the if statement.
You either have to declare x for something less than 20 or change the if statement.

Your if statement doesn't meet the condition. Try this:
public static void main(String[] args) {
// TODO code application logic here
int x = 10;
if( x < 20 ) {
System.out.print("This is if statement");
int a = 10;
while( a < 20 ) {
System.out.print("value of x : " + a );
a++;
System.out.print("\n");
}
}else {
System.out.print("This is else statement");
}
}
And output:
This is if statementvalue of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

you assigned the value for x is 30 , but in your condition you are checking x is less than 20 , hence it the condition will fail and it wont execute...

Related

How do I display the first five numbers on a single line and the second five numbers on another line?

I am currently trying to create a do-while loop, counting from 1 to 10, that displays the first five numbers on the first line and the next five numbers on another line.
But whenever I run my code, the 6th iteration and onward print on seperate lines each instead of the same line.
If anyone could help me understand the error that I made and how to corret it, I would appreciate it.
import java.text.DecimalFormat;
import java.util.Scanner;
public class Hello_World { // Declare Class
public static void main(String[] args) { // Main Method
Scanner key = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("##.##"); // Decimal_Format!
/*
* While Loop count from 1 to 10
*/
int x = 1; // Declare and Initialize variable 'X'
do
{
System.out.print(x + " ");
x++;
if (x > 5) {
System.out.println("");
continue;
}
}
while (x <= 10);
}
} // Braces delimit blocks of code!
You only want to print the newline when x is 6. Also, you could use printf to make this a bit cleaner. Something like,
int x = 1; // Declare and Initialize variable 'X'
do {
if (x == 6) {
System.out.println();
}
System.out.printf("%3d ", x);
x++;
} while (x <= 10);
Outputs
1 2 3 4 5
6 7 8 9 10
The error in your code is with your if-statement
By setting the if statement to be true if x > 5 it will trigger for 6 through 10 which is why you get your numbers above 5 all on a separate line. If you just set the new line to kick in when x == 6 it will only trigger a new line when x is 6 and in no other circumstance.
I prefer a two-loop solution. The outer loop increments through the entire range you want; the inner loop increments through a single line.
int limit = 10;
int perLine = 5;
for (int x = 1; x <= limit; x += perLine) {
for (int y = x; y < x + perLine && y <= limit; y++) {
System.out.printf("%3d", y);
}
System.out.println();
}
This solution works even if the 'limit' is not an exact multiple of 'perLine'.
I wrote this with a for-loop out of sheer habit, but you could convert it to do-while easily enough.
int limit = 10;
int perLine = 5;
int x = 1;
do {
int y = x;
do {
System.out.printf("%3d", y);
y++;
} while (y < x + perLine && y <= limit);
System.out.println();
x += perLine;
} while (x <= limit);
However, it's a lot more long-winded that way, so unless this is being done as an exercise in using do-while, I'd use a for-loop.

How does this program work. Im rookie in programming so please explain it in simplified way

please explain to me how program output
a noise
annoy
an oyester
while ( x < 4 ) {
System.out.print("a");
if ( x < 1 ) {
System.out.print(" ");
}
System.out.print("n");
if (x> 1 ) {
System.out.print(" oyster");
x = x + 2;
}
if ( x == 1 ) {
System.out.print("noys");
}
if ( x < 1 ) {
System.out.print("oise");
}
System.out.println("");
x = x + 1;
So, the first line is a while-loop it keeps evaluating the statements in between the {} brackets if the condition x<4 is fulfilled.
while ( x < 4 ) {
Let's assume x = 0
x<4 is a true statement so we enter the brackets:
System.out.print("a"); will print 'a'
the next line if ( x < 1 ) { is an if statement, it will evaluate the code in the brackets once if the condition (x < 1) is fulfilled. Because x is 0 at this point the condition is true.
`System.out.print(" "); this will print a space
System.out.print("n"); this line in not in the brackets of the if-statement so it will also print.
if (x> 1 ) { x is 0 at this point so the condition is false and the program will skip everything in between these brackets
if ( x == 1 ) { this condition is also false because x is 0 so the program will skip everything in between these brackets
if ( x < 1 ) { this condition is true and `System.out.print("oise")`` prints "oise"
System.out.println(""); starts a new line
x = x + 1; the x variable is incremented by one: new x = 0 + 1 = 1
So the first iteration of the while statement yields:
a noise
For the second iteration of the while loop:
x is 1 and we follow the logic sequence again:
while ( x < 4 ) { this is true so we enter the brackets
System.out.print("a"); a is printed
if ( x < 1 ) { this statement is false, so next line is skipped
System.out.print("n"); n is printed
if ( x < 1 ) { this statement is false, so next line is skipped
if (x> 1 ) { this statement is false, so next two lines are skipped
if ( x == 1 ) { this is true
System.out.print("noys"); noys is printed
the next if-statement is also false
System.out.println(""); a new line is started
x = x + 1; and x is incremented
So the second iteration of the while-statement yields:
annoy
I think it is a good exercise now for you to try and figure out what happens in the next iteration of the while-loop.

Adding repeating integers in a "for" loop a specified number of times

I am looking for a way to run a "for" loop that will start with the user defined value "d" (between 1 and 7) and add 1 continuously until it reaches the other user defined value "n". Here's the catch...I need it to repeat the count back to "1" once the value "7" is reached without it being stuck in an infinite loop.
For example, my program prompts a user to input 2 numbers and it stores them as "d" and "n" respectively. The first number can be "1-7" and the second number can be anything. So, if the user inputs "5" and "10" I need my loop to start counting at "6" and count up "10" times, starting back over at "1" once the value has reached "7". It should look like this...
"6 7 1 2 3 4 5 6 7 1".
Right now I have it looking like this...
"6 7 8 9 10 11 12 13 14 15"
This is my current code and output with "5 = d" and "10 = n"
Code
public void incrementDay3()
{
int i;
for(i = (d + 1);i <= (d + n);i++)
{
System.out.print(i);
}
}
Output
"6789101112131415"
Any help is greatly appreciated!
Try this (modulo operator) :
public void incrementDay3()
{
int i;
for(i = d ; i < (d + n) ; i++)
{
System.out.print((i % 7) + 1);
}
}
For more information about what a modulo is, you can check this wikipedia article
public void incrementDay3()
{
int i;
int d = 5, n = 10;
int result;
result = d + 1;
for(i = result;i <= (d + n);i++)
{
result++;
if(result > 7)
result = 1;
System.out.print(result);
}
}
I find this variant easier to read. In particular, the "simple for loop that just counts to n" is instantly recognizable, and easier to parse than the loop condition in the current best answer.
public static void incrementMod7(int d, int n) {
for (int i=0; i<n; i++) {
d = (d%7) + 1;
System.err.print(d + " ");
}
}
int d=5;
for (int i = 0; i < 10; i++) {
d =(d==7)?1:d+1;
System.out.println(d);
}
easy.

Java application integer

I'm trying to create my fist java application with an if statement that will take an integer (e.x 22) and find out if its sum is equal when multiplied and subtract (e.x 2*2=4 and 2+2=4) or if its not.
Though i can't figure out how to do the if decision. can someone point out how to do that?
thank u
package 1;
import java.util.Scanner;
public class 1
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
int x;
int y;
System.out.print( "Enter a number from 10 to 99: " );
x = input.nextInt();
if ( x >= 10 && x <= 99 )
{
x= x % 10;
x= x / 10;
}
else
{
System.out.println( "you must enter a number from 10 to 99" );
}
}
}
You just need to assign them to different variable and check for the condition
if (x >= 10 && x <= 99) {
int first = x / 10; // Take out the first digit and assign it to a variable first
int second = x % 10; // Take out the second digit and assign it to a variable second
if (first * second == first + second) { // Check for your condition, which you mentioned in your question
System.out.println("Yep, they match the condition"); // If it satisfies the condition
} else {
System.out.println("Nope, they don't match the condition"); // If it doesn't satisfy the condition
}
}
P.S: You question said multiplied and subtract but the example just after the example was (e.x 2 x 2=4 and 2+2=4). I went with the ex.
try
import java.util.Scanner;
public class One {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x;
int y;
System.out.print("Enter a number from 10 to 99: ");
x = input.nextInt();
if (x >= 10 && x <= 99) {
y = x % 10;
x = x/10 ;
if(x* y== x+ y)){
System.out.println("Sum and product are equal" );
}
else
System.out.println("Sum and product are not equal" );
} else {
System.out.println("you must enter a number from 10 to 99");
}
input.close();
}
}

Understanding a for loop and if condition

Below is come code that I am having trouble understanding. The output is 13 15 x=6. I understand how we get the number 13 because when we go through the loop the value of x is 5 and its corresponding y value is 12. So if x > 4 we then increment y which gives u the value of 13. The next number printed out then should be 14 because the next value of x is 6 and its corresponding y value is 13 so when you increment that it will be 14. However when I run the code its gives a different answer. Can anyone please help? Thanks
public class Output {
public static void main(String[] args) {
Output o = new Output();
o.go();
}
void go() {
int y = 7;
for (int x = 1; x < 8; x++) {
y++;
if (x > 4) {
System.out.print(++y + " ");
}
if (y > 14) {
System.out.println(" x = " + x);
break;
}
}
}
} ​
The reason is because ++y increments y before printing it.
y++ would do what you are expecting
When entering the loop iteration where x is 6, y is 13, as you said. Then y is immediately incremented to 14. x is greater than 4, so System.out.print(++y + " "); is executed. ++y increments y to 15 before it is printed.

Categories

Resources