c programming slide c04
TRANSCRIPT
Wednesday, April 12, 2023
1
PRADEEP DWIVEDI
PREPARED BY-PRADEEP DWIVEDI(persuing B.TECH-IT)from HINDUSTAN COLLEGE OF SCIENCE AND
TECHNOLOGY(MATHURA)[email protected]
C-PROGRAMMING SLIDE-4
Wednesday, April 12, 2023PRADEEP DWIVEDI
2
C-4
TOPIC:- Loop break statement continue statement go to statement
Wednesday, April 12, 2023PRADEEP DWIVEDI
3
LOOP
when we want to repeat a particular task again and again that time we use the loop.
loop is a control structure that repeats a group of sets in a program.
there are three loops in c-1. while 2. for3. do while
Wednesday, April 12, 2023PRADEEP DWIVEDI
4
WHILE LOOP
syntax:-
while(test-condition){
body of the loop.
}
Wednesday, April 12, 2023PRADEEP DWIVEDI
5
WHILE LOOP
the while loop is a entry controlled loop statement.
the test condition is evaluated and if the condition is true then the body of loop is executed.
after execution of the body the test condition is once again evaluated and if it is true the body is executed once again.
this process of repeated execution of the body continues until the test condition finally becomes false and controlled is transferred out of the loop.
Wednesday, April 12, 2023PRADEEP DWIVEDI
6
FLOW CHART(WHILE LOOP)
Wednesday, April 12, 2023PRADEEP DWIVEDI
7
PROG13
/* w a p to print 1 to 10 */#include<stdio.h>#include<conio.h>void main(){int i;clrscr();i=1;while(i<=10){printf("%d\t",i);i++;}getch();}
Wednesday, April 12, 2023PRADEEP DWIVEDI
8
DO WHILE LOOP
in while loop, makes a test of condition before the loop is executed.
therefore, the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt.
on a some situations it must be necessary to execute the body of the loop before the test is performed.
such situation can be handled with the help of the do statement.
Wednesday, April 12, 2023PRADEEP DWIVEDI
9
DO WHILE LOOP
syntax:-do{
body of the loop (true)
}while(test-condition);
Wednesday, April 12, 2023PRADEEP DWIVEDI
10
DO WHILE LOOP
in the do while loop it first execute the body and then checks the condition.
so if the condition is initially false, it execute atleast once.
Wednesday, April 12, 2023PRADEEP DWIVEDI
11
prog14
/* w.a.p. to print 1 TO 10 digit */#include<stdio.h>#include<conio.h>void main(){int i;clrscr();i=1;do{printf("%d\t",i);i++;}while(i<=10);getch();}
Wednesday, April 12, 2023PRADEEP DWIVEDI
12
FOR LOOP
It is two type if1. simple for loop.2. nested for loop.
Wednesday, April 12, 2023PRADEEP DWIVEDI
13
FOR LOOP
syntax:-
for(initialization; test of the condition; increment/decrement)
{
body of loop
}
Wednesday, April 12, 2023PRADEEP DWIVEDI
14
FOR LOOP
in a for loop we always follows the following 4 steps sequentially.
1. initialize the value.2. test the condition (if it is true)3. execute the body.4. increment/decrement the value.
Wednesday, April 12, 2023PRADEEP DWIVEDI
15
FOR LOOP
eg:-
for(i=1;i<=10;i++){
printf(“%d”,i);
}
1 2
3
4
falsetrue
Wednesday, April 12, 2023
16
PRADEEP DWIVEDI
prog15
/* w.a.p. to check the entered number is a prime number or not */
#include<stdio.h>#include<conio.h>#include<process.h>void main(){int i,num;clrscr();printf("Enter a number");scanf("%d",&num);
for(i=2;i<num;i++){if(num%i==0){printf("number is not
prime");getch();exit(0);}}printf("Number is prime");getch();}
Wednesday, April 12, 2023PRADEEP DWIVEDI
17
SOME POINTS
exit() is a predefine function with the help of it we move out or terminate from the program.
it comes from process.h header file. when we use semicolon after the for loop
that time it become a self executed loop and it execute when condition become false.
we can have more than one initialization and the iteration in a for loop.
eg. for(i=1;j=10;i<=10;i++; j--)
Wednesday, April 12, 2023PRADEEP DWIVEDI
18
prog16
/*w.a.p. to calculate factorial of a given number */#include<stdio.h>#include<conio.h>void main(){int n,i,fac=1;clrscr();printf("Enter a number whose factorial is found :");scanf("%d",&n);for(i=n;i>=1;i--){fac=fac*i;}printf("factorial is:%d",fac);getch();}
Wednesday, April 12, 2023PRADEEP DWIVEDI
19
prog17
/* w.a.p. to print fabonicai series 1 1 2 3 5 8 13 21 34 55 89 144 223 337 */#include<stdio.h>#include<conio.h>void main(){int num1,num2;clrscr();num1=num2=1;printf("%d\t",num2);while(num2<=200){printf("%d\t",num2);num2=num2+num1;num1=num2-num1;}getch();}
Wednesday, April 12, 2023PRADEEP DWIVEDI
20
NESTED FOR LOOP
nesting of loops, that is, one for statement with in another for statement is allowed in c.
eg.for(i=1;i<10;++i)
{for(j=1;j<5;++j)
{
} }
innerloop
outer loop
Wednesday, April 12, 2023
21
PRADEEP DWIVEDI
prog18
/* print this pattern
11 2 1 2 31 2 3 41 2 3 4 5
*/#include<stdio.h>#include<conio.h>void main(){int i,j,num;clrscr();
printf("Enter the number :");scanf("%d",&num);for(i=1;i<=num;i++){for(j=1;j<=i;j++){printf("%d\t",j);}printf("\n");}getch();}
Wednesday, April 12, 2023
22
PRADEEP DWIVEDI
prog19
/* w.a.p. to print this pattern
* * * * ** * * ** * * * **
*/#include<stdio.h>#include<conio.h>void main(){int i,j;clrscr();
for(i=1;i<=5;i++){for(j=5;j>=i;j--){printf("* ");}printf("\n");}getch();}
Wednesday, April 12, 2023PRADEEP DWIVEDI
23
JUMPING IN THE LOOP
For jumping in the loop we use two statements:
1. break2. continue
Wednesday, April 12, 2023PRADEEP DWIVEDI
24
THE BREAK STATEMENT
break is a keyword with the help of it we jump out from the loop.
break is only use in the loop and switch case.
Wednesday, April 12, 2023PRADEEP DWIVEDI
25
SYNTAX FOR BREAK STATEMENTA. while(………..) {
…….……..
if (condition)break;…….}
exit from loop
Wednesday, April 12, 2023PRADEEP DWIVEDI
26
SYNTAX FOR BREAK STATEMENTB. do
{ ……..
if(condition)break;……….……….}while(………);
exit from loop
Wednesday, April 12, 2023PRADEEP DWIVEDI
27
SYNTAX FOR BREAK STATEMENTC.
for(……………………………………){---------
--------- if(condition)
break;--------------
}------
exit from loop
Wednesday, April 12, 2023PRADEEP DWIVEDI
28
SYNTAX FOR BREAK STATEMENTD.
for(……………………………….){
………for(……………………..){
if(condition)break;…}..}
exit from inner loop
Wednesday, April 12, 2023
29
PRADEEP DWIVEDI
prog20
/*w.a.p. to determine wheather a number is prime or not(using break statement)*/
#include<stdio.h>#include<conio.h>void main(){int num,i;clrscr();printf("Enter a number: ");scanf("%d",&num);i=2;
while(i<num){if(num%i==0){printf("number is not prime"); break;
}i++;}if(i==num)printf("number is prime");getch();}
Wednesday, April 12, 2023PRADEEP DWIVEDI
30
goto STATEMENT
goto keyword (statement) can transfers the control to any place in a program, it is useful to provide branching with in a loop.
Wednesday, April 12, 2023PRADEEP DWIVEDI
31
SYNTAX OF GOTO STATEMENTwhile(………..) (A){if(error)goto stop;…….…… if(condition)goto abc;……….……….abc:……..}stop:…………
jump with in loop
exit from loop
Wednesday, April 12, 2023PRADEEP DWIVEDI
32
SYNTAX OF GOTO STATEMENTfor(………………..) (B){……..……..
for(…….){………....
if(error)goto error;……..}…..}error:…..…….
Wednesday, April 12, 2023PRADEEP DWIVEDI
33
SOME POINTS
in syntax(A) if the condition is satisfied goto statement transfers control to the label abc:
stop: abc: error:
called label
Wednesday, April 12, 2023
34
PRADEEP DWIVEDI
prog21
/* Demo for goto statement */#include<stdio.h>#include<conio.h>void main(){int i,j,k;clrscr();for(i=1;i<=3;i++){for(j=1;j<=3;j++){for(k=1;k<=3;k++){
if(i==3&&j==3&&k==3)goto out;elseprintf("%d%d%d\n",i,j,k);}}}out:printf("out of the loop at
last!");getch();}
Wednesday, April 12, 2023PRADEEP DWIVEDI
35
SKIPPING A PART OF A LOOP
For skipping a part of a loop we use continue keyword.
the continue statement tells the compiler ,”skip the following statements and continue with next iteration”.
the format of continue statement is simply:
continue;continue:- is a keyword with the help of
that we can move up at the beginning of the loop.
Wednesday, April 12, 2023PRADEEP DWIVEDI
36
SYNTAX
A.while(test-condition){continue;……..……..}
Wednesday, April 12, 2023PRADEEP DWIVEDI
37
SYNTAX
B.do{
……….……….
if(……..)continue;……..……..
}while(test-condition);
Wednesday, April 12, 2023PRADEEP DWIVEDI
38
SYNTAX
C.
for(initialization;test-condition;increment){
…………..…………..
if(………….)continue;……….……….}
Wednesday, April 12, 2023PRADEEP DWIVEDI
39
prog22
/* Demo for break statement */#include<stdio.h>#include<conio.h>void main(){int i;for(i=0;i<=10;i++){if(i==5)continue;printf("Er.pradeep dwivedi\n");}getch();}