[go: up one dir, main page]

0% found this document useful (0 votes)
19 views64 pages

T7 Jump Nested Loops

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views64 pages

T7 Jump Nested Loops

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Winter Camp 2022

C Programming

T7-Jump Statements, Nested Loops


Prepared By: REC Faculty 4.0 Team
TOPICS
JUMP STATEMENTS
BREAK
CONTINUE
GOTO
EXIT
NESTED LOOP STATEMENT SYNTAX &
EXAMPLES
MCQ
JUMP STATEMENTS
• C programming language allows jumping from one
statement to another.

• It also supports break, continue, return and go to


jump statements.
JUMP STATEMENTS
Jump statements?

Transfer control from one point to another point


within the program
break
• The keyword break allows the programmer to
terminate the loop.
• The break skips from the loop or block in which it
is defined.
• The control then automatically goes to the first
statement after the loop or block.
• The break can be associated with all conditional
statements.
break
• We can also use break statements in the nested
loops.
• If we use break statement in the innermost loop,
then the control of the program is terminated only
from the innermost loop.
• break statement can be used in an if statement only
when the if statement is written in a loop.
• Syntax: break;
continue
• The continue statement is exactly opposite to
break.
• The continue statement is used for continuing next
iteration of loop statement.
• When it occurs in the loop, it does not terminate,
but it skips the statements after this statement.
• It is useful when we want to continue the program
without executing any part of the program.
exit

• The function ‘exit’ is used to quit the


program.
• It terminates the program and returns the
status code to the operating system.
• This function is defined in ‘stdlib.h’ header
file.
goto

• A goto statement in C programming


provides an unconditional jump from the
'goto' to a labeled statement in the same
function.
• The syntax for a goto statement in C is as
follows:
#include <stdio.h>
int main () {
/* local variable definition */
int a = 10;
/* do loop execution */
LOOP:do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
goto LOOP;
}
printf("value of a: %d\n", a);
a++;
}while( a < 20 );
return 0;
}
exit
• exit(status);

• The status code zero indicates that the program


completed successfully.
• If there is a failure any other code has to be
returned.
MCQS
Answer : C
Answer : C
Answer : C
Answer : B
Answer : A
Answer : A
Answer : D
Nested Loops
• Similar to nested if statements, loops can be nested as
well
• That is, the body of a loop can contain another loop
• For each iteration of the outer loop, the inner loop
iterates completely

22
Nested Loops
 How many times will the string "Here" be printed?
count1 = 1;
while (count1 <= 10)
{
count2 = 1;
while (count2 <= 20)
{
printf ("Here");
count2++;
}
count1++;
}
10 * 20 = 200 23
Looping- nested for
statements
What are nested for loops?

Loop nested within loops

For example:

for(initialization; condition test ; updation)


{
//outer loop statement;
for(initialization; condition test ; updation)
{
//inner loop statements;
}
//outer loop statements;
}
Flowchart

25
Looping- nested for statement
working
for(int i=1; i<=2;i++) OUTER FOR
{
int j;
for( j=1 ;i<=3 ; j++) INNER FOR
{
printf(“%d %d”,i,j);
} How many
statements
printf(“\n”); inside inner
How many
} for?
statements
inside outer
for?
Looping- nested for statement
working
i=1 2( for true)
for(int i=1; i<=2;i++)
{
int j;
j= 1 2 3 (for true)
for( j=1 ;i<=3 ; j++)
{ i j printf
printf(“%d %d”,i,j); 1 1 11
} 1 2 12
printf(“\n”); 1 3 13
} How many 2 1 21
times i and j 2 2 22
are printed? 2 3 23
Looping- nested for statement
working
How many times outer for
loop got executed ?
for(int i=1; i<=2;i++)
{ 2
int j;
for( j=1 ;i<=3 ; j++)
{ How many times inner for
loop got executed ?
printf(“%d %d”,i,j);
} 6
printf(“\n”); For each execution of the
} outer loop the inner loop
is executed 3 times, hence
2*3=6
MCQS
Answer : C
Answer : B
Programs
33
Programs

1. Write a program in C to display the triangle using “*” in the


following pattern.
35
Programs

1. Write a program in C to display the triangle using “*” in the


following pattern.
36
Programs

1. Write a program in C to display the triangle using “*” in the


following pattern.
37
Programs

1. Write a program in C to display a pyramid with “*”.


38
Programs

1. Write a program in C to display a pyramid with “*”.


39
Programs

1. Write a program in C to display a pyramid with “*”.


41
Programs

1. Write a program in C to display the right angle triangle using


numbers.
42
Programs

1. Write a program in C to display the right angle triangle using


numbers.
43
Programs

1. Write a program in C to display the right angle triangle using


numbers.
45
Programs

1. Write a program in C to display the right angle triangle using


numbers.
47
Programs

1. Write a program in C to display the right angle triangle using


numbers.
Simple Chessboard
Reverse and Add Until Get a
Palindrome Number
Lucky Number
55
Programs

1. Write a program in C to display a square matrix with alternative


character in the cell( consider only 2 characters).Eg:

1 2 1
2 1 2
1 2 1
56
Programs

1. Write a program in C to display a square matrix with alternative


character in the cell( consider only 2 characters).Eg:
57
Programs

1. Write a program in C to display a following output.


58
59
Programs

1. Write a program in C to display a following output.


60
61
Programs

1. Write a program in C to display a following output.


62
63
Programs

1. Write a program in C to display a following output.


64

You might also like