+ 2
If loop without any statement
I found this modular exponentiation algorithm on the internet, and i confused. Why the if loop dont have any statement? And when i erase the if loop , the output gone wrong, can anyone tell me? Thank you This is c++ program https://www.tutorialspoint.com/cplusplus-program-to-implement-modular-exponentiation-algorithm
5 Réponses
+ 3
if isn't a loop! It's a condition which means, it will execute the code if the condition is true.
like,
int x=1;
If (x==1){
cout<<"hello";
}
else{
cout<<"Bye";
}
//the output of this code will be "hello" because the condition is true.
In the code, we will find that if has a condition which is,
if (exp % 2 == 1)
and also statement, which is,
res= (res * base) % mod;
So according to that, the code executes.
+ 3
The first line below the if condition belongs to the if statement. When you have a single statement to execute for if then you do not need curly braces { }.
Also you can make this out through indentation after if condition.
+ 2
Thank you guys , i didnt realize about the single statment doesnt need a curly braces, and sorry if i said if was a loop , i still new in programming :(
+ 2
CodeCat_ thank you once again 😁
+ 1
Spiral Phoenix No problem 😊 feel free asking ur doubts