SlideShare a Scribd company logo
1
Java Programming
Statement and loop
2
Control Statement
________________________________________
If Else Statement
In Java, if statement is used for testing the conditions. The condition matches the statement it returns true
else it returns false. There are four types of If statement they are:
i. if statement
ii. if-else statement
iii. if-else-if ladder
iv. nested if statement
if Statement
In Java, if statement is used for testing conditions. It is used for only true condition.
Syntax:
if(condition)
{
//code
}
3
Example:
public class IfDemo1 {
public static void main(String[ ] args)
{
int marks=70;
if(marks > 65)
{
System.out.print("First division");
}
}
}
4
if-else Statement
In Java, the if-else statement is used for testing conditions. It is used for true as well as for false condition.
Syntax:
if(condition)
{
//code for true
}
Else
{
//code for false
}
public class IfElseDemo1 {
public static void main (String[] args)
{
5
int marks=50;
if (marks > 65)
{
System.out.print("First division");
}
else
{
System.out.print("Second division");
}
}
}
6
if-else-if ladder Statement
In Java, the if-else-if ladder statement is used for testing conditions. It is used for testing one condition from multiple
statements.
Syntax:
if(condition1)
{
//code for if condition1 is true
}
else if(condition2)
{
//code for if condition2 is true
}
else if(condition3)
{
//code for if condition3 is true
}
...
else
{
//code for all the false conditions
}
7
Nested if statement
In Java, the Nested if statement is used for testing conditions. In this, one if block is created inside another if block when the
outer block is true then only the inner block is executed.
Syntax:
if(condition)
{
//statement
if(condition)
{
//statement
}
}
8
Example:
public class NestedIfDemo1 {
public static void main(String[] args)
{
int age=25;
int weight=70;
if(age>=18)
{
if(weight>50)
{
System.out.println("You are eligible");
}
}
}
}
9
Nested try block
In Java, there can be multiple try blocks. A try block within another try block is known as nested try block. In a program
when any block of code can cause an error and the entire program can cause another error then try blocks are made
try
{
statements
try
{
Statements
try
{
statements
}
catch (Exception e)
{
}
10
}
catch(Exception e)
{
}
}
catch(Exception e)
{
}
Example
class NestedDemo1
{
public static void main(String args[])
{
try{
11
try{
System.out.println("Divide 39/0");
int b =39/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
} try{
int a[]=new int[8];
a[8]=4;
}
12
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
13
For Loop
In Java, for loop is used for executing a part of the program again and again. When the number of execution is fixed then it is
suggested to use for loop. In java there are 3 types of for loops, they are as follows:
1. Simple for loop
2. For-each loop
3. labelled for loop
Syntax:
for(initialization; condition; increment/decrement)
{
//statement
}
14
Parameters:
1) Initialization
It is the initial condition which is executed only once at the starting of a loop. It is an optional condition.
2) Condition
It is used to test a condition each time while executing. The execution continues until the condition is false. It is an optional
condition.
3) Statement
It is executed every time until the condition is false.
4) Increment/Decrement
It is used for incrementing and decrementing the value of a variable. It is an optional condition.
15
Example for simple For loop
public class ForDemo1
{
public static void main(String[] args)
{
int n, i;
n=2;
for(i=1;i<=10;i++)
{
System.out.println(n+"*"+i+"="+n*i);
}
}
}
16
Example for Nested for loop
public class ForDemo2
{
public static void main(String[]args)
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print("* ");
}
System.out.println();
} }
}
17
for-each Loop
In Java, for each loop is used for traversing array or collection. In this loop, there is no need for increment or decrement
operator.
Syntax:
for(Type var:array)
{
//code for execution
}
Example:
public class ForEachDemo1
{
public static void main(String[] args)
{
Int a[]={20,21,22,23,24};
for(int i:a)
18
{
System.out.println(i);
}
}
}
Labelled For Loop
In Java, Labelled For Loop is used to give label before any for loop. It is very useful for nesting for loop.
Syntax:
labelname :
for(initialization; condition; incr/decr)
{
//code for execution
}
19
Example:
public class LabeledForDemo1
{
public static void main(String[] args)
{
num:
for(int i=1;i<=5;i++)
{
num1:
for(int j=1;j<=5;j++)
{
if(i==2&&j==2)
{
break num;
}
System.out.println(i+" "+j);
}
}
}
}
20
Switch Statement
In Java, the switch statement is used for executing one statement from multiple conditions. it is similar to an if-else-if ladder. In
a switch statement, the expression can be of byte, short, char and int data types. From JDK7 enum, String class and the
Wrapper classes can also be used. Following are some of the rules while using the switch statement:
1. There can be one or N numbers of cases.
2. The values in the case must be unique.
3. Each statement of the case can have a break statement. It is optional.
Syntax:
switch(expression)
{
case value1:
//code for execution;
break; //optional
case value2:
// code for execution
21
break; //optional
......
......
......
Case value n:
// code for execution
break; //optional
default:
code for execution when none of the case is true;
}
22
While Loop
In Java, While loop is a control statement. It is used for iterating a part of the program several times. When the number of
iteration is not fixed then while loop is used.
Syntax:
while(condition)
{
//code for execution
}
Example:
public class WhileDemo1
{
public static void main(String[] args)
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
23
Example for infinite while loop
public class WhileDemo2
{
public static void main(String[] args)
{
while(true)
{
System.out.println("infinitive while loop");
}
}
}
24
do-while loop
In Java, the do-while loop is used to execute a part of the program again and again. If the number of iteration is not fixed then
the do-while loop is used. This loop executes at least once because the loop is executed before the condition is checked.
Syntax:
do
{
//code for execution
}while(condition);
Example:
public class DoWhileDemo1
{
public static void main(String[] args)
{
Int i=1;
do {
System.out.println(i);
i++;
}while(i<=10);
}
}
25
Break Statement
In Java, a break statement is used inside a loop. The loop is terminated immediately when a break statement is encountered
and resumes from the next statement.
Syntax:
jump-statement;
break;
Example:
public class BreakDemo1 {
public static void main(String[] args) {
for(int i=1;i<=10;i++){
if(i==8){
break;
}
System.out.println(i);
}
}
}
26
Example using break in do while loop
public class BreakDoWhileDemo1
{
public static void main(String[] args)
{
int i=1;
do
{
if(i==15)
{
i++;
break;
}
System.out.println(i);
i++;
}while(i<=20);
}
}
27
continue Statement
In Java, the Continue statement is used in loops. Continue statement is used to jump to the next iteration of the loop
immediately. It is used with for loop, while loop and do-while loop.
jump-statement;
continue;
Example:
public class ContinueDemo1
{
public static void main(String[] args)
{
for(int i=1; i<=10; i++)
{
if(i==5)
{
continue;
}
System.out.println(i);
} } }
28
Example:
public class ContinueDemo2 {
public static void main(String[] args) {
xy:
for(int i=1;i<=5;i++){
pq:
for(int j=1;j<=5;j++){
if(i==2&&j==2){
continue xy;
}
System.out.println (i+" "+j);
}
} }
}
29
Java return Keyword
Java return keyword is used to complete the execution of a method. The return followed by the appropriate value that is
returned to the caller. This value depends on the method return type like int method always return an integer value.
Points to remember
o It is used to exit from the method.
o It is not allowed to use return keyword in void method.
o The value passed with return keyword must match with return type of the method.
Example
public class ReturnExample1 {
int display()
{
return 10;
}
30
public static void main(String[] args) {
ReturnExample1 e =new ReturnExample1();
System.out.println(e.display());
}
}
Output
10
31
Example
public class ReturnExample2 {
void display ()
{
return null;
}
public static void main (String [] args) {
ReturnExample2 e =new ReturnExample2();
e. display ();
}
}
Output:
Void methods cannot return a value
32
/* Java Program Example - Interchange Two Numbers */
import java.util.Scanner;
public class swap
{
public static void main(String args[])
{
int a, b, temp;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Value of A and B :n");
System.out.print("A = ");
a = scan.nextInt();
System.out.print("B = ");
b = scan.nextInt();
temp = a;
a = b;
b = temp;
System.out.print("Number Interchanged Successfully..!!n");
System.out.print("A = " +a);
System.out.print("nB = " +b);
}
}
33
/* Java Program Example - Convert Fahrenheit to Centigrade */
import java.util.Scanner;
public class fahren
{
public static void main(String args[])
{
float fah;
double cel;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Temperature in Fahrenheit : ");
fah = scan.nextFloat(); //input 68
cel = (fah-32) / 1.8;
System.out.print("Equivalent Temperature in Celsius = " + cel); // 20.0
}
}
34
/* Java Program Example - Print Table of Number */
import java.util.Scanner;
public class table
{
public static void main(String args[])
{
int num, i, tab;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Number : ");
num = scan.nextInt(); //5
System.out.print("Table of " + num + " isn");
for(i=1; i<=10; i++)
{
tab = num*i;
System.out.print(num + " * " + i + " = " + tab + "n");
}
}
}
35
// creation of table of 2 through command line
import java.io.*;
class table2{
static int sum=0; // static is required
static int t=2,p;
public static void main(String args[])
{
System.out.println(args[0]); // pass the base address i:e args[0]
for(int i=1;i<=Integer.parseInt(args[0]);i++) // convert string args[0] to integer value
{
p=i*t;
System.out.println(i + "*" + t + "=" +p);
}
}
}
// input java table2 10 output is tableof 2 i:e 2 4 6 8 10--------=20
36
/* Java Program Example - Calculate Arithmetic Mean */
import java.util.Scanner;
public class mean
{
public static void main(String args[])
{
int n, i, sum=0, armean;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);
System.out.print("How many Number you want to Enter ?
");
n = scan.nextInt();
System.out.print("Enter " +n+ " Numbers : ");
for(i=0; i<n; i++)
{
arr[i] = scan.nextInt();
sum = sum + arr[i];
37
}
armean = sum/n;
System.out.print("Arithmetic Mean = " +armean);
}
}
// WAP to create Fibonacci series
import java.util.*;
public class fibo
{
public static void main (String[] args)
{
fibo fs=new fibo();
38
fs.fibonacci();
}
public void fibonacci()
{
int numb1 = 1;
int numb2 = 1;
int temp = 0;
Scanner input=new Scanner(System.in);
System.out.println("How Many Terms? (Up To 45)");
int x=input.nextInt();
x=x-2;
39
System.out.println (numb1);
System.out.println (numb2);
for (int i = 0; i < x; i++)
{
System.out.println(numb1 + numb2 + " ");
temp = numb1;
numb1 = numb2;
numb2 = temp + numb2;
}
}
}
40
// Java Program Example WAP enter character and Check Alphabet or Not
import java.util.Scanner;
public class alphabet
{
public static void main(String args[])
{
char ch;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Character : ");
ch = scan.next().charAt(0); // enter character x
if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
{
System.out.print(ch + " is an Alphabet");
}
else
{
System.out.print(ch + " is not an Alphabet");
}
}
}
41
/* Java Program Example - Generate Armstrong Numbers */
import java.util.Scanner;
public class armsgen
{
public static void main(String args[])
{
int num1, num2, i, n, rem, temp, count=0;
Scanner scan = new Scanner(System.in);
/* enter the interval (two number) */
System.out.print("Enter the Interval :n");
System.out.print("Enter Starting Number : ");
num1 = scan.nextInt(); //1
System.out.print("Enter Ending Number : ");
num2 = scan.nextInt(); //160
42
for(i=num1+1; i<num2; i++)
{
temp = i;
n = 0;
while(temp != 0)
{
rem = temp%10;
n = n + rem*rem*rem;
temp = temp/10;
}
if(i == n)
{
if(count == 0)
{
43
System.out.print("Armstrong Numbers Between the Given Interval are :n");
}
System.out.print(i + " "); // output 153
count++;
}
}
if(count == 0)
{
System.out.print("Armstrong Number not Found between the Given Interval.");
}
}
}
44
/* Java Program Example - Find Largest of Three Numbers */
import java.util.Scanner;
public class largest
{
public static void main(String args[])
{
int a, b, c, big;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Three Numbers : ");
a = scan.nextInt();
b = scan.nextInt();
c = scan.nextInt();
// let a is the largest
big = a;
if(big<b)
{
45
if(b>c)
{
big = b;
}
else
{
big = c;
}
}
else if(big<c)
{
if(c>b)
{
big = c; }
46
{
big = b;
}
}
else
{
big = a;
}
System.out.print("Largest Number is " +big);
}
}

More Related Content

What's hot

JMockit Framework Overview
JMockit Framework OverviewJMockit Framework Overview
JMockit Framework Overview
Mario Peshev
 
Java unit3
Java unit3Java unit3
Java unit3
Abhishek Khune
 
Fnt software solutions placement paper
Fnt software solutions placement paperFnt software solutions placement paper
Fnt software solutions placement paper
fntsofttech
 
Java Programs
Java ProgramsJava Programs
Java Programs
vvpadhu
 
Interface
InterfaceInterface
Interface
vvpadhu
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
Nalinee Choudhary
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
Fraz Bakhsh
 
E:\Plp 2009 2\Plp 9
E:\Plp 2009 2\Plp 9E:\Plp 2009 2\Plp 9
E:\Plp 2009 2\Plp 9
Ismar Silveira
 
Operators
OperatorsOperators
Operators
vvpadhu
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
Kuppusamy P
 
Java language fundamentals
Java language fundamentalsJava language fundamentals
Java language fundamentals
Kapish Joshi
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Kapish Joshi
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
Usama Malik
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
Upender Upr
 
Rechecking SharpDevelop: Any New Bugs?
Rechecking SharpDevelop: Any New Bugs?Rechecking SharpDevelop: Any New Bugs?
Rechecking SharpDevelop: Any New Bugs?
PVS-Studio
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
Tarunsingh198
 

What's hot (19)

JMockit Framework Overview
JMockit Framework OverviewJMockit Framework Overview
JMockit Framework Overview
 
Java unit3
Java unit3Java unit3
Java unit3
 
Fnt software solutions placement paper
Fnt software solutions placement paperFnt software solutions placement paper
Fnt software solutions placement paper
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Interface
InterfaceInterface
Interface
 
Nalinee java
Nalinee javaNalinee java
Nalinee java
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
E:\Plp 2009 2\Plp 9
E:\Plp 2009 2\Plp 9E:\Plp 2009 2\Plp 9
E:\Plp 2009 2\Plp 9
 
Operators
OperatorsOperators
Operators
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Java language fundamentals
Java language fundamentalsJava language fundamentals
Java language fundamentals
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Rechecking SharpDevelop: Any New Bugs?
Rechecking SharpDevelop: Any New Bugs?Rechecking SharpDevelop: Any New Bugs?
Rechecking SharpDevelop: Any New Bugs?
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 

Similar to Loop

Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
manish kumar
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
HongAnhNguyn285885
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
rohitgudasi18
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
PRN USM
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
TekobashiCarlo
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
Java PSkills Session-4 PNR.pptx
Java PSkills Session-4 PNR.pptxJava PSkills Session-4 PNR.pptx
Java PSkills Session-4 PNR.pptx
ssuser99ca78
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
Niloy Saha
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - ii
Ravindra Rathore
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Java performance
Java performanceJava performance
Java performance
Sergey D
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
Intro C# Book
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
RajkumarHarishchandr1
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
Марія Русин
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
AmbigaMurugesan
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 

Similar to Loop (20)

Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Java PSkills Session-4 PNR.pptx
Java PSkills Session-4 PNR.pptxJava PSkills Session-4 PNR.pptx
Java PSkills Session-4 PNR.pptx
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
17 exception handling - ii
17 exception handling - ii17 exception handling - ii
17 exception handling - ii
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Java performance
Java performanceJava performance
Java performance
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
java notes.pdf
java notes.pdfjava notes.pdf
java notes.pdf
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 

More from prabhat kumar

Class and object
Class and objectClass and object
Class and object
prabhat kumar
 
Inheritance
InheritanceInheritance
Inheritance
prabhat kumar
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
prabhat kumar
 
Data Mining
Data MiningData Mining
Data Mining
prabhat kumar
 
Decision making model
Decision making modelDecision making model
Decision making model
prabhat kumar
 
Intro to critical thinking
Intro to critical thinkingIntro to critical thinking
Intro to critical thinking
prabhat kumar
 

More from prabhat kumar (6)

Class and object
Class and objectClass and object
Class and object
 
Inheritance
InheritanceInheritance
Inheritance
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Data Mining
Data MiningData Mining
Data Mining
 
Decision making model
Decision making modelDecision making model
Decision making model
 
Intro to critical thinking
Intro to critical thinkingIntro to critical thinking
Intro to critical thinking
 

Recently uploaded

Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
HODECEDSIET
 

Recently uploaded (20)

Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEMTIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
TIME DIVISION MULTIPLEXING TECHNIQUE FOR COMMUNICATION SYSTEM
 

Loop

  • 2. 2 Control Statement ________________________________________ If Else Statement In Java, if statement is used for testing the conditions. The condition matches the statement it returns true else it returns false. There are four types of If statement they are: i. if statement ii. if-else statement iii. if-else-if ladder iv. nested if statement if Statement In Java, if statement is used for testing conditions. It is used for only true condition. Syntax: if(condition) { //code }
  • 3. 3 Example: public class IfDemo1 { public static void main(String[ ] args) { int marks=70; if(marks > 65) { System.out.print("First division"); } } }
  • 4. 4 if-else Statement In Java, the if-else statement is used for testing conditions. It is used for true as well as for false condition. Syntax: if(condition) { //code for true } Else { //code for false } public class IfElseDemo1 { public static void main (String[] args) {
  • 5. 5 int marks=50; if (marks > 65) { System.out.print("First division"); } else { System.out.print("Second division"); } } }
  • 6. 6 if-else-if ladder Statement In Java, the if-else-if ladder statement is used for testing conditions. It is used for testing one condition from multiple statements. Syntax: if(condition1) { //code for if condition1 is true } else if(condition2) { //code for if condition2 is true } else if(condition3) { //code for if condition3 is true } ... else { //code for all the false conditions }
  • 7. 7 Nested if statement In Java, the Nested if statement is used for testing conditions. In this, one if block is created inside another if block when the outer block is true then only the inner block is executed. Syntax: if(condition) { //statement if(condition) { //statement } }
  • 8. 8 Example: public class NestedIfDemo1 { public static void main(String[] args) { int age=25; int weight=70; if(age>=18) { if(weight>50) { System.out.println("You are eligible"); } } } }
  • 9. 9 Nested try block In Java, there can be multiple try blocks. A try block within another try block is known as nested try block. In a program when any block of code can cause an error and the entire program can cause another error then try blocks are made try { statements try { Statements try { statements } catch (Exception e) { }
  • 10. 10 } catch(Exception e) { } } catch(Exception e) { } Example class NestedDemo1 { public static void main(String args[]) { try{
  • 11. 11 try{ System.out.println("Divide 39/0"); int b =39/0; } catch(ArithmeticException e) { System.out.println(e); } try{ int a[]=new int[8]; a[8]=4; }
  • 13. 13 For Loop In Java, for loop is used for executing a part of the program again and again. When the number of execution is fixed then it is suggested to use for loop. In java there are 3 types of for loops, they are as follows: 1. Simple for loop 2. For-each loop 3. labelled for loop Syntax: for(initialization; condition; increment/decrement) { //statement }
  • 14. 14 Parameters: 1) Initialization It is the initial condition which is executed only once at the starting of a loop. It is an optional condition. 2) Condition It is used to test a condition each time while executing. The execution continues until the condition is false. It is an optional condition. 3) Statement It is executed every time until the condition is false. 4) Increment/Decrement It is used for incrementing and decrementing the value of a variable. It is an optional condition.
  • 15. 15 Example for simple For loop public class ForDemo1 { public static void main(String[] args) { int n, i; n=2; for(i=1;i<=10;i++) { System.out.println(n+"*"+i+"="+n*i); } } }
  • 16. 16 Example for Nested for loop public class ForDemo2 { public static void main(String[]args) { for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) { System.out.print("* "); } System.out.println(); } } }
  • 17. 17 for-each Loop In Java, for each loop is used for traversing array or collection. In this loop, there is no need for increment or decrement operator. Syntax: for(Type var:array) { //code for execution } Example: public class ForEachDemo1 { public static void main(String[] args) { Int a[]={20,21,22,23,24}; for(int i:a)
  • 18. 18 { System.out.println(i); } } } Labelled For Loop In Java, Labelled For Loop is used to give label before any for loop. It is very useful for nesting for loop. Syntax: labelname : for(initialization; condition; incr/decr) { //code for execution }
  • 19. 19 Example: public class LabeledForDemo1 { public static void main(String[] args) { num: for(int i=1;i<=5;i++) { num1: for(int j=1;j<=5;j++) { if(i==2&&j==2) { break num; } System.out.println(i+" "+j); } } } }
  • 20. 20 Switch Statement In Java, the switch statement is used for executing one statement from multiple conditions. it is similar to an if-else-if ladder. In a switch statement, the expression can be of byte, short, char and int data types. From JDK7 enum, String class and the Wrapper classes can also be used. Following are some of the rules while using the switch statement: 1. There can be one or N numbers of cases. 2. The values in the case must be unique. 3. Each statement of the case can have a break statement. It is optional. Syntax: switch(expression) { case value1: //code for execution; break; //optional case value2: // code for execution
  • 21. 21 break; //optional ...... ...... ...... Case value n: // code for execution break; //optional default: code for execution when none of the case is true; }
  • 22. 22 While Loop In Java, While loop is a control statement. It is used for iterating a part of the program several times. When the number of iteration is not fixed then while loop is used. Syntax: while(condition) { //code for execution } Example: public class WhileDemo1 { public static void main(String[] args) { int i=1; while(i<=10) { System.out.println(i); i++; } } }
  • 23. 23 Example for infinite while loop public class WhileDemo2 { public static void main(String[] args) { while(true) { System.out.println("infinitive while loop"); } } }
  • 24. 24 do-while loop In Java, the do-while loop is used to execute a part of the program again and again. If the number of iteration is not fixed then the do-while loop is used. This loop executes at least once because the loop is executed before the condition is checked. Syntax: do { //code for execution }while(condition); Example: public class DoWhileDemo1 { public static void main(String[] args) { Int i=1; do { System.out.println(i); i++; }while(i<=10); } }
  • 25. 25 Break Statement In Java, a break statement is used inside a loop. The loop is terminated immediately when a break statement is encountered and resumes from the next statement. Syntax: jump-statement; break; Example: public class BreakDemo1 { public static void main(String[] args) { for(int i=1;i<=10;i++){ if(i==8){ break; } System.out.println(i); } } }
  • 26. 26 Example using break in do while loop public class BreakDoWhileDemo1 { public static void main(String[] args) { int i=1; do { if(i==15) { i++; break; } System.out.println(i); i++; }while(i<=20); } }
  • 27. 27 continue Statement In Java, the Continue statement is used in loops. Continue statement is used to jump to the next iteration of the loop immediately. It is used with for loop, while loop and do-while loop. jump-statement; continue; Example: public class ContinueDemo1 { public static void main(String[] args) { for(int i=1; i<=10; i++) { if(i==5) { continue; } System.out.println(i); } } }
  • 28. 28 Example: public class ContinueDemo2 { public static void main(String[] args) { xy: for(int i=1;i<=5;i++){ pq: for(int j=1;j<=5;j++){ if(i==2&&j==2){ continue xy; } System.out.println (i+" "+j); } } } }
  • 29. 29 Java return Keyword Java return keyword is used to complete the execution of a method. The return followed by the appropriate value that is returned to the caller. This value depends on the method return type like int method always return an integer value. Points to remember o It is used to exit from the method. o It is not allowed to use return keyword in void method. o The value passed with return keyword must match with return type of the method. Example public class ReturnExample1 { int display() { return 10; }
  • 30. 30 public static void main(String[] args) { ReturnExample1 e =new ReturnExample1(); System.out.println(e.display()); } } Output 10
  • 31. 31 Example public class ReturnExample2 { void display () { return null; } public static void main (String [] args) { ReturnExample2 e =new ReturnExample2(); e. display (); } } Output: Void methods cannot return a value
  • 32. 32 /* Java Program Example - Interchange Two Numbers */ import java.util.Scanner; public class swap { public static void main(String args[]) { int a, b, temp; Scanner scan = new Scanner(System.in); System.out.print("Enter Value of A and B :n"); System.out.print("A = "); a = scan.nextInt(); System.out.print("B = "); b = scan.nextInt(); temp = a; a = b; b = temp; System.out.print("Number Interchanged Successfully..!!n"); System.out.print("A = " +a); System.out.print("nB = " +b); } }
  • 33. 33 /* Java Program Example - Convert Fahrenheit to Centigrade */ import java.util.Scanner; public class fahren { public static void main(String args[]) { float fah; double cel; Scanner scan = new Scanner(System.in); System.out.print("Enter Temperature in Fahrenheit : "); fah = scan.nextFloat(); //input 68 cel = (fah-32) / 1.8; System.out.print("Equivalent Temperature in Celsius = " + cel); // 20.0 } }
  • 34. 34 /* Java Program Example - Print Table of Number */ import java.util.Scanner; public class table { public static void main(String args[]) { int num, i, tab; Scanner scan = new Scanner(System.in); System.out.print("Enter a Number : "); num = scan.nextInt(); //5 System.out.print("Table of " + num + " isn"); for(i=1; i<=10; i++) { tab = num*i; System.out.print(num + " * " + i + " = " + tab + "n"); } } }
  • 35. 35 // creation of table of 2 through command line import java.io.*; class table2{ static int sum=0; // static is required static int t=2,p; public static void main(String args[]) { System.out.println(args[0]); // pass the base address i:e args[0] for(int i=1;i<=Integer.parseInt(args[0]);i++) // convert string args[0] to integer value { p=i*t; System.out.println(i + "*" + t + "=" +p); } } } // input java table2 10 output is tableof 2 i:e 2 4 6 8 10--------=20
  • 36. 36 /* Java Program Example - Calculate Arithmetic Mean */ import java.util.Scanner; public class mean { public static void main(String args[]) { int n, i, sum=0, armean; int arr[] = new int[50]; Scanner scan = new Scanner(System.in); System.out.print("How many Number you want to Enter ? "); n = scan.nextInt(); System.out.print("Enter " +n+ " Numbers : "); for(i=0; i<n; i++) { arr[i] = scan.nextInt(); sum = sum + arr[i];
  • 37. 37 } armean = sum/n; System.out.print("Arithmetic Mean = " +armean); } } // WAP to create Fibonacci series import java.util.*; public class fibo { public static void main (String[] args) { fibo fs=new fibo();
  • 38. 38 fs.fibonacci(); } public void fibonacci() { int numb1 = 1; int numb2 = 1; int temp = 0; Scanner input=new Scanner(System.in); System.out.println("How Many Terms? (Up To 45)"); int x=input.nextInt(); x=x-2;
  • 39. 39 System.out.println (numb1); System.out.println (numb2); for (int i = 0; i < x; i++) { System.out.println(numb1 + numb2 + " "); temp = numb1; numb1 = numb2; numb2 = temp + numb2; } } }
  • 40. 40 // Java Program Example WAP enter character and Check Alphabet or Not import java.util.Scanner; public class alphabet { public static void main(String args[]) { char ch; Scanner scan = new Scanner(System.in); System.out.print("Enter a Character : "); ch = scan.next().charAt(0); // enter character x if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) { System.out.print(ch + " is an Alphabet"); } else { System.out.print(ch + " is not an Alphabet"); } } }
  • 41. 41 /* Java Program Example - Generate Armstrong Numbers */ import java.util.Scanner; public class armsgen { public static void main(String args[]) { int num1, num2, i, n, rem, temp, count=0; Scanner scan = new Scanner(System.in); /* enter the interval (two number) */ System.out.print("Enter the Interval :n"); System.out.print("Enter Starting Number : "); num1 = scan.nextInt(); //1 System.out.print("Enter Ending Number : "); num2 = scan.nextInt(); //160
  • 42. 42 for(i=num1+1; i<num2; i++) { temp = i; n = 0; while(temp != 0) { rem = temp%10; n = n + rem*rem*rem; temp = temp/10; } if(i == n) { if(count == 0) {
  • 43. 43 System.out.print("Armstrong Numbers Between the Given Interval are :n"); } System.out.print(i + " "); // output 153 count++; } } if(count == 0) { System.out.print("Armstrong Number not Found between the Given Interval."); } } }
  • 44. 44 /* Java Program Example - Find Largest of Three Numbers */ import java.util.Scanner; public class largest { public static void main(String args[]) { int a, b, c, big; Scanner scan = new Scanner(System.in); System.out.print("Enter Three Numbers : "); a = scan.nextInt(); b = scan.nextInt(); c = scan.nextInt(); // let a is the largest big = a; if(big<b) {
  • 45. 45 if(b>c) { big = b; } else { big = c; } } else if(big<c) { if(c>b) { big = c; }
  • 46. 46 { big = b; } } else { big = a; } System.out.print("Largest Number is " +big); } }