BOOLEAN EXPRESSION BUG

Write your question here.
Why doesn't the program enter the while loop in the foo function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 // buggy program
// 
//

#include <cstdio>
#include <iostream>
#include <cstdbool>
using namespace std;

void foo(int i)
{

	while (i <= 1)
	{
		i = i - 3;
	}
	printf("%i\n", i);
}

int main(void)
{
	printf("Enter an integer: ");
	int i;
	cin >> i;
	while (i > 10)
	{
		i--;
	}
	foo(i);
}
It depends what value is sent to foo for i from main.
Thank you for your response. I found the problem. The Boolean expression in the while statement should be i >= 1.
Topic archived. No new replies allowed.