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

Unit-I Introduction To Control Statements

exploring different control statements in java
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)
12 views

Unit-I Introduction To Control Statements

exploring different control statements in java
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/ 22

LAKIREDDY BALI REDDY COLLEGE OF

ENGINEERING

Object Oriented Programming


By

S.V.V.D.Jagadeesh
Sr. Assistant Professor
Dept of Artificial Intelligence & Data Science
LBRCE

Previous Discussion
Operator-Definition, Types of Operators
Arithmetic Operators
Assignment Operators
Relational/Equality Operators
Logical Operators
Bitwise and Shift Operator
Instance of operator
Precedence & Associativity of Operators

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Session Outcomes

At the end of this session, Student will be able to:


 CO1: Apply different control statements for solving problems. (Apply- L3)

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

If Statement
The if statement is Java’s conditional branch statement. It can be used to route
program execution through two different paths.
When the condition is true the Statement within the if is executed. After that
execution continues with the next statement after the if statement.
If the condition is false then the statement within the if is not executed and the
execution continues with the statement after the if statement.
Syntax:
If(condition){
 Block of statements;
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

If Statement
The if statement is Java’s conditional branch statement. It can be used to route
program execution through two different paths.
When the condition is true the Statement within the if is executed. After that
execution continues with the next statement after the if statement.
If the condition is false then the statement within the if is not executed and the
execution continues with the statement after the if statement.
Syntax:
If(condition){
 Block of statements;
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

If Statement
• Write a java Program to print the Largest among three numbers using if Statement?

import java.util.Scanner; x2=s.nextInt();


class larger1 System.out.println("Enter
{ value for x3 : ");
public static void main(String args[])
{
x3=s.nextInt();
int x1,x2,x3; large = x1;
int large; if (x2 > large)
Scanner s = new Scanner(System.in); large = x2;
System.out.println("Enter value for if (x3 > large)
x1 : "); large = x3;
x1=s.nextInt();
System.out.println("Enter value for
System.out.println("\n\n\
x2 : "); tLargest number = " + large);
}
}
OOP S.V.V.D.Jagadeesh Friday, July 26, 2024
LBRCE

if-else statement
Syntax:
if (condition) Write a java program to check whether the given mark is pass or
{ fail using if-else statement?
statement1; import java.util.Scanner;
} class result
else {
public static void main(String args[])
{
{
statement2; Scanner s = new Scanner(System.in);
} System.out.println("Enter marks : ");
int marks = s.nextInt();
if (marks<40)
System.out.println("\nThe student has failed .. ");
else
System.out.println("\nThe student has Passed .. ");
}
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

if-else-if ladder
Syntax :
if(condition)
statements;
else if(condition)
statemenst;
else if(condition)
statements;
...
...
else
statements;
Write a java program to accept the day between 0 to 6 based on that it has to print
day between Sunday to Saturday using if-else-if Ladder.

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

if-else-if ladder
import java.util.Scanner;
class Day else if (day == 3)
{ {
public static void main(String args[]) System.out.println("\n Wednesday");
{ }
Scanner s = new Scanner(System.in);
System.out.println("Enet day between 0 to 6 Day = else if (day == 4)
"); {
int day = s.nextInt(); System.out.println("\n Thursday");
if (day == 0) }
{ else if (day == 5)
System.out.println("\n Sunday"); {
} System.out.println("\n Friday");
else if (day == 1) }
{ else
System.out.println("\n Monday"); {
} System.out.println("\n Saturday");
else if (day == 2) }
{ }
System.out.println("\n Tuesday"); }
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Nested if
Syntax :
if(condition)
{
if(condition)
statements....
else
statements....
}
else
{
if(condition)
statements....
else
statements....
}

Write a java program to accept three values a, b, c from the user and find out the
maxvalue using Nested-if.

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Nested if
if (a>b)
{
import java.util.Scanner; if(a>c)
class MaxValue max=a;
{ else //This else is associate with this
public static void main(String args[]) if(a>c)
{ max=c;
int a,b,c; }else //This else is associate with this
if(a>b)
int max=0;
{
Scanner s = new Scanner(System.in); if(b>c)
System.out.println("Enter value for a : "); max=b;
a=s.nextInt(); else //This else is associate with this
System.out.println("Enter value for b : "); if(b>c)
b=s.nextInt(); max=c;
System.out.println("Enter value for c : "); }
System.out.println("\n max value = " +max);
c=s.nextInt();
}
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Switch Statement
The switch statement is Java’s multiway branch statement. It
provides an easy way to dispatch execution to different parts of
your code based on the value of an expression.
Syntax
switch (expression)
{
case value 1 :
statement 1 ; break;
case value 2 :
statement 2 ; break;
...
...
case value N :
statement N ; break;
default :
statements ; break;
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Switch Statement
switch(ch)
import java.util.Scanner; {
public class Calci case 1:
ans=a+b;
{ System.out.println("a + b = " + ans);
public static void main(String[] args) break;
{ case 2:
int a,b,ch; ans=a-b;
System.out.println("a - b = " + ans);
double ans; break;
Scanner s = new Scanner(System.in); case 3:
System.out.print("Enter a : "); ans=a*b;
a=s.nextInt(); System.out.println("a * b = " + ans);
break;
System.out.print("Enter b : ");
case 4:
b=s.nextInt(); ans=a/b;
System.out.println("Enter 1 for addition"); System.out.println("a / b = " + ans);
System.out.println("Enter 2 for subtraction"); break;
System.out.println("Enter 3 for multiplication"); default:
System.out.println("Enter correct choice");
System.out.println("Enter 4 for division"); }
System.out.print("Enter your choice : ");
ch=s.nextInt(); }
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Looping statements-while
Loop repeatedly executes the same set of instructions until a
termination condition is met. Write a java program to print the natural
number from 1 to 10 using while loop?
While Loop Syntax:
class while1
{
while (condition) public static void main(String args[])
{ {
body(statements) of int i=1;
while(i<=10)
the loop {
} System.out.println("\n" + i);
i++;
}
}
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Do-While
• Syntax Write a java program to print the natural
number from 10 to 1 using do while loop?
class dowhile1
do {
{ public static void main(String args[])
{
body of the loop int i=10;
} while (condition); do
{
System.out.println("\n" + i);
i--;
}
while(i>=1);
}
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

for loop
 The for loop repeats a set of statements a certain number of times until a
condition is matched.
Syntax Write a java program to print the numbers
from 1 to 9 using for loop?
class forLoop1
for (initialization; {
condition; expression) public static void main(String args[])
{
{ int i;
Set of statements; for (i=0;i<10;i++)
} {
System.out.println("\n"+i);
}
}
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

for-each
 It provides an alternative approach to traverse the array or collection in Java. It
is mainly used to traverse the array or collection elements.
Syntax
Write a java program to print the numbers
from 1 to 9 using for-each loop?
for (datatype class forLoop1
{
variable:array|collection) public static void main(String args[])
{ {
Set of statements; int a[]={1,2,3,4,5};
for (int i:a)
} {
System.out.println(i);
}
}
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Break Statement
This statement is used to jump out of a loop.
Break statement was previously used in switch – case
statements.
On encountering a break statement within a loop, the
execution continues with the next statement outside the
loop.
The remaining statements which are after the break and
within the loop are skipped.
Break statement can also be used with the label of a
statement.

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Break Statement
class break1
{
public static void main(String args[])
{
int i = 1;
while (i<=10)
{
System.out.println("\n" + i);
i++;
if (i==5)
{
break;
}
}
}
}

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Continue Statement
This statement is used only within looping statements.
When the continue statement is encountered, the next
iteration starts.
 The remaining statements in the loop are skipped. The
execution starts from the top of loop again.

OOP S.V.V.D.Jagadeesh Friday, July 26, 2024


LBRCE

Continue Statement
class continue1
{
public static void main(String args[])
{
for (int i=1; i<=10; i++)
{
if (i%2 == 0)
continue;

System.out.println("\n" + i);
}
}
}
OOP S.V.V.D.Jagadeesh Friday, July 26, 2024
LBRCE

Summary
Conditional Statements-If Statement
If-Else statement
If-Else-If ladder Statement
Nested if statement
Switch Statement
Lopping statements-while loop
Do-while loop
For loop
For-each loop
Break statement
Continue Statement
OOP S.V.V.D.Jagadeesh Friday, July 26, 2024

You might also like