0% found this document useful (0 votes)
8 views43 pages

Lecture 17

Uploaded by

dr_Sakshi_arora
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)
8 views43 pages

Lecture 17

Uploaded by

dr_Sakshi_arora
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/ 43

Jumping statements

Introduction

• There are 2 special statements that can affect the execution


of loop statements (such as a while-statement)
• The special statements are:

• break

• continue
We will study their meaning and how to use these special
statements inside the while-statement
How Break Statement works?
The break statement

• Syntax:

break;

Effect:
• When the break statement is executed inside a loop-
statement, the loop-statement is terminated immediately
• The execution of the program will continue with the statement
following the loop-statement
• The break statement is almost always used
with if...else statement inside the loop.
The break statement (cont.)

• Schematically:
Write a program to calculate the sum of
numbers (max 10 numbers) . If the user enters a
negative number loop terminates.
Example 1:
Example 1:

Output:
Example 2:
Example 2:
Write a program to determine whether a
number is prime or not. A prime number is
one, which is divisible only by 1 or itself.
Write a program to determine whether a number is prime or
not. A prime number is one, which is divisible only by 1 or itself.

Step 1: Enter the num


Step 2: set i=2;
Step 3: repeat steps 4 to 5 while i < =num-1
Step 4: if(num%i==0)
break and exit the loop
else
i=i+1;
Step 5: go to step 3
Step 6: if(i==num)
Print num is a prime number
Step 7: End
Example 3: Write a
program to
determine whether
a number is prime
or not. A prime
number is one,
which is divisible
only by 1 or itself.
The keyword break, breaks the control only from the while in
which it is placed

What would be its output?


Programming example using the break
statement: find the GCD
• Problem description:

• Write a C program that reads in 2 numbers x


and y...
• and prints the largest common divisor of
both x and y
Programming example using the break
statement: find the GCD (cont.)
• A concrete example:

• Input: x = 24 and y = 16
• Output: 8
Programming example using the break
statement: find the GCD (cont.)
• What would you do to solve this problem ?

• Suppose: x = 24 and y = 16
• The lesser of the values is 16
• Therefore, all divisors are ≤ 16
Programming example using the break
statement: find the GCD (cont.)
• Check if 16 and 24 are divisible by 16: no
• Check if 16 and 24 are divisible by 15: no
• ...
• Check if 16 and 24 are divisible by 10: no
• Check if 16 and 24 are divisible by 9: no
• Check if 16 and 24 are divisible by 8: YES

• Print 8 and STOP


Programming example using the break
statement: find the GCD (cont.)
• Algorithm (structured diagram):
The continue statement

• Syntax:

continue;
The continue statement (cont.)

• Effect: When the continue statement is executed inside a


loop-statement, the program will skip over the remainder of
the loop-body to the end of the loop

• What happens next when the program reaches the


end of a loop depends on the type of loop statement
!!!
•The continue statement is almost always used with
the if……else statement
•Continue doesn’t terminate the loop but takes the
loop to next iteration
The continue statement (cont.)

• Effect of a continue statement in a while-loop:

• As given previously:

• the program will skip over the remainder of


the loop-body to the end of the loop

•In the case of a while-loop, when the program


reaches end of the loop, the program will jump back
to the testing of the loop-continuation-condition
How continue statement works?
The continue statement (cont.)

• Schematically:
Example: Continue
Example: Continue
Example
Example
Example 2: continue statement
Example 2: continue statement

Output:
Programming example using the continue
statement: find all divisors of a number
• Problem description:

• Write a C program that reads in an integer n...


• and prints all its divisors
Programming example using the continue
statement: find all divisors of a number (cont.)
• Previously discussed solution:

We try every number a = 1, 2, ..., n


For each number a, we check if n % a == 0.
Programming example using the continue
statement: find all divisors of a number (cont.)
• We can re-write the same algorithm differently using a
continue statement as follows:
n=6
a=1;
6%1!=0 False Print 1 a=2
6%2!=0 False Print 2 a=3
6%3!=0 False Print 3 a=4
6%4!=0 True a=5 continue
6%5!=0 True a=6 continue
6%6!=0 True Print 6
Programming example using the continue
statement: find all divisors of a number (cont.)
Notice that the if-condition has been changed to x %
a != 0, meaning: a is not a divisor of x

When a is not a divisor of x, (the then-part), we


increment a (to try next number) and jump to the
end of the while-loop using the continue statement.

When x % a != 0 is false, the program will print a and


increment a (to try next number)
Difference Between break and continue
Difference Between break and continue
goto statement
Example: goto statement
Example: goto statement

Output:
Return statement
Problem Statement
Write a program to generate all prime numbers
between 1 to 1000
Write a program to generate all prime numbers between
1 to 1000

Algorithm [Though Incomplete]


Strep 1 j=1;
Step 2: Repeat steps 3 to 6 while j <=1000 (i.e
for(j=1;j<=1000 ;j++))
Step 3: num=j
Step 4: Check weather num is prime or not
Step 5: Print num if prime
Step 6: increment j and go to step 2
Step 7: End
Algorithm
Strep 1 j=1;
Step 2: Repeat steps 3 to 6 while j <=1000 (i.e for(j=1;j<=1000 ;j++))
Step 3: num=j
Step 4: set i=2;
Step 5: repeat steps 6 to 7 while i < =num-1
Step 6: if(num%i==0)
break and exit the loop
Inner loop checking
else
prime number
i=i+1;
Step 7: go to step 5
Step 8: if(i==num)
Print num is a prime number
Step 9: increment j and go to step 2
Step 10: End

You might also like