C Program Learning Part-5 - Engineer Simple

Search This Blog

C Program Learning Part-5

 

Decision Making and Branching

C Program Exercise Part-01

language possesses the following statements:

* statement

*switch statement

*Conditional statement

*goto statement

Decision Making With IF Statement:

It takes the following form:

if(test expression)

SIMPLE IF STATEMENT

The general form of a simple if statement is:

if(test expression)




{

statement block;

}

statement-x;

main()

{

int a, b, c, d;

float ratio;

printf(“Enter four integer number.\n”);

scanf(“%d %d %d %d”, &a, &b, &c, &d);

if((c-d)!=0)

{

ratio= (float)(a+b)/ (float)(c-d);

printf(“Ratio = %f”, ratio);

}

}

A Program by using previous Format:

main()

{

int count, i;

float weight, height;

count = 0;

printf(“Enter weight and height for 10 boys. \n”);

for(i = 1; i <= 10; i++)

{

scanf(“%f %f”, &weight, &height);

if( weight < 50 && height > 170)

{

count = count + 1;

}

}

printf(“Number of boys with weight < 50 kg \n”);

printf(“and height > 170 cm = %d”,count);

}

THE IF………….. ELSE STATEMENT


The general form is

if(test expression)

{

true block statement(s):

}

else

{

false block statement(s):

}

statement-x

if((c-d)!=0)

{

ratio= (float)(a+b)/ (float)(c-d);

printf(“Ratio = %f”, ratio);

}

else

printf(“c-d is zero”);




NESTING OF IF………..ELSE STATEMENTS

if(test condition -1)

{

if(test condition -2)

{

statement -1;

{

else

}

statement -2;

}

}

else

{

statement -3;

}

statements -x;





On Application Format:

if(sex is female)

{

if(balance > 5000)

{

bonus = 0.05 * balance;

}

else

{

bonus = 0.02 * balance;

}

}

else

{

bonus = 0.02 * balance;

}

balance = balance + bonus;

 

 

Format:

main()

{

float a, b, c;

printf(“Enter three values: \n”);

scanf(“%f %f %f”, &a, &b, &c);

printf(“The largest value is: ”);

if(a > b)

{

if(a > c)

{

printf(“%f”,a);

}

else

printf(“%f”,c);

}

else

{

if(c > b)

printf(“%d”,c);

else printf(“%d”,b);

}

}


Dangling Else Problem

This occurs when a matching else is not available for an if. To avoid this always match an else to the most recent unmatched if in the current block.

Have logic error:

x= 10;

y= 2;

if( x > 5)

if( y > 5)

printf(“ x and y are > 5”);

else

printf(“x is <= 5.”);

Correctness:

x= 10;

y= 2;

if( x > 5)

{

if( y > 5)

printf(“ x and y are > 5”);

}

else

printf(“x is <= 5.”);

 Polli Bidut Somiti Charges its domestic consumers as follows:

Consumption Units

Rate of Charge

0-200

Tk 0.05 per unit

201-400

Tk 100 per unit

401-600

Tk 230 per unit

601 and above

Tk 390 per unit

Now write a program that reads the customer number and power consumed and prints the amount to be paid by the customer.

Solution:

main()

{

int units, customer;

float charges;

printf(“Enter Customer NO. and UNITS Consumed\n”);

scanf(“%d %d”, &customer, &units);

if(units<=200)

charges = 0.5 * units; else if(units <= 400)

{

charges = 100 + 0.65 * (units - 200);

}

else if(units <= 600)

{

charges = 230 + 0.8 * (units - 400);

}

else

{

charges = 390 + 1.0 * (units - 400);

}

printf(“Customer number = %d and Charges = %.2f”,customer, units);

}



Next Post Previous Post
No Comment
Add Comment
comment url