1 of 11

C programming

COMPILED BY ARUP ROY CHOUDHURY, ASSOCIATE PROFESSOR, DEPARTMENT OF MATHEMATICS, MALDA COLLEGE, MALDA

2 of 11

C Program: if-else statement

Nested if-else statement:

Syntax: if(test condition)

{

statement block;

}

else

{

if(test condition)

{

statement block;

}

3 of 11

C Program

else

{

statement block;

}

}

Example: A student obtained marks in five subjects, each subject have maximum marks 100. Find out the total marks and percentage marks obtained by the students.

Also find the division he carries. Given that percentage>=60, 1st division; 50-59%, 2nd division; 40-49%, 3rd division and less than 40%, fail.

Solution: /*Percentage and division*/

4 of 11

C Program

  •  

5 of 11

C Program

printf(“\n First Division”);

else

{

if(per>=50)

printf(“\n Second Division”);

else

{

if(per>=40)

printf(“\n Third Division”);

6 of 11

C Program: switch statement

else

printf(“\n Fail”);

}

}

}

Switch Statement: Syntax of this statement as follows:

switch(integer expression)

{

case constant 1;

7 of 11

C Program

statement block 1;

case constant 2;

statement block 2;

…….

…….

default:

default statement block;

}

statement block;

The case constants are served as just level. First the integer expression is evaluated

8 of 11

C Program

and the value is matched one after another starting from the first. If the value is not matched, control will be transferred to the statement next. If matched, all statement blocks including default statement block will be executed. To avoid subsequent cases we have to use break statement which transfer the control to outside of switch .

switch(integer expression)

{

case constant 1;

statement block 1;

break;

case constant 2;

statement block 2;

9 of 11

C Program: goto statement

break;

………….

………..

default:

default statement block;

}

statement block;

The goto statement:

The goto statement is used to repeat or skip a block of statements unconditionally.

10 of 11

C Program

Syntax:

goto level;

statement block;

level:

statement block;

or,

level:

statement block;

goto level;

statement block;

11 of 11

C Program: Example

Example: float x, a, e;

scanf(“%f”, &a);

cal:

x=a – f(a)/g(a);

if(fabs(x – a)<e)goto final;

a = x;

goto cal;

final:

printf(“\n The root is = %f”, x);