Evaluating "iarray[i] < 0 && isigned" in C: Meaning Explained

  • Thread starter vorcil
  • Start date
In summary, the given code is an if statement in C that evaluates whether the value at the current index of the iarray is less than 0 AND if the value stored at the address pointed to by isigned is either 0 or 1. It is not a looping statement and the && operator is only performed after the first condition is evaluated.
  • #1
vorcil
398
0

Homework Statement



in this statement

if (iarray < 0 && *isigned)

How do I evaluate that

the code is in c

isigned is either 0 or 1

does it mean that for each value in the array that is less than 0 AND for each value in the array that is less than isigned

or does it mean for each value in the array AND a some other statement

&& isigned (where isigned is either 0 or 1)

what does it mean?

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
  • #2
vorcil said:

Homework Statement



in this statement

if (iarray < 0 && *isigned)

How do I evaluate that

the code is in c

isigned is either 0 or 1

does it mean that for each value in the array that is less than 0 AND for each value in the array that is less than isigned

or does it mean for each value in the array AND a some other statement

&& isigned (where isigned is either 0 or 1)

what does it mean?
/b]


I would think isigned must be a pointer and *isigned is the value stored at the address pointed to. So it isn't isigned which is 0 or 1, it is that value at that address that is. The && operator performs a logical AND interpreting that 1 or 0 as TRUE or FALSE.
 
  • #3
Cheers matey potatey
 
  • #4
vorcil said:
if (iarray < 0 && *isigned)

How do I evaluate that

the code is in c

isigned is either 0 or 1

does it mean that for each value in the array that is less than 0 AND for each value in the array that is less than isigned

or does it mean for each value in the array AND a some other statement

&& isigned (where isigned is either 0 or 1)

what does it mean?

This is not a looping statement. This if statement looks at only one element in the array.

The && is performed last, after iarray < 0 is evaluated. So it's equal to:
if ((iarray < 0) && *isigned)

Also, unless the first condition evaluates as true, then whatever isigned points to is not considered. So isigned need not point anywhere, not until iarray < 0 becomes true (at least as far as evaluation of this statement is concerned).
 
Last edited:
  • #5


The statement "iarray < 0 && isigned" in C is a conditional statement that is evaluating two conditions using the logical AND operator (&&). The first condition is "iarray < 0", which checks if the value at index i in the array iarray is less than 0. The second condition is "*isigned", which checks if the value of the variable isigned is equal to 1 (true).

In this statement, the code is checking if both conditions are true. If the first condition is true (iarray < 0), then the second condition is also checked. If both conditions are true, then the code will proceed to execute the following code block. If either of the conditions is false, then the code will not execute the following code block.

To evaluate this statement, you would need to provide values for the variables iarray and isigned. For example, if iarray = [1, -2, 3, -4] and isigned = 1, then the first condition (iarray < 0) would evaluate to true for the values at indices 1 and 3, while the second condition (*isigned) would evaluate to true for the value 1. Therefore, the code block following this statement would be executed for the values at indices 1 and 3 in the array iarray.

In summary, the statement "iarray < 0 && isigned" in C is evaluating if both the value at index i in the array iarray is less than 0 and if the value of the variable isigned is equal to 1. It does not mean for each value in the array AND a some other statement, but rather for each value in the array that satisfies both conditions.
 

1. What does "iarray[i] < 0 && isigned" mean in C?

The expression "iarray[i] < 0 && isigned" in C is a conditional statement that checks if the value stored in the array at index i is less than 0 and if the variable isigned is true. It will return a boolean value of true if both conditions are met, and false otherwise.

2. How is "iarray[i] < 0 && isigned" evaluated in C?

In C, the expression "iarray[i] < 0 && isigned" is evaluated from left to right. This means that the first condition, "iarray[i] < 0", is checked first. If it evaluates to false, the entire expression will return false without checking the second condition. If it evaluates to true, then the second condition, "isigned", is checked and the final boolean value is returned.

3. What happens if "iarray[i] < 0" is true but "isigned" is false?

If "iarray[i] < 0" is true but "isigned" is false, the overall expression "iarray[i] < 0 && isigned" will evaluate to false. This is because both conditions need to be true for the expression to return true. If one of the conditions is false, the entire expression will return false.

4. Can "iarray[i] < 0 && isigned" be used with any data type in C?

Yes, the conditional statement "iarray[i] < 0 && isigned" can be used with any data type in C as long as the variable isigned is a boolean value, such as true or false. This allows for flexibility in programming and can be used with integers, characters, or any other data type.

5. What is the purpose of "iarray[i] < 0 && isigned" in C?

The purpose of "iarray[i] < 0 && isigned" in C is to check if a specific condition is met before performing an action. This is useful in control structures, such as if statements, where certain code should only be executed if a certain condition is true. It allows for more precise and controlled programming in C.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
917
Back
Top