Professional Documents
Culture Documents
Represented By-Muhammad Faisal Imran Ruet, Cse09: Goto Statement
Represented By-Muhammad Faisal Imran Ruet, Cse09: Goto Statement
Goto Statement
goto label;
-------
-------
label :
Whenever goto keyword encountered then it causes the program to continue on the line , so
long as it is in the scope .
Types of Goto :
Forward
Backward
Note :
Some Problems Related to goto
//gcd
#include<stdio.h>
int main()
{
int a,b,x;
printf("Enter 1st number : ");
scanf("%d",&a);
printf("Enter 2nd number : ");
scanf("%d",&b);
if(a<b) x=a;
else if(b<a) x=b;
again:
if(a%x==0&&b%x==0)
{
printf("gcd is: %d",x);
}
else
{
x=x-1;
goto again;
}
return 0;
}
#include<stdio.h>
int main()
{
int i;
i=1;
start:
if(i>10)goto end;
printf("%d\n",i);
i++;
goto start;
end:
return 0;
}
for Loop
Understanding Looping Statement :
Whenever we need to execute certain action multiple times, we need to
wrapprogramming statement in the loop body. Take an example we are computing the sum
of 2 numbers 5 times then we need to write code in the loop body. Consider the following
trailer -
for(i=0; i<5; i++)
{
printf("\nEnter two Numbers : ");
scanf("%d %d",num1,num2);
Note :
1. For Single Line of Code – Opening and Closing braces are not needed.
2. There can Exist For Loop without body.
3. Initialization , Incrementation and Condition steps are on same Line.
4. Like While loop , For Loop is Entry Controlled Loop.[i.e conditions are checked if
found true then and then only code is executed ]
Different Ways of Using For Loop in C Programming
In order to do certain actions multiple times , we use loop control statements.
For loop can be implemented in different verities of using for loop -
1. Single Statement inside For Loop
2. Multiple Statements inside For Loop
3. No Statement inside For Loop
4. Semicolon at the end of For Loop
5. Multiple Initialization Statement inside For
6. Missing Initialization in For Loop
7. Missing Increment/Decrement Statement
8. Infinite For Loop
9. Condition with no Conditional Operator.
if(condition)
{
--------
--------
}
}
If we have block of code that is to be executed multiple times then we can use curlybraces to
wrap multiple statement in for loop.
}
this is bodyless for loop. It is used to increment value of “i”.This verity of for loop is not used
generally.
At the end of above for loop value of i will be 5.
for(i=0,j=0;i<5;i++)
{
statement1;
statement2;
statement3;
}
for(i=0;i<5;)
{
statement1;
statement2;
statement3;
i++;
}
i = 0;
for(;i<5;i++)
{
statement1;
statement2;
statement3;
}
we have to set value of „i‟ before entering in the loop otherwise it will take garbage value of „i‟.
i = 0;
for(;;)
{
statement1;
statement2;
statement3;
if(breaking condition)
break;
i++;
}
Infinite for loop must have breaking condition in order to break for loop. otherwise it will cause overflow of stack.
Summary of Different Ways of Implementing For Loop
Form Comment
for ( i=0 ; i < 10;i++) ; For Loop with no Body ( Carefully Look at the
Semicolon )
In the upcoming chapters we will be learning while loop and do-while loop statements in C
Programming.
For loop Related Problems
//loop for
#include<stdio.h>
int main()
{
int counter;
for(counter=1;counter<=10;counter++)
{
printf("Bangladesh\n");
}
return 0;
}
#include<stdio.h>
int main()
{
int i,n,sum;
printf("Enter ur limit : ");
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{
sum=sum+i;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int sum,i,n;//n is any positive integer
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{sum=sum+i;}
printf("The sum of the value is %d\n",sum);
return 0;
}
//CT marks
#include<stdio.h>
#define max 5
int main()
{
int i,marks[max];
for(i=0;i<=max-1;i++)
{
printf("\nPlease enter CT marks of roll %d : ",i);
scanf("%d",&marks[i]);
}
for(i=0;i<=max-1;i++)
printf("roll %d : %d\n",i,marks[i]);
return 0;
}
//loop for product
#include<stdio.h>
int main()
{
int i,n,p;
printf("Enter your number: ");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
p=n*i;
printf("%d*%d=%d\n",n,i,p);
}
return 0;
}
#include<stdio.h>
int main()
{
char word[100];
int i;
printf("Enter ur word to print : ");
scanf("%s",word);
for(i=1;i<=10;i++)
{
printf("%s\n",word);
}
return 0;
}
//loop for odd
#include<stdio.h>
int main()
{
int i,n;
printf("Enter ur limit : ");
scanf("%d",&n);
for(i=2;i<=n;i=i+2)
{
printf("%d ",i);
}
return 0;
}
#include<stdio.h>
int main()
{
int i,j,n,check,count=0;
#include<stdio.h>
#define max 5
int main()
{
int p=0,n=0,i,number[max];
for(i=0;i<=max-1;i++)
{
scanf("%d",&number[i]);
}
for(i=0;i<=max-1;i++)
{
if(number[i]<0)n++;
else p++;
}
return 0;
}
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=9;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
//reverse print using for loop
#include<stdio.h>
#define max 5
int main()
{
int p=0,n=0,i,number[max];
for(i=0;i<=max-1;i++)
{
scanf("%d",&number[i]);
}
for(i=max-1;i>=0;i--)
{
printf("%d ",number[i]);
}
return 0;
}
//continue
#include<stdio.h>
int main()
{
int i;
i=1;
for(i=1;i<=10;i++)
{
if(i==4)continue;
printf("%d\n",i);
}
return 0;
}