Professional Documents
Culture Documents
Experiment 1 Programs On Basic Programming Constructs Like Branching and Looping
Experiment 1 Programs On Basic Programming Constructs Like Branching and Looping
CLASS: SE COMPS
ROLL NO: 57
EXPERIMENT 1
PROGRAMS ON BASIC PROGRAMMING CONSTRUCTS
LIKE BRANCHING AND LOOPING
FOR LOOP
The for loop is used for loops which have a predetermined
number of iterations. A for loop provides a step-by-step way
of performing repeated actions. The parts of a for loop
handle these steps:
1.Set the initial value
2.Perform a test to see whether the loop should continue
3.Executing the loop actions
4.Update value(s) used for the test
The initialization,test and update actions constitute the three
part control section in form of expressions. Each part is
enclosed in parentheses. Semicolon separates these
expressions from each other.
for(initialization; test condition; iteration)
{
//body of the loop
}
THE ? OPERATOR
Java includes a special ternary operator that can be used to
replace certain types of if-else-if statements. It is called
ternary operator, as it requires three operands to work on. It
is represented by the ? and : symbols.
NAME:TAHSEEN SHAIKH
CLASS: SE COMPS
ROLL NO: 57
class EXPT1_1
{
public static void main(String args[])
{
int number,og_num,sum=0;
int digit;
number = og_num=153;
while(number != 0)
{
digit = number % 10;
NAME:TAHSEEN SHAIKH
CLASS: SE COMPS
ROLL NO: 57
sum += digit * digit *digit;
number=number/10;
}
if(sum == og_num)
System.out.println("Entered number is an Armstrong
Number");
else
System.out.println("Entered number is not an Armstrong
number");
}
}
OUTPUT 1.1:
NAME:TAHSEEN SHAIKH
CLASS: SE COMPS
ROLL NO: 57
CODE 1.2:
WAP to find maximum of four numbers using
conditional Operator
class EXPT1_2
{
public static void main(String[] args)
{
int a=78,b=367,c=5,d=4,e;
OUTPUT 1.2:
NAME:TAHSEEN SHAIKH
CLASS: SE COMPS
ROLL NO: 57
CONCLUSIONS:
1)The provision for loops makes it easier to perform
programs which require repetition or decision making