A
arnuld
/* C++ Primer - 4/e
* chapter 5 - Expressions
* exercises 5.1 and 5.2
*/
#include <iostream>
int main()
{
std::cout << "I) -30 / 3 * 21 % 4: "
<< -30 / 3 * 21 % 4
<< "\n\n"
<< "II) -30 + 3 * 21 / 5: "
<< -30 + 3 * 21 / 5
<< "\n\n"
<< "III) -30 * 3 + 21 / 5: "
<< -30 * 3 + 21 /5
<< "\n\n"
<< "IV) 3 * 21 % 4: "
<< 3 * 21 % 4
<< "\n\n"
<< "V) 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2: "
<< 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2
<< std::endl;
return 0;
}
========== OUTPUT =============
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_05-01.cpp
~/programming/cpp $ ./a.out
I) -30 / 3 * 21 % 4: -2
II) -30 + 3 * 21 / 5: -18
III) -30 * 3 + 21 / 5: -86
IV) 3 * 21 % 4: 3
V) 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2: 91 ~/programming/cpp $
i understood how the 4th expressions worked, (3 * (21 % 4)), which shows
that % has higher precedence than *. 2nd expression works like: (-30 + 3
* (21 / 5)) which shows that / has higher precedence than *. i am not able
to understand the default grouping of other expressions. take the 1st
expresson:
((-30 / 3) * (21 % 4)), (-30 / ((3 * 21) % 4), (-30 / (3 * (21 % 4))
but nothing gives output of -2. same for other expressions. can anybody
point me where i am wrong ?
* chapter 5 - Expressions
* exercises 5.1 and 5.2
*/
#include <iostream>
int main()
{
std::cout << "I) -30 / 3 * 21 % 4: "
<< -30 / 3 * 21 % 4
<< "\n\n"
<< "II) -30 + 3 * 21 / 5: "
<< -30 + 3 * 21 / 5
<< "\n\n"
<< "III) -30 * 3 + 21 / 5: "
<< -30 * 3 + 21 /5
<< "\n\n"
<< "IV) 3 * 21 % 4: "
<< 3 * 21 % 4
<< "\n\n"
<< "V) 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2: "
<< 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2
<< std::endl;
return 0;
}
========== OUTPUT =============
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra ex_05-01.cpp
~/programming/cpp $ ./a.out
I) -30 / 3 * 21 % 4: -2
II) -30 + 3 * 21 / 5: -18
III) -30 * 3 + 21 / 5: -86
IV) 3 * 21 % 4: 3
V) 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2: 91 ~/programming/cpp $
i understood how the 4th expressions worked, (3 * (21 % 4)), which shows
that % has higher precedence than *. 2nd expression works like: (-30 + 3
* (21 / 5)) which shows that / has higher precedence than *. i am not able
to understand the default grouping of other expressions. take the 1st
expresson:
((-30 / 3) * (21 % 4)), (-30 / ((3 * 21) % 4), (-30 / (3 * (21 % 4))
but nothing gives output of -2. same for other expressions. can anybody
point me where i am wrong ?