473,388 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Correct C++ tutorial part 4 "Decisions & conditions" available (Windows, mingw/msvc/std)

The fourth part of my attempted Correct C++ tutorial is now available,
although for now only in Word format (use free Open Office if no Word),
and also, it's not yet been reviewed at all -- comments welcome!

"Decisions & conditions"
<url: http://home.no.net/dubjai/win32cpptut/w32cpptut_01_04.doc>

Different from before I don't expect that this part contains mental food
and technical points that we can strongly disagree about, so I'm mainly
inviting comments from those who are in the process of learning C++, or
who are thinking about starting with C++.

As before, however, the point of inviting comments is to _fix_ anything
incorrect, or the presentation, or whatever; your input is valuable,
even if you're an experienced C++ programmer! ;-)

The first two parts, "Hello, world!" and "Variables", are available
both as Word docs and HTML at

<url: http://home.no.net/dubjai/win32cpptut/html/>

The third part is referred to from that page, but only as a Word doc.

Thanks in advance,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #1
7 1570
Alf P. Steinbach wrote:
The fourth part of my attempted Correct C++ tutorial is now available,
although for now only in Word format (use free Open Office if no Word),
and also, it's not yet been reviewed at all -- comments welcome!

"Decisions & conditions"
<url: http://home.no.net/dubjai/win32cpptut/w32cpptut_01_04.doc>

Different from before I don't expect that this part contains mental food
and technical points that we can strongly disagree about, so I'm mainly
inviting comments from those who are in the process of learning C++, or
who are thinking about starting with C++.

As before, however, the point of inviting comments is to _fix_ anything
incorrect, or the presentation,


Here is my comment (presentation aspect) regarding the same.

In Section 3 , titled - "Use !, && and || for logical NOT, AND and OR."
here is a sample code fragment.

if( command == "H" || command == "h" || command == "?" ) {
// This is a help command.
std::cout << helpText << std::endl;
}

It would be nice, if you orient the style of putting the constant
on the l.h.s in case of a conditional expression ( as in this case).
That can obviously help trap errors out of typos of replacing '=='
by '=' , (say, command = "H" ).

That should really a really good programming tip to be mentioned
at this stage.

--
Karthik.
Jul 22 '05 #2
* Karthik Kumar:

In Section 3 , titled - "Use !, && and || for logical NOT, AND and OR."
here is a sample code fragment.

if( command == "H" || command == "h" || command == "?" ) {
// This is a help command.
std::cout << helpText << std::endl;
}

It would be nice, if you orient the style of putting the constant
on the l.h.s in case of a conditional expression ( as in this case).
That can obviously help trap errors out of typos of replacing '=='
by '=' , (say, command = "H" ).

That should really a really good programming tip to be mentioned
at this stage.


Thanks for that suggestion.

At the very least the fact that you mention it means I should discuss
that more clearly, and/or in one place, and/or perhaps yet again in this
fourth part.

In part 3 I discussed that problem and introduced two devices that tend
to catch most of the '=' errors: a hack that ensures that an 'if' (or
'while') condition is of 'bool' type, and use of 'const'.

Of course that doesn't catch 100% of cases, and cannot even in
principle, AFAICS, catch e.g. that error in a conditional operator, and
'const' is of limited utility until user-defined functions are
discussed.

However, in my experience the problem is sufficiently reduced that
readability of the code then weights more: one shouldn't, IMO, willingly
do over and over the compiler's job, especially when readability suffers
as a result, but find some way (such as the abovementioned hack and use
of 'const') to make the compiler do the job -- to _automate_ things.

Cheers, and thanks again,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #3

"Karthik Kumar" <ka*******************@yahoo.com> skrev i en meddelelse
news:41f3826a$1@darkstar...
[snip]
Here is my comment (presentation aspect) regarding the same.

In Section 3 , titled - "Use !, && and || for logical NOT, AND and OR."
here is a sample code fragment.

if( command == "H" || command == "h" || command == "?" ) {
// This is a help command.
std::cout << helpText << std::endl;
}

It would be nice, if you orient the style of putting the constant
on the l.h.s in case of a conditional expression ( as in this case).
That can obviously help trap errors out of typos of replacing '=='
by '=' , (say, command = "H" ).
Hi Karthlik

I find command == "H" far more readable than "H" == command and thus prefer
the first method. Most - if not all - compilers will warn you if you try to
assign rather than compare. If that was not the case, you could persuade me
to write it your way.

/Peter

That should really a really good programming tip to be mentioned
at this stage.

--
Karthik.

Jul 22 '05 #4
Peter Koch Larsen wrote:
"Karthik Kumar" <ka*******************@yahoo.com> skrev i en meddelelse
news:41f3826a$1@darkstar...
[snip]
Here is my comment (presentation aspect) regarding the same.

In Section 3 , titled - "Use !, && and || for logical NOT, AND and OR."
here is a sample code fragment.

if( command == "H" || command == "h" || command == "?" ) {
// This is a help command.
std::cout << helpText << std::endl;
}

It would be nice, if you orient the style of putting the constant
on the l.h.s in case of a conditional expression ( as in this case).
That can obviously help trap errors out of typos of replacing '=='
by '=' , (say, command = "H" ).

Hi Karthlik

I find command == "H" far more readable than "H" == command and thus prefer
the first method.


I felt the same, until I was oriented to this style.

Most - if not all - compilers will warn you if you try to
assign rather than compare. If that was not the case, you could persuade me
to write it your way.


Well- here is a test code.

int main() {
int a;
if (a = 4) {
// do nothing
}

}
I primarily use the gnu c/c++ compiler, although i use intel's
compiler as well .

As can be seen below, g++ does not seem to warn me,
but icc does.

$ g++ -Wall -ansi -pedantic if_test.cpp
<< no warnings >>

$ icc -Wall if_test.cpp
if_test.cpp(3): warning #187: use of "=" where "==" may have been intended
if (a = 4) {
^

if_test.cpp(2): remark #593: variable "a" was set but never used
int a;
^

The point is, you are dependent on the compiler
to identify the error and there is at least one compiler,
that fails to help in this case. Hence for a tutorial, it
would be a nice idea to orient this practice. Now whether anyone
picks this one up is a matter of personal choice.

--
Karthik.
Jul 23 '05 #5
* Karthik Kumar:
Well- here is a test code.

int main() {
int a;
if (a = 4) {
// do nothing
}

}

I primarily use the gnu c/c++ compiler, although i use intel's
compiler as well .

As can be seen below, g++ does not seem to warn me,
but icc does.

$ g++ -Wall -ansi -pedantic if_test.cpp
<< no warnings >>

$ icc -Wall if_test.cpp
if_test.cpp(3): warning #187: use of "=" where "==" may have been intended
if (a = 4) {
^

if_test.cpp(2): remark #593: variable "a" was set but never used
int a;
^

The point is, you are dependent on the compiler
to identify the error


Well, not in this particular case. You could use the hack introduced
in part 3 in the section that discusses this problem. OK, it's not
formally correct to redefine keywords, but...
template<typename T> inline T ifCondition( T x );
template<> inline bool ifCondition( bool x ){ return x; }
#define if( expression ) if( ::ifCondition( expression ) )

int main() {
int a;
if (a = 4) {
// do nothing
}
}
I don't have the Intel compiler, but g++ 3.4.2 then reports

x.cpp:1: warning: inline function `T ifCondition(T) [with T = int]' used
but never defined
C:\WINDOWS\TEMP/ccM5baaa.o(.text+0x39):x.cpp: undefined reference to
`int ifCondition<int>(int)'
collect2: ld returned 1 exit status

Similarly, Visual C++ 7.1. reports

x.obj : error LNK2019: unresolved external symbol "int __cdecl
ifCondition<int>(int)" (??$ifCondition@H@@YAHH@Z) referen
ced in function _main
x.exe : fatal error LNK1120: 1 unresolved externals

As a refinement you can add a compile-time assert to the mix.

And when you really do want to do the assignment you can
write 'if( !!(a = 4) ) { ... }'.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #6
Karthik Kumar wrote:
Peter Koch Larsen wrote:
I find command == "H" far more readable than "H" == command and
thus prefer the first method.


I felt the same, until I was oriented to this style.
int a;
if (a = 4) {
}

As can be seen below, g++ does not seem to warn me,
but icc does.

$ g++ -Wall -ansi -pedantic if_test.cpp
<< no warnings >>


Does g++ have an option for increasing the warning level or something
similar ?

It seems to me that it's better to make just one change to the make file
than to rely on the programmer always remembering to use an unnatural coding
style...

And if the compiler doesn't have an option to flag this kind of error with a
warning, this is a strong indication that the compiler has plenty of other
weaknesses as well. In that case the solution is simple - use a different
compiler.
--

Sigurd
http://utvikling.com
Jul 23 '05 #7
Karthik Kumar wrote:
Peter Koch Larsen wrote:
"Karthik Kumar" <ka*******************@yahoo.com> skrev i en
meddelelse news:41f3826a$1@darkstar...
[snip]
Here is my comment (presentation aspect) regarding the same.

In Section 3 , titled - "Use !, && and || for logical NOT, AND and OR."
here is a sample code fragment.

if( command == "H" || command == "h" || command == "?" ) {
// This is a help command.
std::cout << helpText << std::endl;
}

It would be nice, if you orient the style of putting the constant
on the l.h.s in case of a conditional expression ( as in this case).
That can obviously help trap errors out of typos of replacing '=='
by '=' , (say, command = "H" ).


Hi Karthlik

I find command == "H" far more readable than "H" == command and thus
prefer the first method.

I felt the same, until I was oriented to this style.

Most - if not all - compilers will warn you if you try to assign
rather than compare. If that was not the case, you could persuade me
to write it your way.

Well- here is a test code.

int main() {
int a;
if (a = 4) {
// do nothing
}

}
I primarily use the gnu c/c++ compiler, although i use intel's
compiler as well .

As can be seen below, g++ does not seem to warn me,
but icc does.

$ g++ -Wall -ansi -pedantic if_test.cpp
<< no warnings >>

I'm not sure which version of g++ you are using. I have 3.3.3 installed,
and I get:

temp.cc: In function `int main()':
temp.cc:3: warning: suggest parentheses around assignment used as truth
value

Chris
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nimmi Srivastav | last post by:
When I was learning C, I learned that the data type of a function name by itself is a function pointer. For example int someFunction(char* str) { .... }
11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
12
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
2
by: arnuld | last post by:
on page 29, section 1.9 Character Arrays, i see an example: /* getline: read a line into s, return length */ int getline(char s,int lim) { int c, i; for (i=0; i < lim-1 &&...
0
by: newbie73 | last post by:
Going through the tutorial on http://swig.org, I created the example files (pasted below). After generating the _wrap file, I tried compiling (using mingw32) and received a lot of undefined...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.