Question: Consider the following two code fragments for counting spaces and newlines: // Version 1 while (cin.get(ch)) // quit on eof { if (ch == '

Consider the following two code fragments for counting spaces and newlines:

// Version 1
while (cin.get(ch)) // quit on eof
{
if (ch == ' ')
spaces++;
if (ch == '\n')
newlines++;
}
// Version 2

while (cin.get(ch)) // quit on eof
{
if (ch == ' ')
spaces++;
else if (ch == '\n')
newlines++;
}

What advantages, if any, does the second form have over the first?

Step by Step Solution

3.33 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The advantages are 1 Congruence The for loop of versio... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction Java Program Questions!