I created two programs (Number 1 and Number 2) using simple if statements. As you know there are two way to create program using simple if statements, syntax are:
Code:
A. if (condition)
Statement;
B. if(condition)
{
Block of statements;
}
My both programs are same except i used set brackets in number 2 and I didn't use set brackets in number 1 but both codes are working fine. Is it not necessary to use set brackets when we write "block of statements" ?
I also want to know a live example of "Block of Statements" I know that when we use more than one statement within set brackets that is called Block of Statements.
Code:
if(gross_salary>=10000)
{
tax = gross_salary*0.15;
net_salary = gross_salary - tax;
}
is above one has example of "Block of statements" ?
Below are my both programs
Code:
//Number 1
/* Program to calculate the net Salary of an employee */
#include<stdio.h>
int main()
{
float tax, gross_salary, net_salary;
printf(" Enter Gross Salary of Employee: ");
scanf("%f", &gross_salary);
printf(" Employee Gross Salary %9.2f\n", gross_salary);
if(gross_salary<10000)
net_salary=gross_salary;
if(gross_salary>=10000)
tax = gross_salary*0.15;
net_salary = gross_salary - tax;
printf(" Tax: %26.2f", tax);
printf("\n Net Salary Rs. %16.2f\n", net_salary);
}
Code:
// Number 2
/* Program to calculate the net Salary of an employee */
#include<stdio.h>
int main()
{
float tax, gross_salary, net_salary;
printf(" Enter Gross Salary of Employee: ");
scanf("%f", &gross_salary);
printf(" Employee Gross Salary %9.2f\n", gross_salary);
if(gross_salary<10000)
net_salary=gross_salary;
if(gross_salary>=10000)
{
tax = gross_salary*0.15;
net_salary = gross_salary - tax;
}
printf(" Tax: %26.2f", tax);
printf("\n Net Salary Rs. %16.2f\n", net_salary);
}