Conditions and Loop

 Conditions.....


Java has 3 types of conditional statements.
  1. If
  2. else
  3. else if
If a specified condition is true, we use "if" to execute a code block  under it.
If the same condition is false, we use "else" to specify a block of code to be executed.
And then we can use "else if" to specify a new condition to test if the 1st condition is false.

The above conditions return only boolean values.(true/false)

eg :- class A{
            if(true){
               sout ("A"); }
            else {
               sout ("B"); }
         }

eg :- class A{
            if(false){
               sout ("A"); }
            else if{
               sout ("B"); }
            else {
              sout ("C"); }
         }

Loops.....



There are mainly 2 types of loops in Java.
  1. While
  2. For
They also return only boolean values.

1) For loop

syntax -    for (statement 1 ; statement 2 ; statement 3) {
                 //code to be executed
                 }

eg :-    print numbers 1 to 5
           
           for (int x=1 ; x<=5 ; x++) {
                 sout ( x );
                 }

2) While loop

we can use only one condition inside the parameter list of while.

syntax - while ( condition ) {
              //code block to be executed
              }

eg :-  while (true) {
             sout (A") ;
         }

Incremental/Decremental Operators

  1. x++ (post increment)
  2. x--   (post decrement)
  3. ++x (pre increment)
  4. --x   (pre decrement)
refer this link for more about incremental and decremental operators




Comments

Popular posts from this blog

Variable and Method types in JAVA

Polymorphism

Casting