How java is important programming language in cyber security part-2

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.

operator.png

Output:-

output.png

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:-
The Java unary operators require only one operand. Unary operators are used to performing various operations i.e.:

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:-
We have two types of logical operators && and ||
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:-

&&operator.png

Output:- True

&&operator.png

Output:- False

It will show false because only one condition is true.

Use of ||: If all conditions are false simultaneously then only it will show false otherwise true.

That means even if only 1 condition is true in all then also it will return true.

| | operator.png

Output: True

As one of the two conditions are true so it will show true

| | operator.png

Output: false 

As both conditions are false here so it will print false

  • Assignment Operator:

It is employed to translate the operand on its left into the value on its right.

assignment operator.png


Java Control Statements

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
    1. if statements
    2. switch statement
  • Loop statements
    1. do while loop
    2. while loop
    3. for loop
    4. for-each loop
  • Jump statements
    1. break statement.
    2. continue statement

Decision-Making statements

Decision-making statements, as the name implies, choose which statement to execute and when. Depending on the outcome of the condition given, decision-making statements analyze the Boolean expression and regulate the program flow. In Java, there are two different categories of decision-making statements: If statements and switch statements.

1) If 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

Let's understand the if-statements one by one.

Simple if statement:
Among all Java's control flow statements, it is the simplest. It tests a Boolean statement and, if it returns true, allows the program to enter a block of code.
simple-if-statement.png

Output: x+ y is greater than 20

if-else statement

Among all Java's control flow statements, it is the simplest. It tests a Boolean statement and, if it returns true, allows the program to enter a block of code.

if-else_statement.png

Output:

x + y is greater than 20


if-else-if ladder:


A series of else-if statements are placed after an if statement in an if-else-if statement. In other words, the series of if-else statements build a decision tree that allows the program to execute the block of code when the condition is true. At the chain's conclusion, we can define an else statement.

if-else-if ladder.png

Output: Delhi

Nested if-statement

The if statement can have an if or an if-else statement inside of another if or else-if statement in nested if statements.

nested_if-else.png

Output: Delhi




so, that's it for today, follow us to learn more about Java.

thank's for learning.



No comments:

Post a Comment