–> (Goes to) in C/ C++
Last Updated :
28 Dec, 2020
–> 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
#include <stdio.h>
int main()
{
int x = 10;
while (x-- > 0) {
printf ( "%d " , x);
}
printf ( "\n%d " , x);
}
|
C++
#include <stdio.h>
int main()
{
int x = 10;
while (x-- > 0) {
cout << ' ' << x;
}
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
#include <stdio.h>
int main()
{
int x = 10;
while (0 < --x) {
printf ( "%d " , x);
}
printf ( "\n%d " , x);
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int x = 10;
while (0 < --x) {
cout << ' ' << x;
}
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
#include <stdio.h>
int main()
{
int x = 10;
while (x--) {
printf ( "%d " , x);
}
printf ( "\n%d " , x);
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int x = 10;
while (x--) {
cout << ' ' << x;
}
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++
#include <iostream>
using namespace std;
int main()
{
int x = 100;
while (0 < --------------------x) {
cout << x << " " ;
}
cout << endl
<< x;
return 0;
}
|
Output:
90 80 70 60 50 40 30 20 10
0
Share your thoughts in the comments
Please Login to comment...