Control Statements in C with Examples

 If Statement

The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below.


1. if(expression){

2. //code to be executed 

3. }


Let's see a simple example of C language if statement.


1. #include<stdio.h>

2. int main(){

3. int number=0;

4. printf("Enter a number:");

5. scanf("%d",&number);

6. if(number%2==0){

7. printf("%d is even number",number); 8. }

9. return 0;

10. }


Output

Enter a number:4 

4 is even number 

enter a number:5


If-else Statement

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simultaneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition. The syntax of the 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. }


Let's see the simple example to check whether a number is even or odd using if-else statement in C language.


1. #include<stdio.h>

2. int main(){

3. int number=0;

4. printf("enter a number:");

5. scanf("%d",&number);

6. if(number%2==0){

7. printf("%d is even number",number); 8. }

9. else{

10. printf("%d is odd number",number);

11. }

12. return 0;

13. }


Output

enter a number:4 

4 is even number


If else-if ladder Statement

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. }


The example of an if-else-if statement in C language is given below.


1. #include<stdio.h>

2. int main(){

3. int number=0;

4. printf("enter a number:");

5. scanf("%d",&number);

6. if(number==10){

7. printf("number is equals to 10"); 8. }

9. else if(number==50){

10. printf("number is equal to 50");

11. }

12. else if(number==100){

13. printf("number is equal to 100");

14. }

15. else{

16. printf("number is not equal to 10, 50 or 100");

17. }

18. return 0;

19. }


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 is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.


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. }


Let's see a simple example of c language switch statement.


1. #include<stdio.h>

2. int main(){

3. int number=0;

4. printf("enter a number:");

5. scanf("%d",&number);

6. switch(number){

7. case 10:

8. printf("number is equals to 10");

9. break;

10. case 50:

11. printf("number is equal to 50");

12. break;

13. case 100:

14. printf("number is equal to 100");

15. break;

16. default:

17. printf("number is not equal to 10, 50 or 100");

18. }

19. return 0;

20. }


Output

enter a number:4

number is not equal to 10, 50 or 100


Post a Comment

0 Comments