Conditions in Java - ProgrammerTech
Language:

Search

Conditions in Java

  • Share this:
Conditions in Java

Conditions in Java

Conditions are used to execute parts of the program only when they meet the requirements.

 

The if conditional statement in Java

The if conditional statement is used to check one or more values, and based on the result of the check, we can take the appropriate decision and direct the program's path.

The general form of an if statement in Java

if (condition)

Conditional operands in the if statement in Java

There are several conditional operators used with if:

  1. The parameter > is less than .
  2. The parameter < is greater than .
  3. The parameter == equals.
  4. Labs! not.
  5. The && parameter means and is used to compare two values ​​and returns True if the condition is met in the two values ​​and returns False if the condition is not met in one of the two values.
  6. parameter || The or means is used to compare two values ​​and returns True if one of the two conditions is met and returns False if the two conditions are not met.
  7. The IF - ELSE parameter is used if the condition is not fulfilled, ELSE is executed if the condition is not fulfilled, and if the condition is not met, it is not executed.

For example, if we want to write a program that asks the user to enter an even number, if the user enters an uneven value, a message will appear stating that the entered value is not acceptable.

if (num%2!=0)
System.out.println("The value entered is not acceptable");

Example:

if(deg>=50)
System.out.println("successful");
else
System.out.println("fail");

else and if are used if one of the two outcomes is expected according to the given condition. If the condition is met, else is ignored. If the condition is not met, if is ignored and else is executed. For example, if we want to check the student’s result, if it is greater than or equal to 50, a pass will be printed, but if his score is less than 50, a failure will be printed.

Example:

Suppose we have a variable containing the value 23, check it through the IF function. If it is checked, print ok. If the condition is not met, print no according to the required condition.

package gradleproject223;
public class JavaApplication {
    public static void main(String[] args) {
        int a=23;
        if (a<23)
            System.out.println("ok");
        else
            Syste.out.println("no");
    }
}

In this example, we put a scalar variable a=23 and told him if a is less than 23, print "ok" and if not, print "no" and when the program is executed, it will print "no" and ignore if because a=23 and not less than it.

Example:

Write a simple program that asks the user for the username and password and compares the entered data with the data in the stored databases, and in the event of a match, the Succes sentence will be printed, and if it does not match, an Error will be printed.

package lesson_if;
import java.util.Scanner;
public class Lesson_IF {
    public static void main(String[] args) {
        Scanner n=new Scanner(System.in);
        String user="Programmer";
        String Password="Tech4567";
        String Enuser,EnPassword;
        System.out.println("Enter User Name");
        Enuser=n.nextLine();
        System.out.println("Enter Password");
        EnPassword=n.nextLine();
        if(user.equals(Enuser) && Password.equals(EnPassword))
            System.out.println("Succes");
        else
            System.out.println("Error");
    }
}

 

if with block in java

In this paragraph, we will learn about a very important thing, which is if with block, and the meaning of the word block is a set of commands.

In this paragraph, we will learn about a very important thing, which is if with block, and the meaning of the word block is a set of commands. We learned a simple example of how to create a conditional if statement with a single command, meaning that if the condition is met, the command is executed. This means that it only takes one step, but the block is doing or executing. A set of steps or a set of commands, if the condition is met. For example let's say we have a simple example of asking the user to enter a value from the keyboard. If the number is entered, two commands will be executed, and the block is two commands and above. This value is equal to the number 1. We will first add the two numbers, which is the first command, and the second command is to print the result on the screen.

Example:

package lesson_if;
import java.util.Scanner;
public class Lesson_IF {
    public static void main(String[] args) {
        Scanner n=new Scanner(System.in) ;
        int a=10,b=12;
        int c=n.nextInt();
        if(c==1)
         {
             int s=a+b;
              System.out.println("The Result Add is" + "" + s);
         }
         else if(c==2)
         {
             int s=a-b;
             System.out.println("The Result Add is" + "" + s);
         }
         else
             System.out.println("The Enter is Error");
    }
}

We created two variables of type int which are int a=10 and int b=12 and we created a variable c to receive the inputs. From the user's keyboard and store the output in it, then we set a condition if c==1 Execute the following commands for us:

  • The first command: collect the value of the variable a and the variable b and store it for us in the variable s, and here the process is a process of all. Then print the result with the message The Result Add is, and the result is written after that, meaning if user enters No. 1, the result will be collected for us on the screen. Then we set another condition, which is that if c==2 we do.
  • The second command: subtract the value of a from b and store it in the variable s.
  • The third command: If the user enters a number other than 1 and 2, the message will be printed.

When running the previous program, if the user enters the number 1, our first block will be executed. From the if conditional which is the plural. And it will print for us the number 22, which means adding the value of a to the value of b. And if the user writes the number 2, the second block, which is the subtraction, will be executed. It will print the number negative -2, and if we write any number other than the previous two numbers, the third block will be executed, telling us an error message.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com