Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

NAME:TAHSEEN SHAIKH

CLASS: SE COMPS
ROLL NO: 57

EXPERIMENT 1
PROGRAMS ON BASIC PROGRAMMING CONSTRUCTS
LIKE BRANCHING AND LOOPING

AIM: To write programs on Basic programming


constructs like branching and looping
THEORY:
Java provides iteration statements such as for,while and do-
while. These statements create what is known as loops. A
loop repeatedly executes the same set of statements until a
termination condition is met.
A loop construct usually includes 4 steps:
1.Initialization of the counter
2.execution of the statements in the loop
3. test for a specified condition for execution of the loop
4.incrementation or decrementation of the counter
WHILE LOOP:
The while loop is the fundamental looping statement.It
repeats a statement or a block of statements, while the
expression that controls it is true. The general syntax of while
loop is
while(condition)
NAME:TAHSEEN SHAIKH
CLASS: SE COMPS
ROLL NO: 57
{
//body of loop
}
Where, condition can be any Boolean expression which can
hold true or false value. The while loop evaluates the
condition at the top of the loop. If the condition of the loop is
true, the body of the loop executes once.
While loop is an entry controlled loop, that means the test
expression is evaluated before each iteration.
DO-WHILE LOOP
Sometimes,programs require that the body of the loop gets
executed atleast once even if the condition is false.Java
provides the do-while loop that tests the condition at the end
of the loop,rather than at the beginning.The do-while loop
thus executes the body of the loop atleast once.The general
syntax of the do-while loop is
do{
// body of the loop
}while(condition);
The do-while loop first executes the body of the loop and
then evaluates the condition.If the condition is true,the loop
repeats.Otherwise the loop terminates and the statement
following the do-while loop executes .The condition must be
a Boolean condition.
NAME:TAHSEEN SHAIKH
CLASS: SE COMPS
ROLL NO: 57

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

The general syntax is


Expression1?expression2:expression3
Here,expression1 can be any expression that evaluates to a
Boolean value(true or false).If expression1 is true,then
expression2 is evaluated.Else expression3 is evaluated
Neither expression2 nor expression3 can be a void
CODE 1.1 :
1.1: WAP to check whether the number is Armstrong
or not using while loop.

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;

e = d > (c > (a > b ? a : b) ? c : ((a > b) ? a : b)) ? d : (c > (a


> b ? a : b) ? c : ((a > b) ? a : b));
System.out.println("Largest Number:"+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

2)There are two kinds of loops


i.Entry controlled loop-for and while
ii.Exit controlled loop-do{…}while()

3)Decision making can also be performed with the help


of ternary operator .

You might also like