C Notes
C Notes
The Author
1 Loop in C
In programming, a loop is used to repeat a code block until the specified condition is
met. In the C programming language, three types of loops are introduced. These are
for loop
while loop
do while loop
1 1 2 3 4 5 6 7 8 9 10
1
1 // Program to calculate the sum of first n natural numbers
2 // Positive integers 1 ,2 ,3... n are known as natural numbers
3
4 # include < stdio .h >
5 int main ()
6 {
7 int num , count , sum = 0;
8
9 printf ( " Enter a positive integer : " ) ;
10 scanf ( " % d " , & num ) ;
11
12 // for loop terminates when num is less than count
13 for ( count = 1; count <= num ; ++ count )
14 {
15 sum += count ;
16 }
17
18 printf ( " Sum = % d " , sum ) ;
19
20 return 0;
21 }
Listing 2: Example 2 of for loop
2
1 value of a: 10
2 value of a: 11
3 value of a: 12
4 value of a: 13
5 value of a: 14
6 value of a: 15
7 value of a: 16
8 value of a: 17
9 value of a: 18
10 value of a: 19
3
1 Outer_loop
2 {
3 Inner_loop
4 {
5 // inner loop statements .
6 }
7 // outer loop statements .
8 }
1 2 is prime
2 3 is prime
3 5 is prime
4 7 is prime
5 11 is prime
6 13 is prime
7 17 is prime
8 19 is prime
9 23 is prime
10 29 is prime
11 31 is prime
4
12 37 is prime
13 41 is prime
14 43 is prime
15 47 is prime
16 53 is prime
17 59 is prime
18 61 is prime
19 67 is prime
20 71 is prime
21 73 is prime
22 79 is prime
23 83 is prime
24 89 is prime
25 97 is prime
5
24 }
25 }
Listing 6: Example of nested loop
6
1 ********
2 ********
3 ********
4 ********
if else statement
else-if ladder
switch case
goto statement
2.1 if statement
The syntax of the if statement is:
1 if ( test expression )
2 {
3 statement block ( s ) ;
4 }
5 statement x ;
Example 1: if statement
1 # include < stdio .h >
2 # include < conio .h >
3 void main ()
4 {
5 int num ;
6 printf ( " Enter a number : " ) ;
7 scanf ( " % d " ,& num ) ;
8 if ( num %2==0)
9 {
10 num = num +2;
11 printf ( " No . is even " ) ;
12 }
13 printf ( " \ n % d " , num ) ;
14 getch () ;
15 }
Listing 8: Example of if statement
7
1 Enter a number : 44
2
3 No . is even
4 46
8
7 else
8 {
9 statement -2;
10 }
11 }
12 else
13 {
14 statement -3;
15 }
16 statement - x ;
9
Example 1: else-if ladder
1 # include < stdio .h >
2 # include < conio .h >
3 int main ()
4 {
5 int marks ;
6 printf ( " Enter the marks between 0 -100\ n " ) ;
7 scanf ( " % d " ,& marks ) ;
8 if ( marks >=90)
9 {
10 printf ( " The grade : A \ n " ) ;
11 }
12 else if ( marks >=70 && marks <90)
13 {
14 printf ( " The grade : B \ n " ) ;
15 }
16 else if ( marks >=50 && marks <70)
17 {
18 printf ( " The grade : C \ n " ) ;
19 }
20 else
21 {
22 printf ( " The grade : Failed \ n " ) ;
23 }
24 getch () ;
25 return (0) ;
26 }
Listing 11: Example of else-if ladder
10
Example 1: switch case statement
1 # include < stdio .h >
2 int main ()
3 {
4 char grade ;
5 printf ( " Enter the grade :\ n " ) ;
6 scanf ( " % c " ,& grade ) ;
7 switch ( grade )
8 {
9 case ’A ’ :
10 printf ( " Excellent !\ n " ) ;
11 break ;
12 case ’B ’ :
13 printf ( " Well done \ n " ) ;
14 case ’C ’ :
15 printf ( " Can do better \ n " ) ;
16 break ;
17 case ’D ’ :
18 printf ( " You passed \ n " ) ;
19 break ;
20 case ’F ’ :
21 printf ( " Failed \ n " ) ;
22 break ;
23 default :
24 printf ( " Invalid grade \ n " ) ;
25 }
26 printf ( " Your grade is % c \ n " , grade ) ;
27 return 0;
28 }
Listing 12: Example of switch case statement
Here, label is an identifier that specifies the place where the branch is to be made. The
label is placed immediately before the statement where the control has to be transferred.
Example 1: goto statement
11
1 # include < stdio .h >
2
3 void main ()
4 {
5
6 int maxInput ;
7 int i ;
8 printf ( " Enter the total nos .: " ) ;
9 scanf ( " % d " ,& maxInput ) ;
10 double number , average , sum = 0.0;
11
12 for ( i = 1; i <= maxInput ; ++ i )
13 {
14 printf ( " % d . Enter a number : " , i ) ;
15 scanf ( " % lf " , & number ) ;
16 // go to jump if the user enters a negative number
17 if ( number < 0.0)
18 {
19 goto jump ;
20 }
21 sum += number ;
22 }
23 jump :
24 average = sum / ( i - 1) ;
25 printf ( " Sum = %.2 f \ n " , sum ) ;
26 printf ( " Average = %.2 f " , average ) ;
27 }
Listing 13: Example of goto statement
12