Java Conditions and If statements

 

 

With  the  help  of  Conditions  and  If  statements , you  can  control  the flow  of your  java  program  by  deciding  which code  to  run  and  which  code  to  skipped.

 

Real time  example:  If  score is more than 80,eligible for gift. Otherwise,not eligible.

 

 

if   statements  work  with  boolean  values. Every  if  statement  needs  a  condition  that  results   in   true  or  false.

 

 

 

 

Example

 

boolean   isEligible=true;

if(isEligible){

System.out.println(“Score is  more  than  80”);

}

 

 

 

 

Mostly, conditions  are   created  using  comparison  operators, as given  below:

 

  • Greater than: a>b
  • Greater than or  equal to: a>=b
  • Less than:  a<b
  • Less than  or  equal to: a<=b
  • Equal to: a==b
  • Not equal  to:  a!=b

 

One  can use  these  conditions  to  perform  different  actions  as  per  requirement.

 

 

 

 

 

 

 

Java  has  the following  conditional  statements:

 

  • Conditional  statement  if   is  used  to  specify  a   block  of  code  to  be  executed, if the  specified  condition  is   true.
  • Conditional  statement  else  is  used  to  specify  a  block  of  code  to  be  executed, if  the  same  condition  is  false.
  • Conditional  statement  else  if  is used  to  specify  a  new  condition  to  test, if  the  first  condition  is false.
  • Conditional  statement  switch  is  used  to  specify  many  alternative  blocks   of  code   to  be  executed.

 

 

 

The  if   Statement

 

In  java, use  if  statement  to  specify  a  block  of  code  to  be  executed  if  a  condition  is  true.

 

 

 

Syntax

 

if (condition){

//block   of  code  to  be  executed  if  the  condition  is  true

}

 

 

 

NOTE:

 

  • if  is  in  lowercase  letters, uppercase  letters (If or IF)  will  generate  an  error.
  • if  statement  must  result  in  boolean  value. 

 

 

 

 

In this  example  we  will  test  two  values  to  find out  if  5  is  smaller  than  7. If  the  condition  is  true, print  some  text.

 

 

 

Example

 

if (5<7) {

System.out.println(“5 is  smaller  than  7”);

}

 

 

 

 

Variables  can  also  be  compared  instead  of  hard-coded  values:

 

Example

 

int  x = 5;

int  y = 7;

if(x < y){

 

System.out.println(” x  is  less  than y”);

 

}

 

 

 

 

 

 

 

Comparision  is  also  often  used  to  check  if  two  values  are  equal, using  the  ==  operator.

 

 

 

Example

 

int  x  =  33;

int  y  = 33;

if (x == y) {

System.out.println(” x  is  equal  to  y”);

}

 

 

 

You  can  also  test  boolean  variables  directly  in  an  if  statement.

 

 

 

 

 

Example

 

boolean  isEligible  =  true;

if( isEligible )  {

System.out.println(“The person is  eligible  for  gift”);

}

 

 

 

 

 

NOTE:

 

  • If an  if    statement  has  only  one  line  of  code, you  can write  it  without  curly  braces  {   }

          Example:

 

           if  (  7 > 5 ) 

                         System.out.println(” Seven is  greater  than  five “)

 

 

 

 

 

  •  If   we   write  any  if  statement  without  any  braces  and  if the  condition is  false  then only first  line  will not  execute , but   any other  lines  after  it will run  which  can  lead  to  unexpected  result. 
  • To  avoid  mistakes, always  use  curly  braces {  }.  This  makes  clear  which  lines   belong to  the  if  statement.

 

 

 

       The  else  Statement

       

       The  else  statement   run  a   block  of  code  when  the  condition  in  the  if  statement is  false.

 

 

 

 

      Syntax

 

          if  ( condition )  {

               // block  of  code  to  be  executed  if  the   condition  is  true

              }  else  {

        //  block  of  code  to  be  executed  if the   condition  is  false

               }

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *