- 2
why do must use assignments (==) in this code?
every one can help me? #include <iostream> using namespace std; int main() { int n; cin >> n; int a=0; while(n>a){ cout<<n<<endl; if(n%5==0){ cout<<"Beep"<<endl; } n--; } return 0; }
7 Antworten
+ 5
== is actually an equality comparison operator which returns true when both LHS and RHS is equal.
In this perticular program, it is being used to check if the result of ( n%5 ) ,is equals to 0 or not ( indirectly checking if the number *n* is divisible by 5 or not )
Is it necessary ?
of course NO ( I leave the alternatives for you to explore )
----
edit : looks like you are confusing it with assignment operator
"=" is an assignment operator which assigns the value of RHS to LHS
+ 5
Yasin no
In programming equal to ( assignment operator ) is used to store some data or assign the data
For example :
String me = ig
It means ' me ' is variable and 'ig ' is a literal means 'ig' is store in 'me' variable
Whereas == is an equality comparison operator which returns true when both LHS and RHS is equal.
For example :
2 == 2
True because both values are equal
2 ==3
False because both values are not equal
+ 2
To check n%5 is equal to 0 ,if yes it will return true else return false
+ 2
Hello Yasin
== means equal
% modulus operator
n % 5 returns the remainder of n/5
It returns 0 if n is divisible by 5
You enter a number n, let's say 10:
It runs from 10 to 0 and 5 and 10 are divible by 5 so it print's two times "Beep".
+ 1
but we can also use = to equal 0! can't we?
0
thanks for answring
0
i really got it🙏