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

257333lecture 8 - C PP Control Statements-1706693016077

Control statement in cpp

Uploaded by

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

257333lecture 8 - C PP Control Statements-1706693016077

Control statement in cpp

Uploaded by

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

Object Oriented

Programming
with C++
Lecture 8-C++
Control Statements
While Statement

For Loop

Jump, Break and return Statement


Lecture Objectives

By the end of this class, you will be able


to:
▪ Provide an understanding of jump
statements
▪ Explore the purpose and application of
the break statement
▪ Introduce the continue statement and
its role in controlling loop iterations
▪ Explain the significance of the return
statement
Jump and Break Statement

Using Break as a Form of Goto

Continue and return


Jump Statements

▪ Break, continue, and return are the three


jump statements supported by C++

▪ Control is transferred to another area of


your application using these statements
Break Statement

The break statement has three purposes in C+


+:

▪ It first terminates a statement sequence in


a switch statement, as you've seen

▪ It can also be used to break a loop

▪ It can also be used as a ‘refined’ version of


goto
Jump and Break Statement

Using Break as a Form of Goto

Continue and return


Using Break to Exit a Loop

▪ Break allows you to force the instant termination of a loop, bypassing the conditional expression and
any remaining code in the loop's body

▪ When a break statement is reached within a loop, the loop is ended, and programme control returns to
the statement that follows the loop. Here’s an easy example:

// Using break to exit a loop.


Output:
class BreakLoop {
public static void main(String i: 0
args[]) { i: 1
for(int i=0; i<100; i++) { i: 2
if(i == 5) break; i: 3
System.out.println("i: " + i); i: 4
} Welcome to C+
System.out.println("Welcome to C+ +
+.");
}
}
Using Break to Exit a Loop

▪ The break statement can be used with any loop in C++, even loops that are purposefully infinite. For
example, consider the following program, which was written using a while loop

// Using break to exit a while loop.


class BreakLoop2 {
public static void main(String
args[]) {
int i = 0;
while(i < 100) {
if(i == 5) break; // terminate
loop if i is 10
System.out.println("i: " + i);
i++;
}
System.out.println("Welcome to
C++.");
}
}
Using Break as a Form of Goto

▪ In addition to its utility with the switch statement


and loops, the break statement can be used alone
to provide a ‘civilized’ version of the goto
statement

▪ C++ lacks a goto statement because it allows for


arbitrary and uncontrolled branching. Goto-heavy
code is notoriously difficult to understand and
maintain

▪ However, in a few cases, the goto is a helpful and


acceptable flow control tool. The goto can be useful
when exiting a heavily nested set of loops, for
example

▪ To handle such cases, C++ includes an extended


variation of the break statement. This form of break
can be used to exit one or more code blocks

▪ Break, as you will see, delivers the advantages of a


goto without the disadvantages. The following is
the general form of the labelled break statement:
Using Break as a Form of Goto

▪ Example:

class Break {
public static void main(String
args[]) {
sboolean t = true;
first: {
second: {
third: {
System.out.println("Before the
break.");
if(t) break second; // break out of
second block
Jump and Break Statement

Using Break as a Form of Goto

Continue and return


Using Break to Exit a Loop

▪ Example continued:

System.out.println("This won't
execute");
} Output:
System.out.println("This won't Before the break
execute"); This is after second
} block
System.out.println("This is after
second block.");
}
}
}
Continue Statement

▪ Sometimes, forcing an early loop iteration


can be useful

▪ That is, you may want to continue running


the loop but stop processing the rest of the
code in its body for this iteration

▪ This is essentially a goto to the loop's end,


just past the loop's body. The continue
statement accomplishes this
Using Continue

▪ Example:

// Demonstrate continue.
class Continue {
public static void main(String Output:
args[]) { 0 1
for(int i=0; i<10; i++) { 2 3
System.out.print(i + " "); 4 5
if (i%2 == 0) continue; 6 7
System.out.println(""); 8 9
}
}
}
Using Continue

▪ Like the break statement, the continue statement can specify a label to indicate which enclosing loop
should be continued. Here is an example program that uses continue to print a triangle multiplication
table for 0 through 9

// Using continue with a label.


class ContinueLabel {
public static void
main(String args[]) {
outer: for (int i=0; i<10; i+
+) {
for(int j=0; j<10; j++) {
if(j > i) {
System.out.println();
Using Continue

▪ Program continued..

Output:
continue outer; 0
} 0 1
System.out.print(" " + (i * 0 2 4
j)); 0 3 69
} 0 4 8 12 16
0 5 10 15 20 25
} 0 6 12 18 24 30 36
System.out.println(); 0 7 14 21 28 35 42 49
} 0 8 16 24 32 40 48 56 64
} 0 9 18 27 36 45 54 63 72 81
Return

▪ The final control statement is return.


The return statement is used to return
immediately from a method. That is,
the method's caller regains control of
the programme. As a result, it is
considered a jump statement

▪ To cause execution to return to the


method's caller, use the return
statement at any point in the method.
As a result, the return statement
immediately ends the method in which
it is used
Return

▪ The following example demonstrates this:

// Demonstrate return.
class Return {
public static void main(String
args[]) {
boolean t = true; Output:
System.out.println("Before the Before the
return."); return
if(t) return; // return to
caller
System.out.println("This won't
execute.");
}
}
Jump and Break Statement

Using Break as a Form of Goto

Continue and return

You might also like