Conditional or turnery operators:
?, : are two operators used to write the conditional
statements.
The conditional statements are used in place of if-else
control structure.
Conditional operators support two syntaxes
- Statement
- Expression
Sysntax1:
According to the above
syntax if the condition is true then the first statement is executed, otherwise
ststement2 is executed.
Specification1:
Accept any number and
print whether the number is an even or odd.
|
#include<stdio.h>
void main()
{
int n;
printf("Enter any
number:");
scanf("%d",&n);
(n%2==0)?printf("Even
number"):printf("Odd number");
}
Execution1:
Enter any number:25
Odd number
Execution2:
Enter any number:12
Even number
Specification2:
Accept any number and
print whether the number is a zero, positive or negative.
Program:
#include<stdio.h>
void main()
{
int n;
printf("Enter any number:");
scanf("%d",&n);
(n==0)?printf("Zero"):((n>0)?printf(" Positive "):printf("Negative"));
Incase of if-else
if(n==0)
printf("Zero);
else if(n>0)
printf("Positive");
else
printf("Negative");
}
Execution1:
Enter any number: 25
Positive
Execution2:
Enter any number: -14
Negative
Execution3:
Enter any number: 0
Zero
Syntax2:
According to the above
syntax if the condition is true then the value of expression1 is assigned to
the variable otherwise the value of expression2 is assigned to the variable.
Specification1:
Accept the price, quantity of a product and print the bill,
discount and the net bill. Allow 5% discount if bill exceeds to 500 otherwise
allow 3% discount.
|
#include<stdio.h>
void main()
{
float price,bill,dis,net_bill;
int qty;
printf("Price:");
scanf("%f",&price);
printf("Quantity:");
scanf("%d",&qty);
bill=price*qty;
dis=(bill>500)?bill*5/100:bill*3/100;
net_bill=bill-dis;
printf("Total bill %f",bill);
printf("\nDiscount %f",dis);
printf("\nNet bill %f",net_bill);
}
Execution1:
Price: 100
Quantity: 10
Total bill 1000.000000
Discount 50.000000
Net bill 950.000000
Points to remember:
1. In C language 0 is considered as false and any non-zero
is considered as true.
Example:
#include<stdio.h>
void main()
{
printf("One");
if(10>40)
printf("\nTwo");
if(80<20);
printf("\nThree");
printf("\nFour");
}
Output:
One
Three
Six
2. If the "if control structure" is terminated
with a semicolon then it can’t control the statement or block of statements
under the conditional statement.
Example:
pava
Output:
One
Three
Four
Example explained:
Though (80<20) is false, it couldn’t control printf("\nThree");
because conditional statement is terminated with the semicolon.
3. Assigning (=) is different from comparison (==).
Example:
<pre><code>
#include<stdio.h>
void main()
{
int x=0,y=7;
if(y==1)
printf("Hello");
if(x=5)
printf("World");
}
Output:
World
Example explained:
(y==1) gives false hence printf("Hello"); is not
executed.
x=5 assigns 5 to x and if(5) is true hence printf("World");
is executed.
4. Two nearby floating point constants can’t be compared
because exact value may not be assigned to the variable.
Example:
<pre><code>
#include<stdio.h>
void main()
{
float x=0.4,y=0.7;
if(x<0.4)
printf("Hello");
if(y<0.7)
printf("World");
}
</code></pre>
Output:
World
Example explained:
0.4 is stored accurately into "x" hence (x<0.4)
gives false and printf("Hello"); is not executed.
0.7 is stored approximately into "y" hence
(x<0.7) gives true and printf("World"); is executed.
0 comments:
Post a Comment