how it is logical to get this to work?

#include <iostream>
using namespace std;

int main()
{
	int a;
	if(a = 0)
	{
		cout<< "oldu" << endl;
	}
	else
	{
		cout << "olmadi" << endl;
	}
	return 0;
}

whereas in c# you can not do something like below :

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            if (a = 1)
            {
                Console.WriteLine("oldu");
            }
            else
            {
                Console.WriteLine("olmadi");
            }
        }
    }
}

Recommended Answers

All 5 Replies

in your if statements you are assign the value of 0 and 1 to your variable a, thus it will always evaluate true. I'm guessing you should be using ==. Also since you have an unanitialized variable a, using the == would just be dangerous....

Chris

>how it is logical to get this to work?
C++ takes an expression and evaluates it in a boolean context, but the expression itself doesn't have to evaluate to the bool type. a = 0 evaluates to the value of a, which is 0, which when treated in a boolean context is false. The condition of the if statement is thus false, and the else clause is executed. If you set a to anything other than 0, the opposite will happen because non-zero in a boolean context is true.

>whereas in c#
C++ is not C#. You'd do well to realize that making comparisons between two completely different languages serves little purpose.

>you can not do something like below
Because C# requires the expression to have a boolean type.

in your if statements you are assign the value of 0 and 1 to your variable a, thus it will always evaluate true. I'm guessing you should be using ==. Also since you have an unanitialized variable a, using the == would just be dangerous....

Chris

that is what i thought in the first place. But although = is used, the expression operates correctly.

>how it is logical to get this to work?
C++ takes an expression and evaluates it in a boolean context, but the expression itself doesn't have to evaluate to the bool type. a = 0 evaluates to the value of a, which is 0, which when treated in a boolean context is false. The condition of the if statement is thus false, and the else clause is executed. If you set a to anything other than 0, the opposite will happen because non-zero in a boolean context is true.

>whereas in c#
C++ is not C#. You'd do well to realize that making comparisons between two completely different languages serves little purpose.

>you can not do something like below
Because C# requires the expression to have a boolean type.

It is bad of c++ to evalute = in boolean context.

>It is bad of c++ to evalute = in boolean context.
Some people believe that. It certainly causes a common bug where the programmer types = instead of ==. That's the primary rationale behind Java and C#'s decision to require a boolean type for a boolean condition.

Then again, some people feel that the notational convenience and flexibility is worth the risk. It's not uncommon to see such tests as the following:

if ( T *p = get_something() ) {
  // Work with p
}
else {
  // Process null pointer error
}
char *p = dst;
char *q = src;

/* Copy the source string to the destination */
while ( *p++ = *q++ )
  ;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.