my question is if the "if" statement is only executed 3 times does that mean the "for loop is only executed 3 times?
No.
You need to think about it logically. The if statement is inside the loop. Evaluating the if statement is therefore part of the loop. The loop runs 10 times, and the if statement is evaluated 10 times.
Just because some conditional code inside the loop isn't executed on every iteration of the loop, doesn't mean those iterations aren't occurring.
EDIT: This would be clearer if you enclosed your blocks in braces, even when they're single lines. It would also be clearer if you used code tags to format your post properly, and used sensible indentation:
1 2 3 4 5 6 7 8 9
n = 10; // <--- Note that I added the missing semicolon here
for (int i = 1; i <= n; i++)
{
if(i < 5 && i != 2)
{
cout << 'X';
}
}
Below is the for loop converted into a while loop, am i on the right track?
Yes - although your while loop will only iterate 9 times, not 10.
Also, you don't need to put the i++; in each conditional block. You can simply have it after the whole if statement has finished. Generally speaking, it's better not to duplicate code if you can consolidate it logically into one place.