bool expression seems logical but doesn't work

boolean expression examples
boolean logic
boolean expression python
compound boolean expression python
boolean example
boolean expressions java
boolean expression calculator
logical operators

I'm reading 3 integers in my C program and storing them in an int *[]. While reading the 3rd int however, I want to compare it to the previous 2 and if it's greater than both of them the program ends, if it isn't the program will keep on reading the 3rd. input until it satisfies the condition in the while loop.

My issue is, although the bool expression seems logical (to me at least) and the values I enter are successfully stored in the array, why does it always skip the while loop?

#include <stdio.h>

int main(void) { // Stelios Papamichail 4020
    int *sides[3];
    int i;
    for(i=0; i < 3; i++) {
        if(i == 2) {// third side
            scanf(" %d",&sides[i]);
            while((sides[i] < sides[i-1]) && (sides[i] < sides[i-2])) {
                scanf(" %d",&sides[i]);
            }
        } else {
            scanf(" %d",&sides[i]);
        }
    }
    return 0;
}

The main problem in your code is, it invokes undefined behavior.

To elaborate, in your code, by saying

 int *sides[3];

you are defining an array of 3 int *s (array of 3 pointers to integer), whereas, what you need is an array of 3 ints. Change it to

int sides[3];

That said, based on your requirement, the && in the while condition should be ||, if you want to check for the third input to be the smallest.

Conditionals with if/else & Booleans | AP CSP (article), How to use conditionals (if/else) with Boolean expressions to make decisions in computer programs. Includes links The general structure of every if statement looks like this: Now you can easily see what you can and cannot do at any age. // True bool b = false | SecondOperand(); Console.WriteLine(b); // Output: // Second operand is evaluated. // True The conditional logical OR operator || also computes the logical OR of its operands, but doesn't evaluate the right-hand operand if the left-hand operand evaluates to true.

You say:

I'm reading 3 integers in my C program and storing them in an int *[]. While reading the 3rd int however, I want to compare it to the previous 2 and if it's greater than both of them the program ends, if it isn't the program will keep on reading the 3rd. input until it satisfies the condition in the while loop.

well, an int is not the same as a pointer. An int allows you to do integer arithmetic, while an int * allows you to do pointer arithmetic. (the pointer stores the memory address of an int variable, and as such, when you increment it, it moves its value to the next address, which is not the same as adding one ---because an int needs more than one byte to fit in memory) You can use int * in your program (as you say in the comments, your teacher has indicated that) but that's like making an omelette but using apples instead of eggs (well, they are both round, but the result is not the same). I cannot guess the reason your teacher asked you to use int *, but that gives me warnings from the compiler, advising me of the risk of doing this (compiler is CLANG, on FreeBSD)

My issue is, although the bool expression seems logical (to me at least) and the values I enter are successfully stored in the array, why does it always skip the while loop?

Well, that depends on what you try to check. The first thing on this is that you are say in the code is compare the last input number with the earlier ones, and if it is greater than or equal than any of them, then input the third number again. That's correct if that is what you want, but I cannot guess also what is the purpose of doing such a test, so the only thing I can conclude is that the test is not well written (and mainly because of what I'm going to say next).

The third comment about your program is:

If you need to check the last value against the previous ones, why don't do that outside of the loop. Doing so not only allows your code to look simpler, but also eliminates the need of the if (i == 2) test (you don't need to use i at all in your code, just write:

    for(i=0; i < 3; i++) {
        scanf("%d",&sides[i]); /* you don't need the space before %d */
    }
    while((sides[2] < sides[1]) && (sides[2] < sides[0])) {
        scanf("%d",&sides[2]);
    }

Last, you say in a comment that changing int *sides[3] to just int sides[3] makes compilation fail. I've tried and just eliminating that * just eliminates three warnings I get from your code compilation. Cannot guess why do you say it doesn't compile with that elimination.

NOTE

Your code seems to try to check if the three sides of a triangle allow you to build one. If that's is true, the correct test is for the third triangle to be greater than the difference of the previous sides and less than the sum of the previous sides, and this is not what you have written above. A valid test should be:

#define ABS(expr) ((expr) < 0 ? -(expr) : (expr))

while (sides[2] >= sides[1] + sides[0] || sides[2] <= ABS(sides[1] - sides[0])) {
    scanf("%d", &sides[2]);
}

Compound Booleans: AND/OR/NOT | AP CSP (article), How to write compound Boolean expressions with the logical operators AND, OR​, Using the OR operator, we can create a compound expression that is true  Logical AND performs Boolean operation with two expression and returns true if both expressions are true and returns false if any one expression is false. If first expression is false then it will not check another expression and returns false. Let’s have a look on below code snippet.

An array go given size is declared with:

int sides[3];

while an array of unknown size (a pointer basically) is declared with:

int* sides;//you should use malloc to allocate the array memory at run time.

Using

int* sides[3];

you are declaring an array of pointers.

Your final code should look like:

#include <stdio.h>

int main(void) { // Stelios Papamichail 4020
    int sides[3];
    int i;
    for(i=0; i < 3; i++) {
        if(i == 2) {// third side
            printf("%d %d %d\n", sides[i], sides[i-1], sides[i-2]);

            scanf(" %d",&sides[i]);
            while((sides[i] < sides[i-1]) && (sides[i] < sides[i-2])) {
                printf("%d %d %d\n", sides[i-2], sides[i-1], sides[i]);
                scanf(" %d",&sides[i]);
            }
        } else {
            printf("%d %d %d\n", sides[i-2], sides[i-1], sides[i]);
            scanf(" %d",&sides[i]);
        }
    }
    return 0;
}

I added some prints to understand what's going on.

Boolean Expressions, More complex boolean expressions can be built out of simpler expressions, using the following boolean operators: exclusive or, True if either side is true (​but not both), walking ^ ridingBus The comparison is redundant and just looks silly. A Boolean expression is an expression that evaluates to a value of the Boolean Data Type: True or False. Boolean expressions can take several forms. The simplest is the direct comparison of the value of a Boolean variable to a Boolean literal, as shown in the following example.

3.6 Boolean Expressions, A Boolean expression is a logical statement that is either TRUE or FALSE . part that does not show in output because of the setting of the DECIMALS option. 100.000001 and the number of decimal places is two, then the value appears in  Suppose the score is 34 and the average is 63. The compiler applies the precedence rule and interprets the Boolean expression as the following ( ! score ) > average This turns out to evaluates to false for the following reasons. First because ! is a unary logical negation operator and the value of score is nonzero and so score is converted to true.

Logical operators, But first, let's see what happens with boolean values. Getting the first truthy value from a list of variables or expressions. if ( 1 && 0 ) { // evaluated as true && false alert ( "won't work, because the result is falsy" ) ; } Although, the variant with && appears shorter, if is more obvious and tends to be a little  Boolean.FALSE : Boolean.TRUE) and it works fine if. both fileds are empty; first field (riepilogo_totaleDichiarazioni) is set and the second (riepilogo_totaleDistinte) is empty; but doesn't work fine if the first field is empty and the secondo in set. It seems not evaluate the second condition after the && operator. Any suggestion?

5. Conditionals, A Boolean expression is an expression that evaluates to produce a result Similarly, for the and operator, if the expression on the left yields False, Python does not There is no limit on the number of statements that can appear under the two  Solved: Hello fellows, I would like to write the following expression (pseudo-code): // define boolean variable let variable = true; // check if - 625387

Comments
  • !((c > a) && (c > b)) is equivalent to ((c <= a) || (c <= b)), not to ((c < a) && (c < b))
  • A int *[] is not an array of integers... but an array of pointers to integers. You must drop the * in your declaration.
  • It looks like you forgot to enable a good set of warnings. For GCC, I recommend -Wall -Wextra -Wwrite-strings as a minimum; consider also -Wpedantic -Warray-bounds for identifying some other common mistakes.
  • Our teacher requested to use an array of int pointers so i must use that. Is it wrong? I changed it earlier to an int array just out of curiosity and that didn't work either
  • @SteliosPapamichail I dont see why'd you need an array of pointers. However, I updated my answer a bit, check if it works.
  • it's probably used later on in the exercise :) . Your edit solved it and i see why i should have used || instead of &&. My bad and thanks for the help
  • @SteliosPapamichail, an array of pointers is being filled with integer values. Nothing wrong, but as they are integers, you must not consider them as pointers. I don't fully understand the reasons your teacher argue to use pointers instead of integers, but you'll run into trouble and run into problems by using the wrong type to operate with numbers. Pointers and numbers, are comparable, and you can do arithmetic with them. But arithmetic and comparison rules differ between pointers and integers and you'll run into trouble for sure in the future.
  • @SouravGhosh, why do you say this code invokes Undefined Behaviour? can you be more specific?