Tuesday, 15 April 2014

The Else-If Statement in C

Else-If Statement

Syntax
if (condition)
statement;
else
if (condition)
statement;

Program
void main ( )
{
float num1=1.0, num2=1.0; //here two float variables num1 and num2 wiht 0.0 values.
char op; //this statement creates a character variable op whose values can be entered in the program later.
while ( ! (num1== 0.0 && num2==0.0) ) // this program ends when we enter 0 value for num1 and num2
{  //start or while loop body
Printf("Type number1, operator, number2\n");  // asking for num1, op and num2 values.
Scanf( "%f %c %f",&num1, &op, &num2); //gets num1, op and num2 values from user at run time.
if (op== '+') //checks if the value entered for operator is +
Printf("= %f, num1+num2); if the above mention condition matches it returns the value of num1+num2
esle // otherwise
if (op== '-') // checks if the value entered for operator is -
Printf("= %f, num1-num2); //if the above mention condition matches it returns the value of num1-num2
esle //otherwise
if (op== '*') // checks if the value entered for operator is -
Printf("= %f, num1*num2); //if the above mention condition matches it returns the value of num1*num2
esle //otherwise
if (op== '/')  // checks if the value entered for operator is /
Printf("= %f, num1/num2); //if the above mention condition matches it returns the value of num1*num2
Printf("\n"); // prints a new line
}
}

Output
Type number1, operator, number2
10 + 20 = 30










No comments:

Post a Comment