Continue:-This keyword is used to skip the statements below that go to next iteration.
syntax:- continue;
Example:- To skip 5 and 7 while printing from 1 to 10
#include<stdio.h>main(){int i;for(i=1;i<=10;i++){if(i==5||i==7)continue;printf("%d\n",i);}}
Explanation:-
1.When i=1 and it is less than 10 so the condition is satisfied.2.It goes if statement and checks whether it is equal to 5 or 7 ans as they proves to be false then it prints the next statement i.e to print '1'.3.Like this it continues till 4 by incrementing 'i' value.4.When it is 5 then the statement which is below continue i.e "printf("%d\n",i);" is skiped and returned to next iteration.And the same thing happens to 7.
Program:-
output:-
Goto Statement:-
This keyword is used when ever you want to jump from one statement to the other where there is a label.For every goto there must be a label from where it starts executing.
Syntax:-
label: ...................; statements; .....................; goto label;we can use any name instead of using label.Remember that only goto is a keyword and not label.
Example:-
#include<stdio.h> main() { int i,j; for(i=1;i<10;i++) { if(i==5) goto next; else printf("%d\n",i); } next: for(j=11;j<=20;j++) { printf("%d\n",j); } }
Explanation:-1.)'i' and 'j 'are intialised as integers. 2.)i=1 and i is equal to 5 or not and as it is not it prints i value in else part. 3.)It goes on until i=5 and now it is equal to 5.So the goto statement is executed and jumps to the label next. 4.)The statement which are under next are executed and here it is to print from 11 to 20. This is how a label works.Output:-
No comments:
Post a Comment