Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How does the C process a conditional statement such as n >= 1 <= 10?
I initially thought that it would get evaluated as n >= 1 && 1 <= 10, as it would be evaluated in Python. Since 1 <= 10 is always true, the second porition of the and is redundant (the boolean value of X && True is equivalent to the boolean value of X).
However, when I run it with n=0, the conditional gets evaluated to true. In fact, the conditional always seems to evaluate to true.
This was the example I was looking at:
if (n >= 1 <= 10)
printf("n is between 1 and 10\n");
>= operator is evaluated from left to right, so it is equal to:
if( ( n >= 1 ) <= 10)
printf("n is between 1 and 10\n");
The first ( n >= 1 ) is evaluated either as true or false, which is equal to either 1 or 0. Then that result of 1 or 0 is compared to result <= 10 which will always evaluate to true.
Thus the statement printf("n is between 1 and 10\n"); will always be printed
It's evaluated left to right like this:
n = 5;
if (n >= 1 <= 10)
// then
if (1 <= 10)
// then
if (1)
It first checks if n >= 1. If it is, it evaluates to 1, otherwise 0. This leads to the next evaluation, 1 <= 10, which evaluates to 1 as well. Note that this also succedes:
n = 5;
if (n >= 3 == 1)
Because it's evaluated like this:
n = 5;
if (n >= 3 == 1) // but you should never write code like this
// then
if (1 == 1)
// then
if (1)
Also note why it works with n = 0
n = 0;
if (n >= 1 <= 10)
// then
if (0 <= 10) // 0 isn't greater or equal to 1, so 0 (false) is "returned"
// then
if (1) // but 0 is less than or equal to 10, so it evaluates as 1 (true)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I need to make a program in C++ in which I insert a number 'n' that's with 4 digits and it's a simple number, in the console and the console is showing me the multiplication of the certain number with 4 digits which I first needed to write.
I tried to write (n/!2) in order to make the program execute only if the number can't divide by 2 and the console showed "main.cpp:22:36: warning: division by zero [-Wdiv-by-zero]".
I've tried removing
(n /! 2) and the code got executed without a problem. I will be glad if someone can tell me how can I fix it.
#include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int getProduct(int n)
{
int product = 1;
while (n != 0) {
product = product * (n % 10);
n = n / 10;
}
return product;
}
int main()
{
int n;
cout << "insert n ";
cin >> n;
if (n >= 1000 && n <= 9999 && n /! 2) {
cout << (getProduct(n));
}
}
In the calculation n / !2 You effectively do n / 0.
! is short for not, not 2 is a boolean expression where 2 (anything but 0) is true.
not true is false.
false is promoted to an int in the calculation and false there becomes 0.
Solution: Use n % 2 != 0 or n & 1 to check for odd numbers.
You are looking for modulo operation. Modulo (the remainder from division) is equal to zero if number is divisible by the other one and non-zero otherwise.
if (n >=1000 && n <= 9999 && (n % 2) != 0)
tried to write (n/!2) in order to make the program execute only if the number can't divide by 2
n / !2
This evaluates to n / 0. which triggers your warning.
To check if n is odd, you should do this instead:
n % 2 != 0
! operator have higher precedence than / , So your expression becomes
=> n/(!2)
=> n/0
Instead you shoud use (n%2==0) to check for even number.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Following code has no output when it executes. Can anyone explain the following code?
int main() {
int i, j;
for (i = 0; i < 10; i++) {
if (i % 2)
printf("%d\t", i);
else
break;
}
}
0 % 2 gives false so the loop terminates at the first iteration without calling the printf.
Your code has two issues:
1.
if (i % 2)
The condition of (i % 2) evaluates to false because the calculation - 0 divided by 2 - result in 0 -> 0 / 2 = 0. The remainder is also 0.
2.
else break;
That provides the result if (i % 2) is not true (which is the case with 0 at the first iteration) you immediately will break out of the for loop. Omit the break; statement in general, if you want to proof if all values from 0 to 9 have an remainder when divided by 2 or not.
Note that to pack break; in an separate else statement is nonetheless redundant.
Side note:
j has no use in your code.
I guess what you want is something like that:
#include <stdio.h>
int main (void) {
int i, j;
for ( i = 0; i < 10; i++ ) {
if ( ( j = i % 2 ) )
printf("(%d / 2) has a remainder of %d.\n", i, j);
else
printf("(%d / 2) has no remainder.\n", i);
}
}
Output:
(0 / 2) has no remainder.
(1 / 2) has a remainder of 1.
(2 / 2) has no remainder.
(3 / 2) has a remainder of 1.
(4 / 2) has no remainder.
(5 / 2) has a remainder of 1.
(6 / 2) has no remainder.
(7 / 2) has a remainder of 1.
(8 / 2) has no remainder.
(9 / 2) has a remainder of 1.
Try this code online.
Note that by the division 1 / 2, the result 0.5 is raised to the nearest upper integral value 1 because of the implicit double to int conversion.
i % 2 gives the remainder of the Euclidian division of i by 2. It does NOT mean "i is divisible by 2". If the latter is true, then i % 2 gives 0 which implicitely converts to false. If it is false, then i % 2 gives 1 which implicitely converts to true. Therefore, in the first iteration, the conditional ex^pression evaluates to false so you break out of the loop immediately.
If you want to check the divisibility of i by 2, you must use i % 2 == 0.
This question already has answers here:
Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?
(5 answers)
Closed 4 years ago.
We usually use logical operators if need to combine boolean expressions. I was wondering about the expressions if don't use logical operators.
int x=101;
if(90<=x<=100)
cout<<'A';
This code still prints 'A' on console. Can you please help me to understand that how and in which order this boolean expression would be evaluated.
Since the operators have equal precedence, the expression is evaluated left-to-right :
if( (90 <= x) <= 100 )
if( (90 <= 101) <= 100 ) // substitute x's value
if( true <= 100 ) // evaluate the first <= operator
if( 1 <= 100 ) // implicit integer promotion for true to int
if( true ) // evaluate the second <= operator
To achieve the comparison you want, you would use the condition :
if( 90 <= x && x <= 100)
This is a common source of errors, because it looks right, and sytactically it is correct.
int x=101;
if(90<=x<=100)
This is equivalent to
if ( (90 <= x) <= 100)
which is
if ( true <= 100 )
and as true can convert to 1 this is
if ( true )
This expression is roughly equals to
int x=101;
bool b1 = 90 <= x; // true
bool b2 = int(b1) <= 100; // 1 <= 100 // true
if(b2)
cout<<'A';
So here is true result.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
THEY WERE ASKING ME TO ADD SOMETHING
.I DINT KNOW WHAT TO ADD.THIS LINE IS A WASTE.
SUGGEST ME AN EDIT
#include
using namespace std;
typedef long long lli;
lli mod = 1000000007;
int n;
char a[200000 + 10];
lli dp[200000 + 10][9];
lli solve(int pos, int rem)
{
if (pos == n) //**HERE**
return (rem == 0);
if (dp[pos][rem] != -1)
return dp[pos][rem];
dp[pos][rem] = 0;
if (pos + 1 <= n)
dp[pos][rem] = solve(pos + 1, (rem * 10 + (a[pos] - '0')) % 8);
if (pos + 1 <= n)
dp[pos][rem] += solve(pos + 1, rem);
dp[pos][rem] %= mod;
return dp[pos][rem];
}
rem==0 returns either true or false ,
Eg :
rem=5;
rem=rem-5;
if(a==0)
cout<<"YES";
whereas
rem=0;
makes the rem variable have a value of 0.
rem == 0
Checks if the value of rem operand is equal to Zero or not, if yes then condition becomes true.
rem=0;
Simple Assigns value from right side to left side operand.
there is a big difference between '==' and '=' operator.
'==' is a RELATIONAL OPERATOR
It checks if the values of two operands are equal or not. If yes, then the condition becomes true else it becomes false.
POSSIBLE USE
1. In if else loops
'=' is an ASSIGNMENT OPERATOR
It assigns values from right side operands to left side operand.
POSSIBLE USE
1. During variable declaration and value assignment
The difference between the 2 is a difference between the assignment operator and the comparison operator.
rem == 0 is an example of a comparison operator, because a comparison is being made to see if the value of rem is zero or not. In this case, it will NOT set rem to zero!
If rem is equal to zero, then the following line in your code:
return (rem == 0);
is the same as
return true; // or return 1, both mean the same thing in the bool expression. It returns true as the statement is true
However if this is not the case, that rem is not equal to 0, then:
return (rem == 0);
Will be the same as:
return false; // or return 0, because the statement is false because rem is not equal to 0
Moving on to what rem = 0 does is that it simply assigns the variable on the left of the equal sign(i.e rem) the value that is on the right of the equal sign (i.e 0). This statement makes no comparison; it merely is used to assign values.
This question already has answers here:
Is Short Circuit Evaluation guaranteed In C++ as it is in Java?
(2 answers)
Closed 9 years ago.
main()
{
int k = 5;
if(++k <5 && k++/5 || ++k<=8); // how to compiler compile this statement
print f("%d",k);
}
// Here answer is 7 but why ?
++k < 5 evaluates to false (6 < 5 = false), so the RHS of the && operator is not evaluated (as the result is already known to be false). ++k <= 8 is then evaluated (7 <= 8 = true), so the result of the complete expression is true, and k has been incremented twice, making its final value 7.
Note that && and || are short circuit boolean operators - if the result of the expression can be determined by the left hand argument then the right hand argument is not evaluated.
Note also that, unlike most operators, short circuit operators define sequence points within an expression, which makes it legitimate in the example above to modify k more than once in the same expression (in general this is not permitted without intervening sequence points and results in Undefined Behaviour).
Unlike many questions like this, it appears to me that your code actually has defined behavior.
Both && and || impose sequence points. More specifically, they first evaluate their left operand. Then there's a sequence point1. Then (if and only if necessary to determine the result) they evaluate their right operand.
It's probably also worth mentioning that due to the relative precedence of && and ||, the expression: if(++k <5 && k++/5 || ++k<=8) is equivalent to: if((++k <5 && k++/5) || ++k<=8).
So, let's take things one step at a time:
int k = 5;
if(++k <5 &&
So, first it evaluates this much. This increments k, so its value becomes 6. Then it compares to see if that's less than 5, which obviously produces false.
k++/5
Since the previous result was false, this operand is not evaluated (because false && <anything> still always produces false as the result).
|| ++k<=8);
So, when execution gets to here, it has false || <something>. Since the result of false | x is x, it needs to evaluate the right operand to get the correct result. Therefore, it evaluates ++k again, so k is incremented to 7. It then compares the result to 8, and finds that (obviously) 7 is less than or equal to 8 -- therefore, the null statement following the if is executed (though, being a null statement, it does nothing).
So, after the if statement, k has been incremented twice, so it's 7.
In C++11, the phrase "sequence point" has been replaced with phrases like "sequenced before", as in: "If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression." Ultimately, the effect is the same though.
In following:
for && "something" is evaluated when first condition is True
for || "something" is evaluated when first condition is False
( (++k <5) && (k++/5) ) || (++k<=8)
( 6 < 5 AND something ) OR something
( False AND something ) OR something
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
False OR 7 < 8
False OR True
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
True
So k comes out to be 7
Initially k is assigned 5, in your declaration, then in following if condition:
++k < 5 && k++ / 5 || ++k <= 8
// ^
// evaluates
Increments k to 6 then its and LHS of && operand evaluates false.
6 < 5 && k++ / 5 || ++k <= 8
// ^ ^^^^^^^
// 0 not evaluates
Then because of Short-Circuit behavior of && operator k++ / 5 will not evaluates.
Short-Circuit behavior of && operator is:
0 && any_expression == 0, so any_expression not need to evaluate.
So above step 2 conditional expression becomes:
0 || ++k <= 8
// ^^^^^^^^
// evaluates
Increments k to 7 then its:
0 || 7 <= 8
because you have ; after if, so no matter whether if condition will evaluates True or False printf() will be called with k = 7.