NO

Author Topic: Need help to understand if else statement in my code.  (Read 4015 times)

Shikatsu

  • Guest
Need help to understand if else statement in my code.
« on: March 20, 2011, 07:48:32 PM »
Greetings EveryOne :D

* Exercise: Write a program that prints its input one word per line.

This is the code that i came up with:

Code: [Select]
#include <stdio.h>

int main()
{
    int c;

    while((c = getchar()) != EOF)
    {
        if(c != ' ' || c != '\n' || c != '\t')
        {
            putchar(c);
        }
        else
            if(c == ' ' || c == '\n' || c == '\t')
            {
                putchar('\n');
            }
    }
}

the upper code is not returning the result i seek, but the next one does:

Code: [Select]
#include <stdio.h>

int main()
{
    int c;

    while((c = getchar()) != EOF)
    {
        if(c != ' ' || c != '\n' || c != '\t')
        {
            putchar(c);
        }

        if(c == ' ' || c == '\n' || c == '\t')
        {
            putchar('\n');
        }
    }
}

Can someone tell me please why "if else" is not an answer for the exercise?

Thank You In Advance ;D
« Last Edit: March 20, 2011, 07:54:47 PM by Shikatsu »

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Need help to understand if else statement in my code.
« Reply #1 on: March 20, 2011, 08:53:02 PM »
The first line is the problem. The result of the condition will be each time true.

You should use the first line in this way:
Code: [Select]
if (!(c == ' ' || c == '\n' || c == '\t'))... and then you can throw away the second if().

But I would change the then and else part and write the code in this way:
Code: [Select]
if(c == ' ' || c == '\n' || c == '\t')
{
    putchar('\n');
}
else
{
    putchar(c);
}
best regards
 Alex ;)

Shikatsu

  • Guest
Re: Need help to understand if else statement in my code.
« Reply #2 on: March 20, 2011, 09:13:21 PM »
Thank you AlexN, but i still dont get it.

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: Need help to understand if else statement in my code.
« Reply #3 on: March 21, 2011, 08:44:00 AM »
Thank you AlexN, but i still dont get it.
If c==' ' then will be c != '\n' and c != '\t' true and you or the single results - so the complete result will be true. The same is for c=='\n' and c=='\t' and if the content of c is any other charater the result of all three conditions are true. How ever the complete result will be true in each case,
best regards
 Alex ;)

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Need help to understand if else statement in my code.
« Reply #4 on: March 21, 2011, 12:36:45 PM »
Even simpler, you want to write the char
    if it is not <space> *and* not <tab> *and* not <newline>
        then print it
    else
        jump to new line
The logical operator that is true only when all its input conditions are true is the *and* operator. Using the 'or' if only one condition is true the output is true.
Now, as AlexN already said, if your input is one of the three spacing codes the other two conditions will be true anyway. So you will never reach the else.
Moreover a comparison is a binary branch meaning that you have two directions you can follow: one if the 'if expression' is true and the other if it false. So you don't need to add another if after else to check again the reverse result.
I suggest you to review logical operators and boolean algebra. (Maybe you want apply the 'break the bar and change the sign' rule to AlexN's proposed 'if (!(c == ' ' || c == '\n' || c == '\t'))' to verify that it is exactly the same reported below using the 'and' operator).
The correct code is simply:
Code: [Select]
#include <stdio.h>

int main()
{
    int c;

    while((c = getchar()) != EOF)
    {
        if(c != ' ' && c != '\n' && c != '\t')
            putchar(c);
        else
            putchar('\n');
    }
}

(The brackets around the if are unnecessary because you have only one statement, and also the brackets of the 'while' conditions would be unnecessary because you have only the 'if' block)
« Last Edit: March 21, 2011, 12:43:51 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Shikatsu

  • Guest
Re: Need help to understand if else statement in my code.
« Reply #5 on: March 22, 2011, 12:02:00 AM »
Thank you frankie and AlexN for the help, i got it now.
the problem i wasn't thinking clearly my head was fuzzy ^^