0% found this document useful (0 votes)
7 views5 pages

Computer Programming - Week 6

The document discusses repetition control structures in Java, including While, Do-While, and For loops, along with branching statements like Break, Continue, and Return. It provides examples of how to implement these structures to print and increment a counter variable. Each example demonstrates the syntax and functionality of the respective control structures.
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)
7 views5 pages

Computer Programming - Week 6

The document discusses repetition control structures in Java, including While, Do-While, and For loops, along with branching statements like Break, Continue, and Return. It provides examples of how to implement these structures to print and increment a counter variable. Each example demonstrates the syntax and functionality of the respective control structures.
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/ 5

Control Structures (Part 2)

 Repetition Control Structures


Types of Repetition Control Structures:
 While
1. While
 Do-While
2. Do-While
 For
 Branching Statements 3. For
 Break
 Continue
 Return

Example:
A program that prints and increments the value of an int counter variable if
the value of the counter is less than/equal to 10:

public class intCounter1


Syntax: {
public static void main (String[] args)
while ([boolean_expression]) {
{ int counter = 1;
[statement1] while (counter <= 10)
{
[statement2]
System.out.println(“counter =“ + counter);
… counter++;
} }
}
}
Example:
A program that prints and increments the value of an int counter variable
if the value of the counter is less than/equal to 10:

public class intCounter2


Syntax: {
do { public static void main (String[] args)
{
[statement1]
int counter = 1;
[statement2] do
… {
} while ([boolean_expression]); System.out.println(“counter =“ + counter);
counter++;
} while (counter <= 10);
}
}

Example:
A program that prints and increments the value of an int counter variable
if the value of the counter is less than/equal to 10:

Syntax: public class intCounter3


for {
([initialization];[termination];[increment]) public static void main (String[] args)
{ {
for (int counter=3;counter<=10;counter++)
[statement1]
{
...
System.out.println(“counter =“ + counter);
} }
}
}
Example:
for(int counter=3;counter<=10;counter++)
Types of Branching Statements in Java:
{
1. Break
System.out.println(“counter =“ +
2. Continue counter);
3. Return if (counter == 5) break;
}

Example:
Example: for (int counter=3;counter<=10;counter++)
for (int counter=3;counter<=10;counter++)
{
{
if (counter == 5){
if (counter == 5) continue;
System.out.println(“counter =“ +
System.out.println(“counter =“ + counter);
counter);
return;
}
}
}
Computer Programming (JAVA) – Week 6 Page 1 of 2
Computer Programming (JAVA) – Week 6 Page 2 of 2

You might also like