Conditions in java. In English
CONDITIONS AND STATEMENTS
Logical conditions
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to: a == b
- Not Equal to: a != b
- a OR b: a||b
If statement
The simplest expression for making decisions is the if clause in Java. It determines whether a given statement or block of statements will be performed or not; that is, whether a block of statements is executed if a given condition is met or not.
Syntax: -
if(condition)
{
// Statements to execute if
// condition is true
}
Example: -
- public class Example {
- public static void main(String[] args) {
- int age=25;
- if(age>20){
- System.out.print("Age is greater than 20");
- }
- } }
Output - Age is greater than 20Else statementUse the else statement to specify a block of code to be executed if the condition is false.Syntax: -if (condition) {// code if condition is true} else {// code if condition is false}Example: -
- public class Example {
- public static void main(String[] args) {
- int number=16;
- if(number%2==0){
- System.out.println("Even number");
- }else{
- System.out.println("Odd number");
- }
- } }
Output - Even numberElse-if statementUse theelse if
statement to specify a new condition if the first condition isfalse
.Syntax: -if (condition1) { // block of code to be executed if condition1 is true} else if (condition2) {//code if condition1 is false and condition2 is true} else {// block of code to be executed if the condition1 is false and condition2 is false }Example: -
- int time = 23;
- if (time < 10) {
- System.out.println("Good morning.");
- } else if (time < 18) {
- System.out.println("Good day.");
- } else {
- System.out.println("Good evening.");
- }
Output -Good eveningTernary operatorThere are three components to the definition of ternary. There are three operands in the ternary operator (? :). Boolean expression evaluation is done with it. Which value is allocated to the variable is decided by the operator. Three operands can only be accepted by this one conditional operator. It can be applied in lieu of the if-else clause. It greatly simplifies, makes the code more understandable, and shortens it.Syntax: -variable = (condition) ? expression True : expression False;Example: -
- int time = 22;
- String result = (time<15)?"Good Evening!":"Good Night";
- System.out.println(result;)
Output -Good Nightswitch statementThe switch statement is a multi-way branch statement.To put it simply,the Java switch statement selects one sentenceto run based on certain factors.It provides an easy way to dispatch execution to differentparts of code based on the value of the expression.Basically, the expression can be a byte, short,char, or int primitive data types. It basically tests the equalityof variables against multiple values.A case can occur in any number of ways. simply enforcing thecondition check, but keep in mind that duplicate case/s valuesare prohibited.The data type of a case's value and the switch variable mustmatch.A case's value needs to be literal or constant. There is no roomfor variables.To end a series of statements, the break statement is used insidethe switch.The break statement is optional. If omitted, execution willcontinue on into the next case.The default statement may appear anywhere in the switch blockand is optional.Syntax: -switch(expression){case value1 ://bodybreak;case value2 ://bodybreak;default ://body}Example: -
- public class Example {
- public static void main(String[] args)
- {
- int day = 5;
- String dayString;
- switch (day)
- case 1:
- dayString = "Monday";
- break;
- case 2:
- dayString = "Tuesday";
- break;
- case 3:
- dayString = "Wednesday";
- break;
- case 4:
- dayString = "Thursday";
- break;
- case 5:
- dayString = "Friday";
- break;
- case 6:
- dayString = "Saturday";
- break;
- case 7:
- dayString = "Sunday";
- break;
- default:
- dayString = "Invalid day";
- }
- System.out.println(dayString);
- }
Output - Friday
Comments
Post a Comment