It is the first
Syntax:
if(condition) Statement;
where a statement can be a single statement or a compound statement(sequence of statement enclosed in a pair of ‘{}’)or nothing.
Remember, not to put a semicolon after any if, else if or else statement. Always write if, else in lowercase, since they are keywords
. For example:-
int main() { int a,b; cin>>a>>b; if(a>b) cout<<â€Greatest number isâ€<<a; return 0; }
OUTPUT
6 4 Greatest number is 6
if-else STATEMENT
Syntax:-
if(condition) Statement 1; else Statement 2;
If the expression evaluates to be true, statement 1 is executed, otherwise, statement 2 is executed.

Let’s take same example but with if-else now.
int main() { int a,b; cin>>a>>b; if(a>b) cout<<â€Greatest number isâ€<<a; else Cout<<â€Greatest number isâ€<<b; return 0; }
The difference between two program is in the first one we can only check whether a is bigger than b or not, but in the second one we can find the greatest of 2 no’s and can print it.
OUTPUT
2 4 Greatest number is 4
if-else-if STATEMENT
Syntax:-
if(condition1) Statement 1; else if(condition2) Statement 2; : : else Statement n;
The expressions are evaluated from the top downwards.As soon as an expression evaluates to true, its associated statement will be executed ,otherwise ladder is bypassed.
If none of the expressions are true
SHORTHAND if
Ternary/conditional operator(? 🙂 can work as an alternative to if-else.
Syntax:-
Condition?statement1:statement2;
It will check the condition if it is true statement 1 will be executed else statement 2 gets executed.
Again from the example given above
int main() { int a,b,c; cin>>a>>b; c=(a>b)?a:b; cout<<â€Greatest number isâ€<<c; return 0; }
If you liked this article please follow me on @priya123, for more latest article and status by me..
You must be logged in to post a comment.