Hello everyone!
I am new in C++ programming community and of course I have a few questions to ask as a beginner.
1. In one closed topic on this forum, I've seen a strange kind of for loop which looked like this:
1 2 3
for(;;){
//instructions...
}
And I was wondering about parameters for the loop itself. Why are there just two semicolons? That looks weird to me because I'm used to something like this:
1 2 3
for(int x=0;x<10;x++){
//instructions...
}
So if someone would explain that to me it would be very helpful. :)
2. The second question is about this kind of an if statement:
1 2 3
if(a >> b){
//instructions...
}
I do not understand the logic behind this expression: "a >> b" because I have never seen that kind of logical operator so if anyone can explain this I would really appreciate it. :)
In the for loop you can omit any part of the statement. If the middle part of the statement is empty that is if there is no an expression or declaration then the condition of the loop is considered as equal to true. Instead of
for( ; ; )
I prefer to write
while ( true )
The last record is more clear than the first.
As for the statement
if(a >> b){
//instructions...
}
then it seems that there is the shift right operator used for integer values . If the result expression is not equal to zero then the compound statement is executed.
I now understand the answer to the first question.
I'm not that familiar with bitwise operators still although I have done some reading on them. It is not byteshifting for sure, but in that example code an input from the user was converted from string to int using stringstream function. So it was like:
1 2 3
if (string_variable >> int_variable){
//instructions...
}
In this case this statement checks whether the input was successful. Any stream has an explicit conversion operator to the type bool. If the state of the stream is o'k then this conversion operator returns true.