1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C - && vs ||

  1. Apr 7, 2016 #1
    1. The problem statement, all variables and given/known data
    Write a program that reads in any characters from the keyboard, sums up only characters corresponding to a digit and prints the result on the screen. The program will exit when the character 'Q' is entered (upper or lowercase).

    Example:
    Input = 9 8 q
    Output = The total is 17.

    2. Relevant equations


    3. The attempt at a solution
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main(){
    char input;
    int total = 0;
    scanf("%c", &input);
    while(input!='q' && input!='Q'){
    if(input>'9' || input<'0'){ //Input is not a valid integer
    printf("Input is not a digit\n");
    }
    else{//Input is a valid integer
    //ASCII value for 0 is 48 .
    total = total + (input-48); // Takes previous total and adds new valid integer onto it
    }
    scanf(" %c", &input);//Scans for new Input
    }
    printf("The total is %d\n", total);



    return 0;
    }


    This is my code that works perfectly, I don't understand one thing though. At the line that is bold I have said that while the input is not q AND not Q. Initially I used a || there, so it was while the input is not Q or q, that didnt work. Its not working as an OR(||) but its working as AND(&&). Why is that? Surely the input can't be Q and q at the same time? I mean only 1 would need to appear to stop the code, so why is it && and not ||.
     
  2. jcsd
  3. Apr 7, 2016 #2

    NascentOxygen

    User Avatar

    Staff: Mentor

    If input is not 'Q' AND it's not 'q' then it must be something you want to process further.

    If you used OR then you might as well have no test because your Boolean will always be TRUE. Every character you nominate will test true on one side or the other in each "not something" condition, meaning both expressions can never be simultaneously false.
     
    Last edited: Apr 8, 2016
  4. Apr 8, 2016 #3

    Mark44

    Staff: Mentor

    Your logic is such that
    Code (Text):
    if (input == 'q' || input == 'Q)
    your program should exit. In your loop, if the logical expression above is false, you want the program to proceed with another iteration of the loop.
    In other words, if the expression ~(input == 'q' || input == 'Q') is false. Due to one of de Morgan's laws, ~ (P || Q) is equivalent to (~P && ~Q), where P and Q represent the logical expressions that make up your test. In your case, ~(input == 'q' || input == 'Q') is equivalent to (input != 'q' && input != 'Q').
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C - && vs ||
  1. C/C++ hw question! (Replies: 3)

  2. C\C++ help needed (Replies: 2)

  3. C++ . (Replies: 19)

  4. Records and C++ (Replies: 3)

Loading...