Open In App

–> (Goes to) in C/ C++

Last Updated : 28 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

–> is called as “Goes to” in C/ C++ and compiles fine in every compiler including GCC and MSVN. This operator (–>) is not described in any C/ C++ standards as it is not an actual operator rather than a mix of two operators (–) and (>).

Program 1:

Below is the program to illustrate the goes to ‘–>’ operator:

C




// C program to illustrate the use of
// goes to (-->) operator
#include <stdio.h>
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (x-- > 0) {
        printf("%d ", x);
    }
  
    // Print value of x after
    // loop exists
    printf("\n%d ", x);
}


C++




// C++ program to illustrate the use
// of goes to (-->) operator
#include <stdio.h>
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (x-- > 0) {
        cout << ' ' << x;
    }
  
    // Print value of x after
    // loop exists
    cout << "\n"
         << x;
}


Output:

9 8 7 6 5 4 3 2 1 0 
-1

Explanation: As compilers try to parse expressions to the biggest token by using the left-right rule.  So, here the tokens are:

  • Token 1: x
  • Token 2:
  • Token 3: >
  • Token 4: 0

And the code compiles as:

((x–) > 0)

Program 2:

Below is the program to mix other conditional operators with postfix and prefix increment or decrement like (>–):

C




// C program to illustrate the use
// of goes to (-->) operator
#include <stdio.h>
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (0 < --x) {
        printf("%d ", x);
    }
  
    // Print value of x after
    // loop exists
    printf("\n%d ", x);
}


C++




// C++ program to illustrate the use
// of goes to (-->) operator
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (0 < --x) {
        cout << ' ' << x;
    }
  
    // Print value of x after
    // loop exists
    cout << '\n'
         << x;
}


Output:

9 8 7 6 5 4 3 2 1 
0

Below is the variation of the postfix and prefix increment/decrement that can be used with this operator:

  Postfix Prefix
Decrement –> >–
–>= >=–
Increment ++> >++
++>= >=++

Program 3: Below is the program to illustrate the use of the (–) operator:

C




// C program to illustrate the use
// of goes to (--) operator
#include <stdio.h>
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    while (x--) {
        printf("%d ", x);
    }
  
    // Print value of x after
    // loop exists
    printf("\n%d ", x);
}


C++




// C++ program to illustrate the use
// of goes to (--) operator
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 10;
  
    // x goes to 0
    while (x--) {
        cout << ' ' << x;
    }
  
    // Print value of x after
    // loop exists
    cout << ' ' << x;
}


Output:

9 8 7 6 5 4 3 2 1 0 
-1

Program 4:

The value of increment and decrement in prefix operations in C++ can be controlled as shown in the below program:

C++




// C++ program to illustrate the use
// of (----------) operator
#include <iostream>
using namespace std;
  
// Driver Code
int main()
{
    // Initialize a variable x
    int x = 100;
  
    while (0 < --------------------x) {
        cout << x << " ";
    }
  
    // Print the value of x
    // after the loop exists
    cout << endl
         << x;
  
    return 0;
}


Output:

90 80 70 60 50 40 30 20 10 
0


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments