What the exclamation before a comparison does is negate it, for example:
if (1==1) {
// DO something
}
if (!(1==1)) {
// DO something
}
In the first case it would enter the IF condition in the second case it would never enter
edited
The problem you have is in the condition is bad you complicate yourself a lot
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num;
do{
System.out.println("Introduce un numero comprendido entre 1 y 10");
num = sc.nextInt();
} while (num < 1 && num > 10);
for(int i = 1; i <= 10; i++){
System.out.println("La tabla de " + num + " es: " + num * i);
}
}
}
The while()would execute normally if it numis greater than or equal to 1 and less than or equal to 10.
By putting the symbol "!" you flip the condition. That is to say: that while()it would be executed while num is NOT between 1 and 10 (both included).
A simple example would be this:
bool flag=true;
while(!flag){
// hago algo
}
In this case the while will only be executed while flag is different from true.
EDIT:
I will explain your code.
Without him "!" : I ask you for a number. If it is less than 10, I will ask you for a number again because my condition is that you continue to ask for numbers until it is NOT 1-10.
With the "!" : You compare the opposite of what you want. That is to say that if you normally want a 1-10 to continue with the loop. By requesting the opposite with a 1-10 you exit the loop
What the exclamation before a comparison does is negate it, for example:
In the first case it would enter the IF condition in the second case it would never enter
edited
The problem you have is in the condition is bad you complicate yourself a lot
The
while()
would execute normally if itnum
is greater than or equal to 1 and less than or equal to 10.By putting the symbol "!" you flip the condition. That is to say: that
while()
it would be executed while num is NOT between 1 and 10 (both included).A simple example would be this:
In this case the while will only be executed while flag is different from true.
EDIT:
I will explain your code.
Without him "!" : I ask you for a number. If it is less than 10, I will ask you for a number again because my condition is that you continue to ask for numbers until it is NOT 1-10.
With the "!" : You compare the opposite of what you want. That is to say that if you normally want a 1-10 to continue with the loop. By requesting the opposite with a 1-10 you exit the loop
The exclamation '!' negates a boolean statement. For example, if I have a variable
If you put an exclamation mark on it, it will become false