Tuesday 22 March 2016

C Programming Control Statement

C if else Statement

The if statement in C language is used to perform operation on the basis of condition. By using if-else statement, you can perform operation either condition is true or false.
There are many ways to use if statement in C language:
  • If statement
  • If-else statement
  • If else-if ladder
  • Nested if

If Statement

The single if statement in C language is used to execute the code if condition is true. The syntax of if statement is given below:
  1. if(expression){  
  2. //code to be executed  
  3. }  

Flowchart of if statement in C

if statement in c
Let's see a simple example of c language if statement.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. if(number%2==0){  
  11. printf("%d is even number",number);  
  12. }  
  13.   
  14. getch();  
  15. }  

Output

enter a number:4
4 is even number
enter a number:5

If-else Statement

The if-else statement in C language is used to execute the code if condition is true or false. The syntax of if-else statement is given below:
  1. if(expression){  
  2. //code to be executed if condition is true  
  3. }else{  
  4. //code to be executed if condition is false  
  5. }  

Flowchart of if-else statement in C

if-else statement in c
Let's see the simple example of even and odd number using if-else statement in C language.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. if(number%2==0){  
  11. printf("%d is even number",number);  
  12. }  
  13. else{  
  14. printf("%d is odd number",number);  
  15. }  
  16. getch();  
  17. }  

Output

enter a number:4
4 is even number
enter a number:5
5 is odd number

If else-if ladder Statement

The if else-if statement is used to execute one code from multiple conditions. The syntax of if else-if statement is given below:
  1. if(condition1){  
  2. //code to be executed if condition1 is true  
  3. }else if(condition2){  
  4. //code to be executed if condition2 is true  
  5. }  
  6. else if(condition3){  
  7. //code to be executed if condition3 is true  
  8. }  
  9. ...  
  10. else{  
  11. //code to be executed if all the conditions are false  
  12. }  

Flowchart of else-if ladder statement in C

if-else-if ladder statement in c
The example of if-else-if statement in C language is given below.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. if(number==10){  
  11. printf("number is equals to 10");  
  12. }  
  13. else if(number==50){  
  14. printf("number is equal to 50");  
  15. }  
  16. else if(number==100){  
  17. printf("number is equal to 100");  
  18. }  
  19. else{  
  20. printf("number is not equal to 10, 50 or 100");  
  21. }  
  22. getch();  
  23. }  

Output

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

C Switch Statement

The switch statement in C language is used to execute the code from multiple conditions. It is like if else-if ladder statement.
The syntax of switch statement in c language is given below:
  1. switch(expression){    
  2. case value1:    
  3.  //code to be executed;    
  4.  break;  //optional  
  5. case value2:    
  6.  //code to be executed;    
  7.  break;  //optional  
  8. ......    
  9.     
  10. default:     
  11.  code to be executed if all cases are not matched;    
  12. }    

Rules for switch statement in C language

1) The switch expression must be of integer or character type.
2) The case value must be integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break statement found in switch case, all the cases will be executed after matching the case value. It is known as fall through state of C switch statement.
Let's try to understand it by the examples. We are assuming there are following variables.
  1. int x,y,z;  
  2. char a,b;  
  3. float f;  
Valid SwitchInvalid SwitchValid CaseInvalid Case
switch(x)switch(f)case 3;case 2.5;
switch(x>y)switch(x+2.5)case 'a';case x;
switch(a+b-2)case 1+2;case x+2;
switch(func(x,y))case 'x'>'y';case 1,2,3;

Flowchart of switch statement in C

flow of switch statement in c
Let's see a simple example of c language switch statement.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. switch(number){  
  11. case 10:  
  12. printf("number is equals to 10");  
  13. break;  
  14. case 50:  
  15. printf("number is equal to 50");  
  16. break;  
  17. case 100:  
  18. printf("number is equal to 100");  
  19. break;  
  20. default:  
  21. printf("number is not equal to 10, 50 or 100");  
  22. }  
  23. getch();  
  24. }  

Output

enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

C Switch statement is fall-through

In C language, switch statement is fall through, it means if you don't use break statement in switch case, all the case after matching case will be executed.
Let's try to understand the fall through state of switch statement by the example given below.
  1. #include<stdio.h>  
  2. #include<conio.h>  
  3. void main(){  
  4. int number=0;  
  5. clrscr();  
  6.   
  7. printf("enter a number:");  
  8. scanf("%d",&number);  
  9.   
  10. switch(number){  
  11. case 10:  
  12. printf("number is equals to 10\n");  
  13. case 50:  
  14. printf("number is equal to 50\n");  
  15. case 100:  
  16. printf("number is equal to 100\n");  
  17. default:  
  18. printf("number is not equal to 10, 50 or 100");  
  19. }  
  20. getch();  
  21. }  

Output

enter a number:10
number is equals to 10
number is equals to 50
number is equals to 100
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
number is equals to 100
number is not equal to 10, 50 or 100

C Loops

The loops in C language are used to execute a block of code or a part of the program several times.
In other words, it iterates a code or group of code many times.

Why use loops in C language?

Suppose that you have to print table of 2, then you need to write 10 lines of code.
By using the loop statement, you can do it by 2 or 3 lines of code only.

Advantage of loops in C

1) It saves code.
2) It helps to traverse the elements of array (which is covered in next pages).

Types of C Loops

There are three types of loops in C language that is given below:
  1. do while
  2. while
  3. for

do-while loop in C

It iterates the code until condition is false. Here, condition is given after the code. So at least once, code is executed whether condition is true or false.
It is better if you have to execute the code at least once.
The syntax of do-while loop in c language is given below:
  1. do{  
  2. //code to be executed  
  3. }while(condition);  
Flowchart and Example of do-while loop

while loop in C

Like do while, it iterates the code until condition is false. Here, condition is given before the code. So code may be executed 0 or more times.
It is better if number of iteration is not known by the user.
The syntax of while loop in c language is given below:
  1. while(condition){  
  2. //code to be executed  
  3. }  
Flowchart and Example of while loop

for loop in C

Like while, it iterates the code until condition is false. Here, initialization, condition and increment/decrement is given before the code. So code may be executed 0 or more times.
It is good if number of iteration is known by the user.
The syntax of for loop in c language is given below:
  1. for(initialization;condition;incr/decr){  
  2. //code to be executed  
  3. }  
Flowchart and Example of for loop

do while loop in C

To execute a part of program or code several times, we can use do-while loop of C language. The code given between the do and while block will be executed until condition is true.
In do while loop, statement is given before the condition, so statement or code will be executed at lease one time. In other words, we can say it is executed 1 or more times.
It is better if you have to execute the code at least once.

do while loop syntax

The syntax of C language do-while loop is given below:
  1. do{  
  2. //code to be executed  
  3. }while(condition);  

Flowchart of do while loop

flowchart of do while loop in c language

do while example

There is given the simple program of c language do while loop where we are printing the table of 1.
  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1;  
  5. clrscr();    
  6.   
  7. do{  
  8. printf("%d \n",i);  
  9. i++;  
  10. }while(i<=10);  
  11.     
  12. getch();    
  13. }    

Output

1
2
3
4
5
6
7
8
9
10

Program to print table for the given number using do while loop

  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1,number=0;  
  5. clrscr();    
  6.   
  7. printf("Enter a number: ");  
  8. scanf("%d",&number);  
  9.   
  10. do{  
  11. printf("%d \n",(number*i));  
  12. i++;  
  13. }while(i<=10);  
  14.     
  15. getch();    
  16. }    

Output

Enter a number: 5
5
10
15
20
25
30
35
40
45
50
Enter a number: 10
10
20
30
40
50
60
70
80
90
100

Infinitive do while loop

If you pass 1 as a expression in do while loop, it will run infinite number of times.
  1. do{  
  2. //statement  
  3. }while(1);  

while loop in C

The while loop in C language is used to iterate the part of program or statements many times.
In while loop, condition is given before the statement. So it is different from the do while loop. It can execute the statements 0 or more times.

When use while loop in C

The C language while loop should be used if number of iteration is uncertain or unknown.

Syntax of while loop in C language

The syntax of while loop in c language is given below:
  1. while(condition){  
  2. //code to be executed  
  3. }  

Flowchart of while loop in C

flowchart of c while loop

Example of while loop in C language

Let's see the simple program of while loop that prints table of 1.
  1. #include <stdio.h>      
  2. #include <conio.h>      
  3. void main(){      
  4. int i=1;    
  5. clrscr();      
  6.     
  7. while(i<=10){    
  8. printf("%d \n",i);    
  9. i++;    
  10. }   
  11.       
  12. getch();      
  13. }      

Output

1
2
3
4
5
6
7
8
9
10

Program to print table for the given number using while loop in C

  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1,number=0;  
  5. clrscr();    
  6.   
  7. printf("Enter a number: ");  
  8. scanf("%d",&number);  
  9.   
  10. while(i<=10){  
  11. printf("%d \n",(number*i));  
  12. i++;  
  13. }  
  14.   
  15. getch();    
  16. }    

Output

Enter a number: 50
50
100
150
200
250
300
350
400
450
500
Enter a number: 100
100
200
300
400
500
600
700
800
900
1000

Infinitive while loop in C

If you pass 1 as a expression in while loop, it will run infinite number of times.
  1. while(1){  
  2. //statement  
  3. }  

for loop in C

The for loop in C language is also used to iterate the statement or a part of the program several times, like while and do-while loop.
But, we can initialize and increment or decrement the variable also at the time of checking the condition in for loop.
Unlike do while loop, the condition or expression in for loop is given before the statement, so it may execute the statement 0 or more times.

When use for loop in C

For loop is better if number of iteration is known by the programmer.

Syntax of for loop in C

The syntax of for loop in c language is given below:
  1. for(initialization;condition;incr/decr){  
  2. //code to be executed  
  3. }  

Flowchart of for loop in C

for loop in c language flowchart

Example of for loop in C language

Let's see the simple program of for loop that prints table of 1.
  1. #include <stdio.h>      
  2. #include <conio.h>      
  3. void main(){      
  4. int i=0;    
  5. clrscr();      
  6.     
  7. for(i=1;i<=10;i++){    
  8. printf("%d \n",i);    
  9. }   
  10.       
  11. getch();      
  12. }      

Output

1
2
3
4
5
6
7
8
9
10

C Program : Print table for the given number using C for loop

  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1,number=0;  
  5. clrscr();    
  6.   
  7. printf("Enter a number: ");  
  8. scanf("%d",&number);  
  9.   
  10. for(i=1;i<=10;i++){    
  11. printf("%d \n",(number*i));  
  12. }  
  13.   
  14. getch();    
  15. }    

Output

Enter a number: 2
2
4
6
8
10
12
14
16
18
20
Enter a number: 1000
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

Infinitive for loop in C

If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as infinitive for loop.
In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.
  1. for(;;){  
  2. printf("infinitive for loop example by javatpoint");  
  3. }  
If you run this program, you will see above statement infinite times.

C break statement

The break statement in C language is used to break the execution of loop (while, do while and for) and switch case.
In case of inner loops, it terminates the control of inner loop only.
There can be two usage of C break keyword:
  1. With switch case
  2. With loop

Syntax:

  1. jump-statement;  
  2. break;  
The jump statement in c break syntax can be while loop, do while loop, for loop or switch case.

Flowchart of break in c

c language break statement flowchart

Example of C break statement with switch case

Click here to see the example of C break with switch statement.

Example of C break statement with loop

  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1;//initializing a local variable  
  5. clrscr();    
  6.   
  7. //starting a loop from 1 to 10  
  8. for(i=1;i<=10;i++){    
  9. printf("%d \n",i);  
  10. if(i==5){//if value of i is equal to 5, it will break the loop  
  11. break;  
  12. }  
  13. }//end of for loop  
  14.   
  15. getch();    
  16. }    

Output

1
2
3
4
5
As you can see on console output, loop from 1 to 10 is not printed after i==5.

C break statement with inner loop

In such case, it breaks only inner loop, but not outer loop.
  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1,j=1;//initializing a local variable  
  5. clrscr();    
  6.   
  7. for(i=1;i<=3;i++){    
  8. for(j=1;j<=3;j++){  
  9. printf("%d &d\n",i,j);  
  10. if(i==2 && j==2){  
  11. break;//will break loop of j only  
  12. }  
  13. }  
  14. }//end of for loop  
  15.   
  16. getch();    
  17. }    

Output

1 1
1 2
1 3
2 1
2 2
3 1
3 2
3 3
As you can see the output on console, 2 3 is not printed because there is break statement after printing i==2 and j==2. But 3 1, 3 2 and 3 3 is printed because break statement works for inner loop only.

C continue statement

The continue statement in C language is used to continue the execution of loop (while, do while and for). It is used with if condition within the loop.
In case of inner loops, it continues the control of inner loop only.

Syntax:

  1. jump-statement;  
  2. continue;  
The jump statement can be while, do while and for loop.

Example of continue statement in c

  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1;//initializing a local variable  
  5. clrscr();    
  6.   
  7. //starting a loop from 1 to 10  
  8. for(i=1;i<=10;i++){    
  9. if(i==5){//if value of i is equal to 5, it will continue the loop  
  10. continue;  
  11. }  
  12. printf("%d \n",i);  
  13. }//end of for loop  
  14.   
  15. getch();    
  16. }    

Output

1
2
3
4
6
7
8
9
10
As you can see, 5 is not printed on the console because loop is continued at i==5.

C continue statement with inner loop

In such case, C continue statement continues only inner loop, but not outer loop.
  1. #include <stdio.h>    
  2. #include <conio.h>    
  3. void main(){    
  4. int i=1,j=1;//initializing a local variable  
  5. clrscr();    
  6.   
  7. for(i=1;i<=3;i++){    
  8. for(j=1;j<=3;j++){  
  9. if(i==2 && j==2){  
  10. continue;//will continue loop of j only  
  11. }  
  12. printf("%d &d\n",i,j);  
  13. }  
  14. }//end of for loop  
  15.   
  16. getch();    
  17. }    

Output

1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
As you can see, 2 2 is not printed on the console because inner loop is continued at i==2 and j==2.

C goto statement

The goto statement is known as jump statement in C language. It is used to unconditionally jump to other label. It transfers control to other parts of the program.
It is rarely used today because it makes program less readable and complex.
Syntax:
  1. goto label;  

goto example

Let's see a simple example to use goto statement in C language.
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void main() {  
  4.   int age;  
  5.   clrscr();  
  6.    ineligible:  
  7.    printf("You are not eligible to vote!\n");  
  8.   
  9.    printf("Enter you age:\n");  
  10.    scanf("%d", &age);  
  11.    if(age<18)  
  12.         goto ineligible;  
  13.    else  
  14.         printf("You are eligible to vote!\n");  
  15.   
  16.    getch();  
  17. }  
Output:
You are not eligible to vote!
Enter you age:
11
You are not eligible to vote!
Enter you age:
44
You are eligible to vote!

Type Casting in C

Type casting allows us to convert one data type into other. In C language, we use cast operator for type casting which is denoted by (type).
Syntax:
  1. (type)value;      

Note: It is always recommended to convert lower value to higher for avoiding data loss.

Without Type Casting:
  1. int f= 9/4;  
  2. printf("f : %d\n", f );//Output: 2  
With Type Casting:
  1. float f=(float) 9/4;  
  2. printf("f : %f\n", f );//Output: 2.250000  

Type Casting example

Let's see a simple example to cast int value into float.
  1. #include <stdio.h>      
  2. #include <conio.h>    
  3. void main(){      
  4. clrscr();      
  5.   
  6. float f= (float)9/4;  
  7. printf("f : %f\n", f );  
  8.   
  9. getch();      
  10. }      
Output:
f : 2.250000

By R.T.Reddy