The if statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.
Below is the structure of if statement:
The above image illustrates the flow of application when it meets a if
- The program checks the condition.
- If the condition is true, code in the if block will be executed. It then continues executing the codes below the if block.
- If the condition is false, the program will ignores the code in the if block and execute code snippets below the if block.
Run the example:
The if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.
Running the example:
3- if - else if - else statement
The structure of an if - else if - else statement is:
Create ElseIfExample1 class:
Results of running the class ElseIfExample1:
Change the value of the variable "score" in the above example and rerun the ElseIfExample1 class:
boolean is a data type, it only has two values true or false.
Create class BooleanExample:
5- Operators involved in conditional expression
This is a list of operators, commonly used in conditional expressions.
- > Greater Than
- < Less Than
- >= Greater Than or Equal To
- <= Less Than or Equal To
- && AND
- || OR
- == HAS A VALUE OF
- != Not Equal To
- ! NOT
Chạy ví dụ:
You can change the value of "age" and rerun ElseIfExample2 class and see the results