Thursday 18 October 2012

Difference between C++ and Java

This is the 2nd post in "Difference between C++ and Java" series.In this post I am going to discuss about the differences in C++ and Java in case of conditional statements.
Condition Statement in Loops
Statement
Conditional statement in Java have boolean value i.e. either true or false but same in case of C++ can have any numeric value(integer or  float)
There are 3 types of loops in both the languages namely 'while', 'do-while' and 'for'. The loops are very important in any language. The basic difference in the looping statements of these languages is the condition which is tested every time the loop is iterated. The conditional statement in Java can only boolean type that is it may have the value true or false. But in case of C++, the conditional statement can have any numerical value. Here, any non-zero numeric value refers to true and 0 value refers to false.
  • Java
int i=1;
while(i++<=5)
{
   System.out.println("hey");
}
  • C++
i=5;
while(i)
{
    cout<<"hey";
   i--;
}
Both the code snippets produce same output.We cannot write integer value instead of conditional statement in Java but in case of C++ we can do so.
Thanks all for your time. We will continue with 'Difference between C++ and Java' series in next post. Any queries and suggestions are invited in comments

No comments:

Post a Comment

Java vs C++

THis is a basic blog that differentiates features of two of the widely used languages C++ and Java.