Showing posts with label control statement. Show all posts
Showing posts with label control statement. Show all posts

Wednesday, February 4, 2015

C continue statement


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
4
3 6
9
7 8
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
2 1
1 3 2 3
3 3
3 1
3 2
As you can see, 2 2 is not printed on the console because inner loop is continued at i==2 and j==2.

C break statement


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.

Tuesday, February 3, 2015

C Switch Statement


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:
switch(expression){    
case value1:    
 //code to be executed;    
 break;  //optional  
case value2:    
 //code to be executed;    
 break;  //optional  
......  
   
default:     
 code to be executed if all cases are not matched;    
}    

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.
#include<stdio.h>  
#include<conio.h>  
void main(){  
int number=0;  
clrscr();

printf("enter a number:");  
scanf("%d",&number);  

switch(number){  
case 10:  
printf("number is equals to 10");  
break;  
case 50:  
printf("number is equal to 50");  
break;  
case 100:  
printf("number is equal to 100");  
break;  
default:  
printf("number is not equal to 10, 50 or 100");  
}
getch();
}  

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.
#include<stdio.h>  
#include<conio.h>  
void main(){  
int number=0;  
clrscr();

printf("enter a number:");  
scanf("%d",&number);  

switch(number){  
case 10:  
printf("number is equals to 10\n");  
case 50:  
printf("number is equal to 50\n");  
case 100:  
printf("number is equal to 100\n");  
default:  
printf("number is not equal to 10, 50 or 100");  
}
getch();
}  

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 if else 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:


if(expression){  
//code to be executed  

Flowchart of if statement in C


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

#include<stdio.h>  

#include<conio.h>  
void main(){  
int number=0;  
clrscr();  
  
printf("enter a number:");  
scanf("%d",&number);  
  
if(number%2==0){  
printf("%d is even number",number);  
}  
  
getch();  
}  

Output

enter a number:4
4 is even number

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:

if(expression){  
//code to be executed if condition is true  
}else{  
//code to be executed if condition is false  

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.\
#include<stdio.h>  
#include<conio.h>  
void main(){  
int number=0;  
clrscr();  
  
printf("enter a number:");  
scanf("%d",&number);  
  
if(number%2==0){  
printf("%d is even number",number);  
}  
else{  
printf("%d is odd number",number);  
}  
getch();  

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:
if(condition1){  
//code to be executed if condition1 is true  
}else if(condition2){  
//code to be executed if condition2 is true  
}  
else if(condition3){  
//code to be executed if condition3 is true  
}  
...  
else{  
//code to be executed if all the conditions are false  

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.
#include<stdio.h>  
#include<conio.h>  
void main(){  
int number=0;  
clrscr();  
  
printf("enter a number:");  
scanf("%d",&number);  
  
if(number==10){  
printf("number is equals to 10");  
}  
else if(number==50){  
printf("number is equal to 50");  
}  
else if(number==100){  
printf("number is equal to 100");  
}  
else{  
printf("number is not equal to 10, 50 or 100");  
}  
getch();  
}

Output

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

enter a number:50
number is equal to 50