Hello everyone, this is the second post on Java programming language. Today we will read about operators.
if you haven't seen the last post on Java programming, then go and see it first.
what are operators?
operators are symbols that are used to perform operations on variables and manipulate the values of the operands. Each operator performs specific operations. Let us consider an expression 5 + 1 = 6; here, 5 and 1 are operands, and the symbol + (plus) is called the operator. We will also learn about operator precedence and operator associativity.Here we will talk about Arithmetic operators, Relational operators, Unary operators, etc.
- Arithmetic Operators:- There are five operators: ‘+’, ‘ – ‘, ‘ * ‘, ‘ / ‘, and ‘ % '.
- ‘ + ‘ used for addition and concatenation (adding two strings).
- ‘ – ‘is used for subtracting two values.
- ‘ * ‘ is used for multiplication.
- ‘ / ‘is used for division.
- ‘ % ‘Is used for finding the remainder.
I used float data type so that on dividing output should be
in decimal value.
If I have used int then on division the answer will be 2.
- Relational Operators:-
- Comparison Operators:
- ‘a<b' means a is less than b.
- ‘a>b’ means a is greater than b.
- ‘a<=b’ means a is less than or equal to b.
- ‘a>=b’ means a is greater than or equal to b.
- Equality Operators :
- ‘==’ it is used to check equal values.
- ‘!=’ it is used to check whether two values are unequal.
- Unary Operator:-
incrementing/decrementing a value by one
inverting the value of a boolean
If value of a = 10;
Then
System.out.println(a++); //It will print the value of an i.e., 10
and then it will increase to 11.
System.out.println(++a);
//It will first increase the value and then print it increased value.
Similarly ‘—a’ and ‘a—’ are used for decreasing the value.
If c = true; // a
Boolean variable
Then
System.out.pritnln(!c); //it will print opposite of c i.e., false
- Logical Operator:-
These operators are used to check two or more conditions
Use of &&: If all conditions are true simultaneously then only it will show true otherwise false.
example:-
Output: false
As both conditions are false here so it will print false
- Assignment Operator:
The Java compiler runs the code from top to bottom. The statements of the code are executed in the order they appear. Statements in Java, however, can be used to control how Java code is executed. Control flow statements are the phrases that fit this description. It guarantees a smooth flow of programs, which is one of Java's fundamental features.
Java provides three types of control flow statements.
- Decision-Making statements
- if statements
- switch statement
- Loop statements
- do while loop
- while loop
- for loop
- for-each loop
- Jump statements
- break statement.
- continue statement
In Java, a condition is evaluated using the "if" expression. Depending on the circumstances, the software's control is changed. The If statement's condition produces a Boolean value, either true or false. In Java, there are four types of if-statements given below.
- Simple if statement
- if-else statement
- if-else-if ladder
- Nested if-statement
Output: x+ y is greater than 20
Output:
x + y is greater than 20
Output: Delhi
Nested if-statement
Output: Delhi
No comments:
Post a Comment