pj3
pj3
pj3
1.Selection Statements:
=>Types:
(a)simple if
i
=>The process of declaring if-block without else-block is known as
thi
"simple if"
(b)if-else
ipa
=>we use if-else block in the program when we want to check true or false
or "Inner if"
sh
(d)Ladder if-else
ate
as "Ladder if-else"
nk
(e)switch-case
=>we use switch-case in the program when we want to select one from
Ve
----------------------------------------------------------------------------
2.Iterative Statements:
=>Types:
(a)while loop
=>In while looping structure the condition is checked first and if the
(b)do-while loop
=>In do-while looping structure the loop-body is executed first and then
i
condition is checked,this process is repeated until the condition is false.
thi
(c)for loop
ipa
do-while loops,because Initialization,Condition and Incre/Decre are
Ma
declared in the same line separated by SemiColon.
-----------------------------------------------------------------------------
3.Branching Statements:
sh
=>Types:
ate
(a)break
(b)continue
Ve
(c)return
and the returned value will come back to the method call.
(d)exit
=>"exit" will stop the program execution or will terminate the program
execution
===================================================================
=======
faq:
i
thi
(i)while loop
(ii)do-while loop
ipa
=>In while-loop condition is checked first then loop-body is executed,but in
do-while loop the loop-body is executed first and then the condition is checked.
Ma
=>In while-loop the body is not executed for false-condition,but in do-while loop
Note:
ate
=>do-while loop in applications will waste the execution time in executing body
===================================================================
===========
Ve
Note:
=>In realtime for-loop is used for Strings and Arrays,because they use index.
===================================================================
=============
Assignment:(Solution)
Update above program by displaying result as "Fail",if any one Subject marks
are
in b/w 0 to 34
import java.util.Scanner;
i
thi
class StudentResult
ipa
String res(float pr,boolean p)
{ Ma
if(p)
{
sh
return "Fail";
}
ate
{
nk
return "Distinction";
Ve
return "FirstClass";
}
else if(pr>=50 && pr<60)
return "SecondClass";
i
{
thi
return "ThirdClass";
ipa
else
return "Fail";
Ma
}
sh
}
ate
class Percentage
nk
float p = (float)totMarks/6;
return p;
}
class StuMainClass
i
int i=1,totMarks=0;
thi
boolean p=false;
while(i<=6)
ipa
{
Ma
System.out.println("Enter marks of sub-"+i);
if(sub<0 || sub>100)
sh
{
ate
System.out.println("Invalid marks...");
continue;
nk
p=true;
totMarks=totMarks+sub;
i++;
}
System.out.println("TotalMarks:"+totMarks);
float pr = ob.per(totMarks);
System.out.println("Percentage:"+pr);
i
StudentResult sr = new StudentResult();
thi
String result = sr.res(pr,p);
System.out.println("Result:"+result);
ipa
}
}
Ma
===================================================================
============
sh
Note:
reading
Ex : DemoMethods7.java
i
import java.util.Scanner;
thi
class Customer
ipa
//Instance Variables
int custNo;
String custName;
Ma
long phNo;
sh
void getCustomer()
ate
System.out.println("****CustomerDetails****");
nk
System.out.println("CustNumber:"+custNo);
System.out.println("CustomerName:"+custName);
Ve
System.out.println("CustomerPhNo:"+phNo);
class DemoMethods7
{
public static void main(String[] args)
i
c.custNo = Integer.parseInt(s.nextLine());
thi
System.out.println("Enter the CustName:");
c.custName = s.nextLine();
ipa
System.out.println("Enter the CustPhNo:");
c.phNo = s.nextLong();
c.getCustomer();
Ma
}
sh
}
ate
o/p:
112
Alex
9898981234
****CustomerDetails****
CustNumber:112
CustomerName:Alex
CustomerPhNo:9898981234
Diagram:
i
thi
ipa
Ma
sh
===================================================================
====
ate
nk
Ve