if-then Statement
if-then is most basic statement of Decision making statement. It tells to program to execute a certain part of code only if particular condition or test is true.
Syntax
if(condition) { .......... .......... }
Example
#include<stdio.h> #include<conio.h> void main() { int a=10; if(a<=10) { printf("a is less than 10"); } getch(); }
Output
a is less than 10
- Constructing the body of "if" statement is always optional, Create the body when we are having multiple statements.
- For a single statement, it is not required to specify the body.
- If the body is not specified, then automatically condition part will be terminated with next semicolon ( ; ).
else
It is a keyword, by using this keyword we can create a alternative block for "if" part. Using else is always optional i.e, it is recommended to use when we are having alternate block of condition.
In any program among if and else only one block will be executed. When if condition is false then else part will be executed, if part is executed then automatically else part will be ignored.
Syntax
if(condition) { .......... .......... } else { ........ ........ }
Example
#include<stdio.h> #include<conio.h> void main() { int age=40; if(age<18) { printf("you are child"); } else { printf("you are young"); } getch(); }
Output
you are young
No comments:
Post a Comment