The IF Statement
The 'IF' Statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically a two way decision making statement and is used with an expression. It takes the following form :
i. Simple IF
General Form :
Sample Program : Find the absolute value of a given number.
ii. The IF .... ELSE
General Form :
Sample Program : Find the largest among two input numbers.
if (test condition)It allows the computer to evaluate the test condition first and then depending on whether the value of the expression is true of false it transfers the control to a particular statement.i. Simple IF
General Form :
if(test expression)
{
statement block;
}
statement x;If the test expression is true then statement block will be executed otherwise the statement block will be skipped and the execution will be jumped to the statement x. When the condition is true both the statement block and the statement x are executed in sequence.Sample Program : Find the absolute value of a given number.
1: #include<stdio.h> 2: #include<conio.h>3: void main()
4: {5: int num;
6: clrscr();7: printf("Enter a number : ");
8: scanf("%d",&num);
9: if(num<0)
10: num=num*-1;11: printf("Absolute Value : %d",num);
12: getch(); 13: }ii. The IF .... ELSE
General Form :
if(test condition) { statement block1; } else { statement block2; } statement x;If the test expression is true, the statement block 1 will be executed otherwise the statement block 2 will get executed. In both the situation statement x will be executed.
Sample Program : Find the largest among two input numbers.
1: #include<stdio.h> 2: #include<conio.h>3: void main()
4: {5: int a,b;
6: clrscr();7: printf("Enter two numbers : ");
8: scanf("%d%d",&a,&b);
9: if(a>b)
10: {11: printf("%d is largest",a);
12: }13: else
14: {15: printf("%d is largest",b);
16: } 17: getch(); 18: }


0 comments:
Post a Comment