Lab # 2 Loops and Logic: Objective
Lab # 2 Loops and Logic: Objective
Lab # 2 Loops and Logic: Objective
LAB # 2
OBJECTIVE:
To Study Control and Iteration Statements and Math Classes.
THEORY:
if Statement:
The if statement tests the condition. It executes the if block if condition is true.
Syntax:
if(condition){
//code to be executed
}
As a practical example of an if statement that includes a statement block, you
could write:
if-else Statement:
The if-else statement also tests the condition. It executes the if block if
condition is true otherwise else block is executed.
Syntax:
if(condition){
//code if condition is true }
else{
//code if condition is false
}
Leap Year Example:
//code to be executed if condition3 is true
}
else{
//code to be executed if all the conditions are false
}
Nested if statement:
The nested if statement represents the if block within another if block. Here,
the inner if block condition executes only when outer if block condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed }
}
To illustrate the nested if statement, I can modify the if from the previous
example:
Switch statement:
Use the switch statement to select one of many code blocks to be executed.
Syntax
switch(expression) {
case x:
//code block
break;
case y:
// code block
break;
default:
// code block
}
Example
int day = 4;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
}
Loops:
A loop allows you to execute a statement or block of statements repeatedly. The need to
repeat a block of code arises in almost every program.
How It Works
All the work is done in the for loop. The loop counter is i, and this is declared
and initialized within the for loop statement. The syntax of this for loop is
shown in Figure
As you see, there are three elements that control the operation of the for loop,
and they appear between the parentheses that follow the keyword for. In
sequence, their purpose is to:
❑Set the initial conditions for the loop, particularly the loop counter
❑Specify the condition for the loop to continue
❑Increment the loop counter
For-Each Loop:
There is also a "for-each" loop, which is used exclusively to loop through
elements in an array:
Syntax
for (type variable : arrayname) {
// code block to be executed
}
The While Loop:
The while loop, loops through a block of code as long as a specified condition is true:
Syntax:
while(condition) {
// body of loop
}
You can write the program for summing integers again using the while loop,
which will show you how the loop mechanism differs from the for loop:
The do/while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat the
loop as long as the condition is true.
Syntax:
do {
// body of loop
} while (condition);
You can write an integer-summing program with this kind of loop too:
Prorgam # 1
This java program finds factorial of a number. Entered number is checked first
if its negative then an error message is printed.
if ( n < 0 )
System.out.println("Number should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial of "+n+" is = "+fact);
}}}
The Java Math library function Math.random() generates a double value in the
range [0,1). Notice this range does not include the 1.
import java.lang.Math;
public class MathDemo
{
public static void main(String[] args)
{ double num = -4876.1874d;
double x=10.0,y=20.0;
int max=15;
LAB TASK
1. Write a program to solve quadratic equation using( if, else-if and else) .
Example:
Input a: 1
Input b: 5
Input c: 1
Expected Output :
The roots are -0.20871215252208009 and -4.7912878474779195
Expected Output:
Number is positive
5. Write a program to read 3 integer values and print the greater and
lesser number respectively.
Example:
A=29
B=25
C=72
Output:
Greater number=72
Lesser number=29