Can someone please explain the output to me? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
okay, so when i change the " = " in if( (i = 4) || (i = 5) ) to "==" it returns hello world. But when the "=" is kept at "=" the output is nothing. It does not give me a syntax error cuz ik you need to put "==" inside an if
void f( int i )
{
if( (i = 4) || (i = 5) ) return;
cout << "hello world\n" ;
}
int main()
{
f( 3 );
f( 4 );
f( 5 );
return 0;
}
So when the code is "if( (i = 4) || (i = 5) )" output is nothing (as in the screen is empty).
when the code is "if( (i == 4) || (i == 5) )" output is hello world.
my main question is: Why does == and = make a difference in the output but not give me a syntax error?

As #Carcigenicate said, == and = is different.
== is for comparing and = is for assigning.

An assignment a = b does not only set the value in variable a to b, but also returns the value of b. This way, an assignment like a = b = c is possible, because the value returned to be put in a is the same as has been set to b. You can use this trick in conditionals, as you've done, for example:
int x;
while(x = functionWhichCouldReturnZero()){
// Do something with x
}
When C++ tries to interpret the return value as a boolean (true or false), it interprets 0 as false, and all other values as true.
On the other hand, with (a == b), this is a pure conditional, and returns true if a is equal to b, otherwise it returns false.

Related

Why does == work and = does not in an if statement? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
for (unsigned int i = 0; i < list.size(); i++) {
if (list.at(i) = n) {
cout << "True";
return 0;
}}
I was wondering why this would not work I understand that tou should use list.at(i) == n.
However i thought that a single = means assigning, and a double == means equal to. I understand it is different but wouldn't using only one = still be correct when using it in an if statement?
It would not necessarily be correct. When you use an assignment expression as a boolean for integers, it will return true if the integer is not zero, and it will return false if the integer is zero.
Suppose our list looks like this: 1, 2, 0, 5. Now, suppose we have this if-statement:
if (list.at(0) = 1) {
cout << "True";
}
Since the 1 in list.at(0) = 1 is not 0, the if-condition will be satisfied. If we used ==, it would be satisfied since the first value is indeed 1.
Now let's suppose we have this if-statement:
if (list.at(1) = 3) {
cout << "True";
}
The "True" would be printed because 3 is not equal to 0. However, if we replaced = with ==, the "True" would not be printed since the second value is not 3.
Let's look at one last example.
if (list.at(2) = 0) {
cout << "True";
}
This would not print out "True" since we are assigning list.at(2) to 0. However, if we replaced the = with ==, the "True" would be printed since the third value in the list is actually 0.
This shows that = cannot be used as ==.
P.S. And, if you wanted to use the list later, your list would be modified into a different list.
if (condition) {
// block of code to be executed if the condition is true
}
Here is your code list.at(i) = n is an assignment and not a condition. Seeing your code I can say that you want to check if any value in the list is equal to n or not if it is then you want to print True.
So to do this you have to use a condition if(list.at(i)==n).
For more information read about condition statements, comparison operators, and assignment operators.

Is this explanation of ternary operator valid? [closed]

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 3 years ago.
Improve this question
Question:
I want to know if this explanation of the ternary operator is valid.
var = (condition) ? set value if condition one : set value of condition two;
If the condition is something than the value of the variable will be something. If it's not the value will be different. Basically assign a variable with a value based on a condition. Is this explanation valid? I need to know this if I'm understanding this correctly.
Code:
#include <iostream>
bool maxEntries()
{
int entries = 11;
bool users = (entries > 10) ? true : false;
return users;
}
int main(int argc, const char* argv[])
{
if(maxEntries())
{
std::cout << "Entries are greater than 10." << std::endl;
} else {
std::cout << "Entries are less than 10." << std::endl;
}
return 0;
}
.
I want to know if this explanation of the ternary operator is valid.
That explanation is not for ternary operator, but for expression that assigns to a variable result of ternary operator. Ternary operator itself is more than that:
int a = 0, b = 0;
condition ? a = 1 : b = 2; // based on condition assign 1 to a or 2 to b
( condition ? a : b ) = 100; // based on condition assign 100 to a or b
in your case
condition ? value1 : value2; // result of ternary operator either value1 or value2 based on condition
you then use the result to assign to a variable, so your explanation is not for ternary operator but for whole expression that uses ternary:
var = condition ? value1 : value2;
you may think this is neat picking but if somebody would read your explanation he may think that assignment to a variable is part of the ternary operator itself, but that is not the case. For example:
function_call( condition ? value_on_true : value_on_false );
Also this at least confusing: "If the condition is something" it should be "if the condition is true then first otherwise (condition is false) the second.

Get rid of if statement [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In code I come to situation like this:
if (a && b || c && d || e && f || g && h){
// do something
}
Like this:
if len(env.workers) == 0 && env.minQueue.Len() == 0 || len(env.workers) == len(env.daemonList) && env.minQueue.Len() == 0 || len(env.workers) > 0 && len(env.workers) == len(env.daemonList) {
env.shouldStop = true
return nil
}
But it's hard to debug and find errors. Is there any way to use more friendly constructuion to replace such statement.
As #Eugene mentioned it's always good idea to break long expressions like this into multiple smaller expressions.
exp1 = a && b
exp2 = c && d
exp3 = exp1 || exp2
exp4 = e && f
exp5 = g && h
exp6 = exp4 || exp5
exp7 = exp3 || exp6
if(exp7){
//doSomething
}
This may look absurd in beginning but believe me it has long way to go, at any point you can come back to the above code and easily understand what's cooking there. In fact if you like using debuggers then doing this would make your life way easier.
Also in point of performance, all you are doing is making extra 7 boolean variables. It's insignificant when code readability is concerned. And the thumb rule for better code readability is naming your variable right, not exp1,2,....
You use len(env.daemonList), len(env.workers) and env.minQueue.Len() multiple times. Storing them in variables not only shortens up that long condition, but also gives you variables that can be referenced when debugging.
You could write it as:
w_len = len(env.workers)
d_len = len(env.daemonList)
q_len = env.minQueue.Len()
if w_len == 0 && q_len == 0 || w_len == d_len && q_len == 0 || w_len > 0 && w_len == d_len {...
Now, of course the problem here is that while shorter, the names aren't as descriptive. You could give them better names at the cost of verbosity. How much you want to lean in each direction is a matter of taste and context.
This also doesn't "get rid" of the if like the title states, but that's not always a great goal to have. ifs aren't necessarily bad.

Mathematic equation solved recursively in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a task to make a code which will write 100 first numbers of an equation (or a function, I don't know what this is)
A(n) = (A(n-1))^2 -n*A(n-2) where A(1) = 1 and A(2) = 1
It has to be solved recursively. I have written this code so far
#include <iostream>
using namespace std;
int rekurzija(int n){
if(n=1){
return 1;
}
if(n=2){
return 1;
}
if(n>2){
return rekurzija(n-1)*rekurzija(n-1)-n*rekurzija(n-2);
}
}
int main(){
for(int n=1;n<101;n=n+1){
cout << rekurzija(n) << endl;
}
}
The problem is that the program returns 1 hundred times instead of 1,1,-2,0,...(instead of actually solving this function). What is wrong in this code?
You are using simple assignment operator = instead of Is equals to relational operator == in your rekurzija() function for if conditions
if(n = 1) //here `n = 1`is an assignment statement
{
//something...
}
What happens if you use = instead of ==?
The if condition will always evaluate to be true if the assigned value in the assignment statement is non-zero number.
Note: An assignment to zero evaluates to be false i.e, for if(n = 0), the if block will not be entered. You don't have any such if blocks in your code.
So your first if is always evaluated to be true because you are assigning a non-zero value i.e, 1 and thus your function always returns 1. that's the reason why you get 100 1's as your answer.
So, instead try changing all the if conditions to something like:
if(n == 1)
{
//something...
}
This would check if n is equals to 1 or not. If n is equal to 1 then the if block is entered, else it would not enter the if block and the next if condition is checked.
Note: Just remember this while using the = and == operators
= is for assignment
== is for comparison
When you compare things in C++ you need to do it like:
if (a == b)
and not
if (a = b)
The latter will assign b to a and return the value of a.

if condition for a number multiple [closed]

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 8 years ago.
Improve this question
I got an if condition as a multiples of 5, i need to check if condition until a value <= 10000. My if statement looks like this
// in main function
if(value >=0 && value <16){
function(number,value);
}
else if(value >=5 && value <10){
value-=16;
function(number,value);
}
....
// function
int function(int n, int value){
return (n<<value)|(n>>(16-value))
}
Is there a better way to do this if statement. I am new to programming world and a bit curious to know how to do this.
Thanks in advance
You could use function pointers.
typedef void (*func)();
func fpointers[] = {func1, func2, func3}
int check = value / 5;
fpointers [check] ();
Put the amounts you're subtracting from value into an array, indexed by the multiple of 5 for each range:
int subtract[] = [2, 5, ...];
if (value > 0 && value < 5*(sizeof subtract/sizeof(*subtract))) {
value -= subtract[value/5];
functioncall(value);
}
If you need to evaluate the condition for multiples of 5 , I suggest you to use swich case
int check = value/5;
switch(check)
{
case 0: // 0 <= value < 5
// do things
break;
case 1 : // 5 <= value < 10
// do things ...
break;
.............
default:
break;
}
I got an if condition as a multiples of 5
To check if a number is a multiple of 5, use the modulus operator:
if (number % 5 == 0) ...
i need to check if condition until a value <= 10000
This sounds like you need a loop.