isless() in C/C++


In C++, isless() is a predefined function used for mathematical calculations. math.h is the header file required for various mathematical functions.

isless() function used to check whether the 1st argument given to the function is less than the 2nd argument given to the function or not. Means if a is the 1st argument and b is the 2nd argument then it check whether a<b or not.

Syntax:



bool isless(a, b)

    Parameters:

  • a, b => These two are the parameters whose value we want to compare.
  • Result:

  • The function will return the true if a<b else it returns false.
  • Error:

  • No error occurs with this function.
  • Exception:

  • If a or b or both is NaN, then the function raised an exception and return false(0).
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to illustrate
// the exception of function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Take any values
    int a = 5;
    double b = nan("1");
    bool result;
  
    // Since b value is NaN so
    // with any value of a, the function
    // always return false(0)
    result = isless(a, b);
    cout << a << " isless than " << b
         << ": " << result;
  
    return 0;
}

chevron_right


Output:

5 isless than nan: 0

Example:

  • Program:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // CPP code to illustrate
    // the use of isless function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
        // Take two any values
        int a, b;
        bool result;
        a = 5;
        b = 8;
      
        // Since 'a' is not greater
        // than 'b' so answer
        // is true(1)
        result = isless(a, b);
        cout << a << " isless than " << b
             << ": " << result << endl;
      
        char x = 'a';
      
        // Since 'a' ascii value is less
        // than b vaiable so answer
        // is true(1)
        result = isless(x, b);
        cout << x << " isless than " << b
             << ": " << result;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    5 isless than 8: 1
    a isless than 8: 0
    

Note: Using this function you can compare any data type with any other data type as well.

Application
There are variety of applications where we can use isless() function to compare two values like to use in while loop to print the first 9 natural number.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP code to illustrate
// the use of isless function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int i = 1;
    while (isless(i, 10)) {
        cout << i << " ";
        i++;
    }
    return 0;
}

chevron_right


Output:

1 2 3 4 5 6 7 8 9 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.