+ 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

22nd Nov 2019, 2:14 PM
Spiral Phoenix
Spiral Phoenix - avatar
5 ответов
+ 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.
22nd Nov 2019, 2:20 PM
VOID
VOID - avatar
+ 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.
22nd Nov 2019, 2:19 PM
Avinesh
Avinesh - avatar
+ 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 :(
22nd Nov 2019, 2:25 PM
Spiral Phoenix
Spiral Phoenix - avatar
+ 2
CodeCat_ thank you once again 😁
22nd Nov 2019, 2:48 PM
Spiral Phoenix
Spiral Phoenix - avatar
+ 1
Spiral Phoenix No problem 😊 feel free asking ur doubts
22nd Nov 2019, 2:47 PM
VOID
VOID - avatar