Вы находитесь на странице: 1из 16

Represented by-

Muhammad Faisal Imran


RUET,CSE09

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

//program about goto statement

#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);

ans = num1 + num2;

printf("\nAddition of 2 Numbers is %d");


}

Syntax of the For Loop :


for(initial expression; test expression; update expression)
{
body of loop;
}
We will get better idea after looking at above for loop flowchart.
Flowchart of For Loop :

Explanation of For Loop :


1. Firstly the for loop executes the initialize statement in which the subscript variable will
be initialized with the initial value.
2. After the initialize statement the condition part of the loop will be executed if the
condition becomes true then body of the loop will be executed otherwise the loop will
be terminated
3. If the loop condition becomes true then body of the loop will be executed. After the
execution of the for loop body the control goes to the third part of the loop statement
i.e Expression Updation
4. After updating subscript variable control again goes to execute condition statement.
For Loop : Dry Run
Consider the following image then -

Sequence of the execution will be as below -


Seq. No Statement Flow Explanation

01 Flow No 1 will be executed i=0

02 Flow No 2 will be executed Condition Checking

03 Flow No 3 will be executed True Condition

04 Flow No 4 will be executed -

05 Flow No 5 will be executed i=1

06 Flow No 3 will be executed True Condition

07 Flow No 4 will be executed -


Seq. No Statement Flow Explanation

08 Flow No 5 will be executed i=2

09 Flow No 3 will be executed True Condition

10 Flow No 4 will be executed -

11 Flow No 5 will be executed i=3

12 Flow No 3 will be executed True Condition

13 Flow No 4 will be executed -

14 Flow No 5 will be executed i=4

15 Flow No 3 will be executed True Condition

16 Flow No 4 will be executed -

17 Flow No 5 will be executed i=5

18 Flow No 3 will be executed False Condition

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.

Different Ways of Writing For Loop in C Programming Language


Way 1 : Single Statement inside For Loop
for(i=0;i<5;i++)
printf("Hello");
1. Above code snippet will print Hello word 5 times.
2. We have single statement inside for loop body.
3. No need to wrap printf inside opening and closing curly block.
4. Curly Block is Optional.

Way 2 : Multiple Statements inside For Loop


for(i=0;i<5;i++)
{
printf("Statement 1");
printf("Statement 2");
printf("Statement 3");

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.

Way 3 : No Statement inside For Loop


for(i=0;i<5;i++)
{

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

Way 4 : Semicolon at the end of For Loop


for(i=0;i<5;i++);
1. Generally beginners thought that , we will get compile error if we write semicolon at the end of for loop.
2. This is perfectly legal statement in C Programming.

3. This statement is similar to bodyless for loop. (Way 3)


Way 5 : Multiple Initialization Statement inside For

for(i=0,j=0;i<5;i++)
{
statement1;
statement2;
statement3;
}

Multiple initialization statements must be seperated by Comma in for loop.

Way 6 : Missing Increment/Decrement Statement

for(i=0;i<5;)
{
statement1;
statement2;
statement3;
i++;
}

however we have to explicitly alter the value i in the loop body.

Way 7 : Missing Initialization in For Loop

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

Way 8 : Infinite For Loop

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++ ) Single Statement


Statement1;

for ( i=0 ;i <10; i++) Multiple Statements within for


{
Statement1;
Statement2;
Statement3;
}

for ( i=0 ; i < 10;i++) ; For Loop with no Body ( Carefully Look at the
Semicolon )

for Multiple initialization & Multiple


(i=0,j=0;i<100;i++,j++) Update Statements Separated by Comma
Statement1;

for ( ; i<10 ; i++) Initialization not used

for ( ; i<10 ; ) Initialization & Update not used

for ( ; ; ) Infinite Loop,Never Terminates

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

//loop for sum

#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;
}

printf("Summation between 1 to %d is : %d",n,sum);


return 0;
}
//sum of positive integer using for loop

#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;
}

//loop for input word

#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;
}

//loop for prime

#include<stdio.h>
int main()
{
int i,j,n,check,count=0;

printf("Enter ur limit : ");


scanf("%d",&n);
for(j=1;j<=n;j++)
{
check=1;
for(i=2;i<=j/2;i++)
{
if(j%i==0){check=0;break;}
else check=1;
}
if(check==1){printf("%d ",j);count++;}
}

printf("\nTotal prime number between 1 to %d is: %d",n,count);


return 0;
}
//positive negative check using for loop and if else statement

#include<stdio.h>
#define max 5
int main()
{
int p=0,n=0,i,number[max];

printf("\nEnter 5 numbers sequentially : ");

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++;
}

printf("\nTotal positive number is : %d",p);


printf("\nTotal negative number is : %d",n);

return 0;
}

//printing pyramid using for loop

#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];

printf("\nEnter 5 numbers sequentially : ");

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