C Program to Find Total of Even Integers

C – If else Statement with Examples

The if else statement is used to express decisions and the syntax as per below,

if (expression)
statement 1
else
statement 2

where the else part is optional. The expression is evaluated; if it is true {that is, if>expression has a non-zero value}, statement 1 is executed. If it is false {expression is zero} and if there is an else part, statement 2 is executed instead.

Since an if simply tests the numeric value of an expression, certain coding shortcuts are possible. The most obvious writing as below with easy example.

if (expression)

instead of

if (expression 1= 0)

Sometimes this is natural and clear; at other times it can be cryptic. Because the else part of an if-else is optional, there is an ambiguity when an else is omitted from a nested if sequence. This is resolved by associating the else with the closest previous else-less if. For example, in

if (n > 0)
       if (a > b)
        z = a;
else
          z = b;

the else goes with the inner if, as we have shown by indentation. If that isn’t what you want, braces must be used to force the proper association

If (n > 0) {
If (a > b)
Z =a;
}

Else
               Z = b;

The ambiguity is especially pernicious  in situations like this:

if (n >= 0)
for (i = 0; i < n; i++)
if (s [ i] > 0) {
printf( "... n);
return i;

else          /*wrong*/

printf("error -- n is neqative\n");

The indentation shows unequivocally what you want, but the compiler doesn’t get the message, and associates the else with the inner if. This kind of bug can be hard to find; it’s a good idea to use braces when there are nested ifs.

if else statement

Here an Example shows how to calculate gross salary for the firm,

/* Calculation of gross salary */

main( )
{

float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}

gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;

}

If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee’s salary is input through the keyboard write a program to find his gross salary.


Please write your comments if you find anything incorrect OR need to explain in detail, or you want to share more information about the topic discussed above.

Online Training Tutorials

  • SAP BODS Tutorial Beginners – Learn SAP SAP BODS OnlineSAP BODS Tutorial Beginners – Learn SAP SAP BODS OnlineThis SAP BODS tutorial is designed for beginners looking to adopt SAP BODS Cloud in their business or career.
  • Invoice VerificationWhat is Invoice Verification in the SAP System?The SAP system is made up of several components linked together so that different departments within a company can cooperate with one another. The Invoice Verification component is part of […]
  • Central User AdministrationHow to Setting Up Central User Administration in SAP?Step by Step Settings Up Central User Administration in SAP Here the procedure for Central User Administration Configuration in a Landscape: 1) Create Logical systems to all clients […]
  • What is Client Copy in SAP Step by StepA client copy does not "generate a new system". It is used to transfer information between clients within the same system or to another system at the same release and db level. Client […]
  • Search engine optimizationSEO Strategies for New Website : SEO ChecklistThese SEO Strategies assume that you have some basic knowledge of SEO- Search Engine Optimization. These basis SEO Strategies help beginners and professionals to learn SEO strategies to […]
  • release procedureHow to do Pricing Release Procedure in SD?Define Processing Status - SM30 -> V_T686E You are free to design your own processing status flow. e.g. from 01 -> 02 or from 01 -> 02 -> 03 To convert the old Pricing […]
  • Chart of DepreciationChart of Depreciation in SAP OverviewChart of Depreciation in SAP: The chart of depreciation is a list of depreciation areas arranged according to business and legal requirements. The chart of depreciation enables you to […]
  • blanket purchase orderWhat Is a Blanket Purchase Order?Blanket Purchase Order in SAP MM for Consumable Materials Blanket Purchase Order (Po's) are often appropriate for low value materials and are used to procure consumable materials and […]