1 of 22

Object Oriented Programming

By

​

S.V.V.D.Jagadeesh

Sr. Assistant Professor

Dept of Artificial Intelligence & Data Science

​

​

LAKIREDDY BALI REDDY COLLEGE OF ENGINEERING

2 of 22

  • 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

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Previous Discussion

LBRCE

OOP

3 of 22

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

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

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Session Outcomes

LBRCE

OOP

4 of 22

  • 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;
  • }

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

If Statement

LBRCE

OOP

5 of 22

  • 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;
  • }

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

If Statement

LBRCE

OOP

6 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

If Statement

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

import java.util.Scanner;

class larger1

{

        public static void main(String args[])

        {

                int x1,x2,x3;

                int large;

                Scanner s = new Scanner(System.in);

                System.out.println("Enter value for x1 : ");

                x1=s.nextInt();

                System.out.println("Enter value for x2 : ");

              

​

  x2=s.nextInt();

                System.out.println("Enter value for x3 : ");

                x3=s.nextInt();

                large = x1;

                if (x2 > large)

                        large = x2;

                if (x3 > large)

                        large = x3;

                System.out.println("\n\n\tLargest number = " + large);

        }

}

LBRCE

OOP

7 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

if-else statement

​

Syntax:

if (condition)

{

    statement1;

}

else

{

    statement2;

}

​

​

Write a java program to check whether the given mark is pass or fail using if-else statement?

import java.util.Scanner;

class result

{

        public static void main(String args[])

        {

                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 .. ");

        }

}

​

LBRCE

OOP

8 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

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.

​

LBRCE

OOP

9 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

if-else-if ladder

else if (day == 3)

{

System.out.println("\n Wednesday");

}

​

else if (day == 4)

{

System.out.println("\n Thursday");

}

else if (day == 5)

{

System.out.println("\n Friday");

}

else

{

System.out.println("\n Saturday");

}

}

}

import java.util.Scanner;

class Day

{

public static void main(String args[])

{

Scanner s = new Scanner(System.in);

System.out.println("Enet day between 0 to 6 Day = ");

int day = s.nextInt();

if (day == 0)

{

System.out.println("\n Sunday");

}

else if (day == 1)

{

System.out.println("\n Monday");

}

else if (day == 2)

{

System.out.println("\n Tuesday");

}

LBRCE

OOP

10 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

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.

​

LBRCE

OOP

11 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Nested if

import java.util.Scanner;

class MaxValue

{

public static void main(String args[])

{

int a,b,c;

int max=0;

Scanner s = new Scanner(System.in);

System.out.println("Enter value for a : ");

a=s.nextInt();

System.out.println("Enter value for b : ");

b=s.nextInt();

System.out.println("Enter value for c : ");

c=s.nextInt();

if (a>b)

{

if(a>c)

max=a;

else //This else is associate with this if(a>c)

max=c;

}else //This else is associate with this if(a>b)

{

if(b>c)

max=b;

else //This else is associate with this if(b>c)

max=c;

}

System.out.println("\n max value = " +max);

}

}

LBRCE

OOP

12 of 22

  • 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

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Switch Statement

switch (expression) 

{

case value 1 : 

statement 1 ; break; 

case value 2 : 

statement 2 ; break;  

...

...

case value N : 

statement N ; break;

default : 

statements ; break; 

}

LBRCE

OOP

13 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Switch Statement

import java.util.Scanner;

public class Calci

{

public static void main(String[] args)

{

int a,b,ch;

double ans;

Scanner s = new Scanner(System.in);

System.out.print("Enter a : ");

a=s.nextInt();

System.out.print("Enter b : ");

b=s.nextInt();

System.out.println("Enter 1 for addition");

System.out.println("Enter 2 for subtraction");

System.out.println("Enter 3 for multiplication");

System.out.println("Enter 4 for division");

System.out.print("Enter your choice : ");

ch=s.nextInt();

switch(ch)

{

case 1:

ans=a+b;

System.out.println("a + b = " + ans);

break;

case 2:

ans=a-b;

System.out.println("a - b = " + ans);

break;

case 3:

ans=a*b;

System.out.println("a * b = " + ans);

break;

case 4:

ans=a/b;

System.out.println("a / b = " + ans);

break;

default:

System.out.println("Enter correct choice");

}

}

}

LBRCE

OOP

14 of 22

  • Loop repeatedly executes the same set of instructions until a termination condition is met.
  • While Loop Syntax:

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Looping statements-while

while (condition)

{

      body(statements) of the loop

}

Write a java program to print the natural number from 1 to 10 using while loop?

class while1

{

        public static void main(String args[])

        {

                int i=1;

                while(i<=10)

                {

                        System.out.println("\n" + i);

                        i++;

                }

        }

}

​

LBRCE

OOP

15 of 22

  • Syntax

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Do-While

do

{

      body of the loop

} while (condition);

Write a java program to print the natural number from 10 to 1 using do while loop?

class dowhile1

{

public static void main(String args[])

{

int i=10;

do

{

System.out.println("\n" + i);

i--;

}

while(i>=1);

}

}

LBRCE

OOP

16 of 22

  • The for loop repeats a set of statements a certain number of times until a condition is matched.
  • Syntax

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

for loop

for (initialization; condition; expression)

{

        Set of statements;

}

Write a java program to print the numbers from 1 to 9 using for loop?

class forLoop1

{

public static void main(String args[])

{

int i;

for (i=0;i<10;i++)

{

System.out.println("\n"+i);

}

}

}

LBRCE

OOP

17 of 22

  • 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

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

for-each

for (datatype variable:array|collection)

{

        Set of statements;

}

Write a java program to print the numbers from 1 to 9 using for-each loop?

class forLoop1

{

public static void main(String args[])

{

int a[]={1,2,3,4,5};

for (int i:a)

{

System.out.println(i);

}

}

}

LBRCE

OOP

18 of 22

  • 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.

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Break Statement

LBRCE

OOP

19 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

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;

}

}

}

}

LBRCE

OOP

20 of 22

  • 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.

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

Continue Statement

LBRCE

OOP

21 of 22

S.V.V.D.Jagadeesh

Friday, September 1, 2023

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);

}

}

}

LBRCE

OOP

22 of 22

​

​

​

S.V.V.D.Jagadeesh

Friday, September 1, 2023

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

LBRCE

OOP