Generally in c/c++/java you all have used mainly int,float,char and double data type. Boolean is different from all of them.It is used to check that the required result is ‘true’ or ‘false’.It contains only two values, first one is ‘true’ and remaining one is ‘false’.Relational operators are used for checking condition inside the if statement or even inside System.out.println statement.





Confused? don’t worry you will get more idea after executing below program ,But first let me describe how to use boolean type of variable.


Below is syntax with example


bool var_name;               //declaring variable
var_name =true;   // initializing ‘true’ value into variable
 var-name= false;   //initialize variable again with ‘false’ value





example based on above statement


bool b;       //b declared
b=true;    //b initialized
b=false;  //b initialized again



Boolean data type with if statement


As you all know that we use boolean type for getting required result from condition.There is two possible result .First one is for true and other one is for false.
1. With true value.


syntax


bool b;b=true;if(b){statement ;}

Because b is true it goes inside the if statement and gives required result


2. With false value


syntax


bool b;b=false;if(b){statement1;}statement2;



Because b is false it does not go inside if to execute listed statements,it directly goes on statement2.
Let see the example of boolean type.
problem–>>

Execute a java program for performing some conceptual operation with boolean datatype.



code–>>


class bool{public static void main(String args[]){
boolean a;
a=true;
System.out.println(“a is “+a);
a=false;System.out.println(“a  become “+a)
;a=true;
if(a)
{
System.out.println(“This statement should be executed”);
}
a=false;
if(a)
{
System.out.println(“This statement should not be executed”);
}
System.out.println(“5>9= ” +(5>9));
System.out.println(“15>9= ” +(15>9));
}
}



output–>>



    a is true
     a become false
    This statement should be executed
     5>9= false
     15>9=true
     



Logic–>>


First we have declared class named bool.Now we have declared variable a havig boolean data type.The value of a become true after its first initialization.Now we have simply printed that statement about value a.Now we value of a become false after its second initialization and performed same operation as above.Then after third initialization value of a become true,so it goes inside the if statement and prints the required result.Now a become false so it will not perform the next operation and jump to directly the next statement outside the curly braces.That statement directly checks the data (5>9) and give us the result that it is true or not,as you all know it becomes false.Then the last statement prints 15>9= true
Contact us for any difficulty

Don’t forget to like us on fb from facebook fan page.Happy coding!

By jigar

Leave a Reply

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