convert while loop to for loop

hey guys,
i am just practicing about how to convert a while loop to for loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[code]int i = 2;
while (i <= 9)
{
   if (i < 5 && i != 2)
   cout << "X";
i++;
} 
  
for loop:
int i=2;
for (int i=2; i<=9; i++)
{
   (i<5)&&(i!=2);
    cout<<"x"<<endl;
}[code][/

the output should b xx.
but i am getting
x
x
|
x
8 times
can someone please help me to correct the code.
thanks
Last edited on
Please put your code in <code> tags.
Remove the semicolon after 2nd line inside the for loop and there should be an if before that statement.
I assume that this is just a typo error.Be careful while writing your code.
Last edited on
Please use [ code ] [ / code ] tags.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
int i = 2;
while (i <= 9)
{
    if (i < 5 && i != 2)
        cout << "W";
    i++;
}

for (int j = 2; j <= 9; j++)
{
    if (i < 5 && i != 2)
        cout << "F";
}
...


Your output will be on several lines because you have a endl in your for loop.
Last edited on
thanks guys for helping me learn more
Topic archived. No new replies allowed.