Arduino and LEDS

Whats wrong with my code?

int ledPin = 3;
int ledPin2 = 2;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
}

void loop(){
int num = 0;

if(num = 1)
{
digitalWrite(ledPin, HIGH);
}
else(digitalWrite(ledPin, LOW));

if(num = 2)
{
digitalWrite(ledPin2, HIGH);
}
else(digitalWrite(ledPin2, LOW));

delay(500);
}

Both of the leds turn on anyways.

= is an assignment operator. == is the equality operator.

if(num = 2) is satisfied if what is in the parentheses is true (non zero)
your code set num to 2. 2 is non zero so the if is satisfied

the equality test you need is == if(num ==2).

This bites I'd bet every new C or C++ programmer. Even we very experienced programmers slip every once in a while....

Make it a habit to do something like if (2 == num) instead - if you accidentally type if (2 = num), the compiler will throw an error at you.

There are no comments. There are missing {}s for the ifs, elseifs.

Even if you had used =='s, where does 'num' ever get set to something besides 0?

Was num supposed to be a flag, set to 1 & 2 when the digitalWrites occurred so each LED know to turn on or off?
I think you'd want 2 flags then, one for each LED.

if(num1 == 1) 
{
digitalWrite(ledPin, HIGH);
num1 = 0;
}
else
{
digitalWrite(ledPin, LOW);
num 1 = 1;
}

if(num2 == 1) 
{
digitalWrite(ledPin2, HIGH);
num2 = 0;
}
else
{
digitalWrite(ledPin2, LOW);
num2 = 1;
}

delay(500);
}  // end void loop

either that or combine this into just 1 if/else pair: turn LED1 on & LED2 off with num ==1, and LED1 off and LED2 on with num ==2.
if (num ==1){
LED1 on, LED2 off
num = 2}
else {
LED1 off, LED2 on
num = 1}
delay
repeat