0% found this document useful (0 votes)
309 views

Loops

The document discusses control flow statements in programming languages. It explains that programs make decisions based on the state of data, and then describes common control structures like if/else statements, loops (while, do-while, for), and switch statements. Examples are provided for each type of control structure to illustrate their syntax and usage.

Uploaded by

Anik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
309 views

Loops

The document discusses control flow statements in programming languages. It explains that programs make decisions based on the state of data, and then describes common control structures like if/else statements, loops (while, do-while, for), and switch statements. Examples are provided for each type of control structure to illustrate their syntax and usage.

Uploaded by

Anik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

The first step in learning to use a new programming

language is usually to learn the foundation concepts


such as variables, types, flow-of-control, etc.
 Java programs accomplish their tasks by
manipulating program data using
operators and making decisions by testing
the state of program data. When a
program makes a decision, it is
determining, based on the state of the
program data, whether certain lines of
code should be executed.
Ca tegory Keywo rd

Selection if ,if-else if, switch

Loop While, do-While, for

Branch break, continue


 statement_keyword
(conditional_expression)
{ statements_to_execute}
 Here are some examples of control
statements.
 //Example 1

If (conditional_expression)
{
statements_to_execute
}
 //Example 2
 If (conditional_expression)

{
statements_to_execute
}
else
{
statements_to_execute
}
 //Example 1
if(a == b)
{
c++;
}

 In example 1, the if tests the equality of a and b. If a and b


are equal, c is incremented by 1.

 //Example 2
if((x <= y) && (y <= z))
{ System.out.println("Of course:\n"); System.out.println("x
is less than or equal to z");}
 //Example 2
if(a == b)
{
c++;
}
else
{
c--;
}
 Example
if( color==green) {
System.out.println(“color is green”);
}
else if(color==yellow) {
System.out.println(“color is yellow”);
}
else {
System.out.println(“color unknown”);
}
//do while loop
do{
Statements_to_execute
}while(conditional_expression);

// while loop
while(conditional_expression)
{
statements_to_execute
}

//for loop
for(int i = 0; conditional_expression; i++)
{
statements_to_execute
}
 A switch works with the byte, short, char,
and int primitive data types
 Each case value must be a unique literal

(that is, it must be a constant, not a


variable).
 Duplicate case values are not allowed.
 class SwitchDemo {
public static void main(String[] args) {
int month = 8;
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
default: System.out.println("Invalid month.");break;
}
}
}
 Breaks out of a switch statement.
 Terminates a loop.

Class breakloop{
public static void main(String args[]) {
for(int i=0; i<10; i++) {
if(i==5) break; //terminate loop when i=5
System.out.println(“i:” +i);
}
System.out.println(“Loop Completed”);
}
}
i: 0
i: 1
i: 2
i: 3
i: 4
Loop Completed
 Sometimes we do not need to execute
some statements under the loop, then we
use the continue statement that stops the
normal flow of the control and control
returns to the loop without executing the
statements written after the continue
statement.
class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
System.out.print( i + " ");
if(i%2==0) continue;
System.out.println(" ");
}
}
}
Output:
01
23
45
67
89

You might also like