257333lecture 8 - C PP Control Statements-1706693016077
257333lecture 8 - C PP Control Statements-1706693016077
Programming
with C++
Lecture 8-C++
Control Statements
While Statement
For 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:
▪ 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
▪ 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
▪ 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
▪ 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
▪ 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
// 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