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

4-Control and Looping Constructs-07!08!2023

Uploaded by

msd77sy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

4-Control and Looping Constructs-07!08!2023

Uploaded by

msd77sy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

BCSE103E COMPUTER PROGRAMMING - JAVA

Dr. S. Vinila Jinny


03/08/2022
Module – 2
Looping Constructs and Array

Control and looping constructs – Arrays –


one dimensional and multi-dimensional-
enhanced for loop-Strings-Wrapper Classes.
Control and Looping Constructs – 3 kinds
Control structures are programming blocks that can change
the path we take through execution of instructions.
• Conditional Branches, which we use for choosing between two
or more paths.
if/else/else if, ternary operator and switch.
• Loops that are used to iterate through multiple values/objects
and repeatedly run specific code blocks.

for, while and do while.


• Branching Statements, which are used to alter the flow of
control in loops.
break and continue.
Decision Making Statements – Conditional Branches

• Statements that determine which statement to execute and when are


known as decision-making statements.
• Simple if statement
• If..else statement
• If…else if…else statement
• Nested if statement
• Switch statement
• Ternary operator
if statement
An if statement consists of a boolean expression followed by
one or more statements.
Syntax
if (Boolean_expression)
{ // Statements will execute if the Boolean expression is true }
• If the Boolean expression evaluates to true then the block of
code inside the if statement will be executed.
• If not, the first set of code after the end of the if statement
(after the closing curly brace) will be executed.
if else statement
An if statement can be followed by an optional else
statement, which executes when the boolean expression is
false.
Syntax
if(Boolean_expression)
{ // Executes when the Boolean expression is true }
else { // Executes when the Boolean expression is false }
if...else if...else Statement
Syntax
if(Boolean_expression 1)
{ // Executes when the Boolean expression 1 is true }
else if(Boolean_expression 2)
{ // Executes when the Boolean expression 2 is true }
else if(Boolean_expression 3)
{ // Executes when the Boolean expression 3 is true }
else { // Executes when the none of the above condition is
true. }
nested if statement
one if or else if statement inside another if or else
if statement(s).

Syntax
if(Boolean_expression 1)
{ // Executes when the Boolean expression 1 is true
if(Boolean_expression 2)
{ // Executes when the Boolean expression 2 is true }
}
switch statement
A switch statement allows a variable to be tested for equality
against a list of values.
switch(expression)
{
case value : // Statements
break;
// optional case value : // Statements
break; // You can have any number of case statements.
default : // Optional // Statements
}
The ? : Operator

Can be used in replacement of if else statements.


Syntax:
Exp1 ? Exp2 : Exp3;
• Where Exp1, Exp2, and Exp3 are expressions.
• To determine the value of the whole expression, initially
exp1 is evaluated.
• If the value of exp1 is true, then the value of Exp2 will be
the value of the whole expression.
• If the value of exp1 is false, then Exp3 is evaluated and its
Switch Statement -Example

public class Test {


case 'D' :
public static void main(String System.out.println("You
args[]) { passed");
// char grade = args[0].charAt(0); case 'F' :
char grade = 'C';
System.out.println("Better try
switch(grade) { again");
case 'A' : break;
default :
System.out.println("Excellent!");
break; System.out.println("Invalid
case 'B' : grade");
case 'C' : }
System.out.println("Well System.out.println("Your
done"); grade is " + grade);
break; }
}
JAVA – Loop Control

• A loop statement allows us to execute a statement or group of


statements multiple times
JAVA – Loop Control

while loop
• Repeats a statement or group of statements while a given condition
is true. It tests the condition before executing the loop body.
for loop
• Execute a sequence of statements multiple times and abbreviates the
code that manages the loop variable.
do...while loop
• Like a while statement, except that it tests the condition at the end
of the loop body.
While Loop

while(Boolean_expression) { // Statements }

public class Test {


Output:
public static void main(String args[]) { value of x : 10
value of x : 11
int x = 10;
value of x : 12
while( x < 20 ) { value of x : 13
value of x : 14
System.out.print("value of x : " + x ); value of x : 15
value of x : 16
x++;
value of x : 17
System.out.print("\n"); value of x : 18
value of x : 19
} }}
For Loop

for(initialization; Boolean_expression; update) {


// Statements }
public class Test
Output:
{ value of x : 10
value of x : 11
public static void main(String args[])
value of x : 12
{ value of x : 13
value of x : 14
for(int x = 10; x < 20; x = x + 1) value of x : 15
value of x : 16
{
value of x : 17
System.out.print("value of x : " + x ); value of x : 18
value of x : 19
System.out.print("\n");
}}}
Do..while Loop

do { // Statements }while(Boolean_expression);
public class Test
{ public static void main(String args[])
Output:
{ int x = 10; value of x : 10
do value of x : 11
value of x : 12
{ value of x : 13
System.out.print("value of x : " + x ); value of x : 14
value of x : 15
x++;
value of x : 16
System.out.print("\n"); value of x : 17
value of x : 18
}while( x < 20 );
value of x : 19
}}
Loop Control Statements

break statement
• Terminates the loop or switch statement and transfers execution to
the statement immediately following the loop or switch.
continue statement
• Causes the loop to skip the remainder of its body and immediately
retest its condition prior to reiterating.
Enhanced for loop in Java

• traverse collection of elements including arrays.


for(declaration : expression)
{ // Statements }
For Each - Example String [] names = {"James",
"Larry", "Tom", "Lacy"};
public class Test {

public static void main(String args[])


for( String name : names ) {
{ System.out.print( name );
int [] numbers = {10, 20, 30, 40, 50};
System.out.print(",");
for(int x : numbers )
{ }
System.out.print( x ); }
System.out.print(",");
}
}
System.out.print("\n");

You might also like