0% found this document useful (0 votes)
5 views

C Notes

1st year 2nd sem C programming full notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Notes

1st year 2nd sem C programming full notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Brief Article

The Author

March 21, 2023

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 for loop


The syntax of the for loop is:
1 for ( i ni ti a li z at io n St at e me n t ; testExpression ; updateStatement )
2 {
3 // statements inside the body of loop
4 }

Example 1: for loop


1 // Print numbers from 1 to 10
2 # include < stdio .h >
3
4 int main () {
5 int i ;
6
7 for ( i = 1; i < 11; ++ i )
8 {
9 printf ( " % d " , i ) ;
10 }
11 return 0;
12 }
Listing 1: Example 1 of for loop

1 1 2 3 4 5 6 7 8 9 10

Example 2: for loop

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

1 Enter a positive integer : 10


2 Sum = 55

1.2 while loop


The syntax of the while loop is:
1 while ( condition )
2 {
3 statement ( s ) ;
4 }

Example 1: while loop


1 # include < stdio .h >
2
3 int main () {
4
5 /* local variable definition */
6 int a = 10;
7
8 /* while loop execution */
9 while ( a < 20 ) {
10 printf ( " value of a : % d \ n " , a ) ;
11 a ++;
12 }
13
14 return 0;
15 }
Listing 3: Example of while 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

1.3 do-while loop


The syntax of the do-while loop is:
1 do
2 {
3 statement ( s ) ;
4 }
5 while ( condition ) ;

Example 1: do-while loop


1 # include < stdio .h >
2 int main () {
3 double number , sum = 0;
4
5 // the body of the loop is executed at least once
6 do {
7 printf ( " Enter a number : " ) ;
8 scanf ( " % lf " , & number ) ;
9 sum += number ;
10 }
11 while ( number != 0.0) ;
12
13 printf ( " Sum = %.2 lf " , sum ) ;
14
15 return 0;
16 }
Listing 4: Example of do-while loop

1 Enter a number : 1.5


2 Enter a number : 2.4
3 Enter a number : -3.4
4 Enter a number : 4.2
5 Enter a number : 0
6 Sum = 4.70

1.4 nested loop


The syntax of the nested loop is:

3
1 Outer_loop
2 {
3 Inner_loop
4 {
5 // inner loop statements .
6 }
7 // outer loop statements .
8 }

1.4.1 nested for loop


The syntax of the nested for loop is:
1 for ( initialization ; condition ; update )
2 {
3 for ( initialization ; condition ; update )
4 {
5 // inner loop statements .
6 }
7 // outer loop statements .
8 }

Example 1: nested for loop


1 # include < stdio .h >
2
3 int main () {
4
5 /* local variable definition */
6 int i , j ;
7
8 for ( i = 2; i <100; i ++) {
9
10 for ( j = 2; j <= ( i / j ) ; j ++)
11 if (!( i % j ) ) break ; // if factor found , not prime
12 if ( j > ( i / j ) ) printf ( " % d is prime \ n " , i ) ;
13 }
14
15 return 0;
16 }
Listing 5: Example of nested loop

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

1.4.2 nested while loop


The syntax of the nested while loop is:
1 while ( condition )
2 {
3 while ( condition )
4 {
5 // inner loop statements .
6 }
7 // outer loop statements .
8 }

Example 1: nested while loop


1 # include < stdio .h >
2 int main ()
3 {
4 int rows ; // variable declaration
5 int columns ; // variable declaration
6 int k =1; // variable initialization
7 printf ( " Enter the number of rows : " ) ; // input the number of rows .
8 scanf ( " % d " ,& rows ) ;
9 printf ( " \ nEnter the number of columns : " ) ; // input the number of
columns .
10 scanf ( " % d " ,& columns ) ;
11 int a [ rows ][ columns ]; // 2 d array declaration
12 int i =1;
13 while (i <= rows ) // outer loop
14 {
15 int j =1;
16 while (j <= columns ) // inner loop
17 {
18 printf ( " % d \ t " ,k ) ; // printing the value of k .
19 k ++; // increment counter
20 j ++;
21 }
22 i ++;
23 printf ( " \ n " ) ;

5
24 }
25 }
Listing 6: Example of nested loop

1 Enter the number of rows : 4


2
3 Enter the number of columns : 3
4 1 2 3
5 4 5 6
6 7 8 9
7 10 11 12

1.4.3 nested do-while loop


The syntax of the nested do-while loop is:
1 do
2 {
3 do
4 {
5 // inner loop statements .
6 } while ( condition ) ;
7 // outer loop statements .
8 }
9 while ( condition ) ;

Example 1: nested do-while loop


1 # include < stdio .h >
2 int main ()
3 {
4 /* printing the pattern
5 ********
6 ********
7 ********
8 ******** */
9 int i =1;
10 do // outer loop
11 {
12 int j =1;
13 do // inner loop
14 {
15 printf ( " * " ) ;
16 j ++;
17 } while (j <=8) ;
18 printf ( " \ n " ) ;
19 i ++;
20 } while (i <=4) ;
21 }
Listing 7: Example of nested loop

6
1 ********
2 ********
3 ********
4 ********

2 Conditional Branching Statements in C


These statements help to jump from one part of the program to another depending on
whether a particular condition is satisfied or not. These decision control statements
include:
ˆ if statement

ˆ if else statement

ˆ nested 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

2.2 if-else statement


The syntax of the if-else statement is:
1 if ( test expression )
2 {
3 True - block statements ( s ) ;
4 }
5 else
6 {
7 False - block statement ( s ) ;
8 }
9 statement x ;

Example 1: if-else 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 else
14 printf ( " No . is odd " ) ;
15 getch () ;
16 }
Listing 9: Example of if-else statement

1 Enter a number :25


2 No . is odd

2.3 nested if-else statement


The syntax of the nested if-else statement is:
1 if ( test condition 1)
2 {
3 if ( test condition 2)
4 {
5 statement -1;
6 }

8
7 else
8 {
9 statement -2;
10 }
11 }
12 else
13 {
14 statement -3;
15 }
16 statement - x ;

Example 1: nested if-else statement


1 # include < stdio .h >
2 # include < conio .h >
3 void main ()
4 {
5 int n1 , n2 , n3 ;
6 printf ( " Enter 3 integer numbers : " ) ;
7 scanf ( " % d % d % d " ,& n1 ,& n2 ,& n3 ) ;
8 if ( n1 > n2 )
9 {
10 if ( n1 > n3 )
11 printf ( " First no . is greatest " ) ;
12 else
13 printf ( " Third no . is greatest " ) ;
14 }
15 else
16 printf ( " Second no . is greatest " ) ;
17 }
Listing 10: Example of nested if-else statement

1 Enter 3 integer numbers :55


2 99
3 77
4 Second no . is greatest

2.4 else-if ladder


The syntax of the else-if ladder is:
1 if ( condition 1)
2 statement -1;
3 else if ( condition 2)
4 statement -2;
5 else if ( condition 3)
6 statement -3;
7 else if ( condition n )
8 statement - n ;
9 else
10 default - statement ;
11 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

1 Enter the marks between 0 -100


2 80
3 The grade : B

2.5 switch case


The switch statement in C is a multi-way decision statement that is a simplified version of
an if-else block which allows us to execute multiple operations for the different possibles
values of a single variable called switch variable. The syntax of the switch case is:
1 switch ( expression )
2 {
3 case constant - expression :
4 statement ( s ) ;
5 break ; /* optional */
6 case constant - expression :
7 statement ( s ) ;
8 break ; /* optional */
9 /* you can have any number of case statements */
10 default : /* Optional */
11 statement ( s ) ;
12 }

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

1 Enter the grade :


2 A
3 Excellent !
4 Your grade is A

2.6 goto statement


The goto statement is used to transfer control to a specified label. However, the label
must reside in the same function and can appear only before one statement in the same
function. The syntax of the goto statement is:
1 goto label ;
2 ... .. ...
3 ... .. ...
4 label :
5 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

1 Enter the total nos .:3


2 1. Enter a number : 5.3
3 2. Enter a number : 6.3
4 3. Enter a number : 7.3
5 Sum = 18.90
6 Average = 6.30

12

You might also like