C-Decision Making Statements | with Examples
- The if statement.
- The if-else statement.
- The if-else-if ladder statement.
- The switch-case statement.
Decision making statements check the given condition and then executes its sub block if the condition happens to be true. (A block is a set of statements enclosed within opening brace and closing brace{and}).
THE IF STATEMENT
The general syntax of if statement is as:
{
Statements;
Statements;
Statements;
………………..;
}
The if statement is used to execute/skip a block of statements on the basis of truth or falsity of a condition. The condition to be checked is put inside the parenthesis which is preceded by keyword if.
#include<stdio.h>
#include<conio.h>
main()
{
int x;
clrscr ();
x=10;
if (x>0)
printf(“x is greater than 0”);
getch();
}
Output:
x is greater than 0
Explanation: if is a conditional execution statement. If is a keyword. The condition to be checked is put inside parenthesis. If the condition is true the first statement after the if gets executed, else it is skipped.
THE IF-ELSE STATEMENT
In all the above programs we didn’t write the other side of if condition i.e. we didn’t take the action when the condition fails. The if-else construct allows us to do this.
Its general syntax is
{
statements;
statements;
statements;
………………
}
else
{
statements;
statements;
statements;
……………….;
}
If the condition within if true all the statements within the block following if are executed else they are skipped and else part get executed.
To check whether a number is +ve, -ve or zero */
#include<conio.h>
main()
{
int a;
clrscr ();
printf (“Enter any number\n”);
scanf(“%d”,&a);
if(a==0)
printf(“number is zero\n”);
if(a>0)
printf(“number is +ve\n”);
else
printf(“number is –ve\n”);
getch();
Output:
Enter any number
-34
number is –ve
There are few solutions to make the above program more efficient.
NESTING OF IF-ELSE’S
Nesting of if-else means one if-else or simple if as the statement part of another if-else or simple if statement. There may be various syntaxes of nesting of if-else.
If(condition)
{
If(condition)
{
Statements;
Statements;
Statements;
}
else
{
Statements;
Statements;
Statements;
}
}
2.
If(condition)
{
If(condition)
{
Statements;
Statements;
Statements;
}
else
{
Statements;
Statements;
Statements;
}
}
else
{
Statements;
Statements;
Statements;
}
Explanation: A year is leap year if it is completely divisible by 100 and 400 or not divisible by 100 but divisible by 4.
Else-if ladder
The general syntax of else-if ladder is
else-if(condition)
Statement;
else-if(condition)
Statement;
else-if(condition)
Statement;
Statements;
break;
Case choice2:
Statements;
break;
Case choice3:
statements;
break;
-------------------
-------------------
-------------------
default:
}
Choice1,choice2 and choice are the possible values which we are going to test
with the expressions. In case none of the values from choice1 to
choice matches with the value of expression the default case is executed.
Comments
Post a Comment