Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: CONTROL STATEMENTS IN C LANGUAGE
Description: 1)if – else statement switch statement for statement while statement. 2)MULTIPLE BRANCHING 3)The SWITCH statement for multiple branching 4)Nesting of Loops

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


CONTROL   STATEMENTS

1
...

2
...

3
...


1
...

2
...

3
...

4
...

5
...


For all these example a logical test needs to be carried out at
some particular point within the program
...

This is known as branching
...
(prob 3)
In addition, the program may require that a group of instructions
be executed repeatedly, until some logical condition has been
satisfied
...
(prob 4 and 5)

if – else statement 
switch statement
for statement
while statement

Simple if statement

if ()
statement1;
statement2;
statement3;

A store gives discount of 5% on purchase of 100 items or more
...


scanf( “%d %f”,     &ITEM,         &PRICE);
If ( ITEMS > =100)
PRICE = 0
...
;

...

if ( marks > =50)
printf(“student has passed the exam\n”);
else
printf(“student has failed the exam\n”);

...
;

Suppose we wanted student getting  50 also to be shown as passing
...
;

...
;

...
;

...
;

...
;

...
;

...
;

...
 “);

Printing   floating point values
float  priceA, priceB,;
printf(“Please enter the prices of A and B:”);
scanf( “ %f  %f” ,   &priceA,   &priceB );
if ( priceA > priceB )
{printf( “\n  item A costlier“);
printf( “A costs Rs
...
   “, priceA“);
}
else 
printf( “\n  item A  NOT costlier
...
 
A company conducts two exams for selecting candidates for 
interview
...

int marksA, marksB;
printf(“Please enter the marks in paper A and paper B:”);
scanf( “ %d  %d” ,   &marksA,   &marksB );
if ( marksA > =60        &&   marksB >= 50)
printf( “\n  Qualified“);
else 
printf( “\n  Not qualified“);

Improper use of Boolean Operators
Example 2
...
 To get qualified, the candidate must get at least 50 in both 
paper A and  paper B
...
 
A company conducts two exams for selecting candidates for 
interview
...

int marksA, marksB;
printf(“Please enter the marks in paper A and paper B:”);
scanf( “ %d  %d” ,   &marksA,   &marksB );
if ( marksA

&&   marksB >= 50)

if ( marksA > =50        &&   marksB >= 50)
printf( “\n  Qualified“);
else 
printf( “\n  Not qualified“);

MULTIPLE   BRANCHING
Let us now look at an example where we need multiple 
branching
...

if ( marks >= 80)
printf ( “ Grade A \n”);
else
if ( marks >= 65  && marks < 80)
printf ( “ Grade B \n”);
else  
if ( marks >= 50 && marks < 65)
printf ( “ Grade C \n”);
else  
if ( marks < 50 )
printf ( “ Grade F \n”);

Simplified code !
if ( marks >= 80)
printf ( “ Grade A \n”);
else       /* Marks are now less than 80, so no need to check */ 
if ( marks >= 65)
printf ( “ Grade B \n”);
else  
if ( marks >= 50)
printf ( “ Grade C \n”);
else
printf ( “ Grade F \n”);

Exercise: 
Compute income tax for a person as per following 
table:
income < 1,00,000

: no tax

income between 1,00,000 and 
3,00,000

: 10% of income

income between 3,00,000 and 
8,00,000

: 20% of income

income above 8,00,000 

: 30% of income

The SWITCH  statement for 
multiple branching

selection is done based on exact  integers 
(not range)

Example 1 for switch
A company gives bonus to its employees depending on 
the category of employees as per following schedule:
Category 1: 20000
Category 2: 15000
Category 3: 10000
Category 4:   5000
We can write a switch statement to implement this
...
 The maximum marks for the test is 10
...

We want to print the grade based on following:
marks
10:
9: 
8 :
7:
6:
5: 
4: 
3: 
2: 
1: 
0:

grade
A
A
B
C
D
F
F
F
F
F
F

int marks;
scanf( “ %d “, &marks);
switch (marks)

{
case 10:
case 9:  printf( “ grade 
break;
case 8:  printf( “ grade 
break;
case 7:  printf( “ grade 
break;
case 6:  printf( “ grade 
break;
default:  printf( “ grade 

}

= ‘A’ “ );
= ‘B’  “);
= ‘C’ “);
= ‘D’  “);
= ‘F’  “);

Example 2B for switch
Suppose we have a small change in the grading process
marks
10:
9: 
8 :
7:
6:
5: 
4: 
3: 
2: 
1: 
0:

grade
A
B
B
C
D
F
F
F
F
F
F

int marks;
scanf( “ %d “, &marks);
switch (marks)

{
case 10: printf( “ grade 
break;
case 9: 
case 8:  printf( “ grade 
break;
case 7:  printf( “ grade 
break;
case 6:  printf( “ grade 
break;
default:  printf( “ grade 

}

= ‘A’ “);

= ‘B’ “);
= ‘C’ “);
= ‘D’ “);
= ‘F’ “);

REPITITION

The real power of computers is in their ability to 
repeat an operation or a series of operations 
many times
...
 
The looping can be done for a fixed number of 
times, 
for  loop
or dependent on satisfaction of some condition
while loop
...
;
stmt……;
Steps: 
Loop variable is initialized
...

If it is TRUE, statements in loop body are executed
...

Steps repeated till test condition fails
...

#include ...
+100= %d
...

Find the sum of all odd numbers between 1 and 100
...
h>
int main() {
int val, sum = 0;
for (val = 1;
val < 100; val = val+2)
{
sum = sum + val;
}
printf(“ 1+3+5+
...
h>
int main(void) {
int val , product;
for (val = 1; val < =10; val = val++)
{
product = 8 * val;
printf("8 * “%d “ = “%d\n”, val, product);
}
return 0;
}

Calculating Factorial of a number
6! = 1x 2x 3x 4x 5x 6

printf(“enter number\n");   
scanf(“n= %d", &n);       
answer = 1;       
for (index = 1;    index<=n;      index++) 
answer = answer*index; 
printf("%d! = %d\n", n, answer);

Example: Reverse order
Print all numbers between 1 and 100 in the reverse
order
...
h>
int main(void) {
int val ;
for (val = 100;
val < =1;
{
printf(“ %d “, val);
}
return 0;
}

val = val--)

Nesting of Loops
You can have another loop inside a loop
...


Say you want to print the pattern
* * * * * * * 
* * * * * * *
* * * * * * *
* * * * * * *

int rows = 4;
int numstars = 7;
for (row=1; row<=rows; row++) 
{
// Print out the right number of stars
...

printf("\n");
}

Second Pattern:
*
**
***
****
*****
******
Notice that when we print out ith line, 
we also want to print i stars on that line
...

………
m = 5;
for ( 
{

;  m !=100  ;    
printf(“ m = %d\n”,  m);
m = m+5;

}
………
...


)


Title: CONTROL STATEMENTS IN C LANGUAGE
Description: 1)if – else statement switch statement for statement while statement. 2)MULTIPLE BRANCHING 3)The SWITCH statement for multiple branching 4)Nesting of Loops