"=="

Hello im trying to do a simple factorial loop here. and im wondering why does i==something not work as a proper condition? Or am i doing something wrong
Or am i doing something wrong

Yes.
Would you need to see the code? Or is the == the problem
== is for only conditional tests, like;

1
2
if (i == 5) cout << "you got it" << endl;
else cout << "yack!" << endl;


= is for assignments, like;

1
2
3
int i;
i = 35;
cout << "i is: " << i << endl;


most likely, what we do is that we forget to put the second = on an if statement or alike. we type;

 
if ( i = 5) cout << "done!" << endl;


which is not right.

so,
you should tell us if you're using == in a condition testing statement or an assigment.
in short; post a code.


Last edited on
Okay here is the code. i understand the difference between = and ==

1
2
3
4
5
6
7
	
for(int i = 4; i >= 1; i--)
	{

		factorial = factorial * i;
		cout<<factorial<<" ";
	}


So my problem is that while this above works... What i at first wanted to do was i == 1 for the condition and it doesnt work which makes no sense to me...


EDIT: Factorial in this case is equal to 5
Last edited on
You initialize i to 4, so i==1 would obviously be false and the loop is never entered.
What did you expect to happen?
Well wait, isnt the condition part of the loop basically mean do this loop untill this condition is met?
I'm assuming that you have initialized factorial to 1 at first place.
somewhere before the for loop, you should have;

 
int factorial = 1;


and, why you're counting down?
if you're trying to calculate 4!, then you should do;

1
2
3
4
5
6
7
int factorial = 1;

for (int i= 1;  i < 5; i++)
    factorial *= i;

cout << factorial << " " ;


is this what you need? looks like you need something like this;

1
2
3
4
5
6
7
8
9
int factorial = 1;
int f;
cout << "enter fact: ";
cin >> f;

for (int i= 1;  i < f+1; i++)
    factorial *= i;

cout << factorial << " " ;

Last edited on
Well wait, isnt the condition part of the loop basically mean do this loop untill this condition is met?

Nope, it's the other way round. The loop keeps running as long as the condition is true.
yes thats the same thing but factorials decrease so it just made sense in my mind to decrease. All i am trying to ask is regarding the for loop condition.
@Asthar:
hey, he put for(int i = 4; i >= 1; i--)
so the condition is "run as long as i is bigger than 1 or equal to 1"
and 4 meets this condition, 4 > 1, so it will enter the loop, right?
@Asthar - thanks Thats it. all makes sense now.. haha
Last edited on
so the condition is "run as long as i is bigger than 1 or equal to 1"
and 4 meets this condition, 4 > 1, so it will enter the loop, right?

Yes, but he was asking about why it didn't work with i==1 as the condition.
I don't get it, it's okay, but here is the working version of your code;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//factorial

#include <iostream>
using namespace std;

int main(){

	int factorial = 1;

	for(int i = 4; i >= 1; i--)
	{

		factorial = factorial * i;
		cout<<factorial<<" ";
	}

return 0;

}

/*
4 12 24 24 
*/


good luck.
factorial should be equal to 5
well, my math fails at this point :)

4! = 1 x 2 x 3 x 4 = 24

seriously, this is the definition of factorial right? I'll read all tomorrow to see if I'm just dreaming.
Last edited on
my bad, sorry guys.
4! is 24 ^^
Topic archived. No new replies allowed.