Equality and Relational Operators== Equal to != Not equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to Conditional Operators&& Conditional-AND || Conditional-OR Selection/Branching IF Statement Switch Statement
1. Write a Java program to get a number from the user and print whether it is positive or negative. Test Data HINT: if (input > 0)
{
System.out.println("Number is positive");
}
else if (input < 0)
{
System.out.println("Number is negative");
}
else
{
System.out.println("Number is zero");
}
2. Take three numbers from the user and print the greatest number. Test Data Solution: import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input the 1st number: ");
int num1 = in.nextInt();
System.out.print("Input the 2nd number: ");
int num2 = in.nextInt();
System.out.print("Input the 3rd number: ");
int num3 = in.nextInt();
if (num1 > num2)
if (num1 > num3)
System.out.println("The greatest: " + num1);
if (num2 > num1)
if (num2 > num3)
System.out.println("The greatest: " + num2);
if (num3 > num1)
if (num3 > num2)
System.out.println("The greatest: " + num3);
}
}
3. Write a Java program that keeps a number from the user and generates an integer between 1 and 7 and displays the name of the weekday. Test Data Solution: We can also use switch import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
int day = in.nextInt();
System.out.println(getDayName(day));
}
// Get the name for the Week
public static String getDayName(int day) {
String dayName = "";
switch (day) {
case 1: dayName = "Monday"; break;
case 2: dayName = "Tuesday"; break;
case 3: dayName = "Wednesday"; break;
case 4: dayName = "Thursday"; break;
case 5: dayName = "Friday"; break;
case 6: dayName = "Saturday"; break;
case 7: dayName = "Sunday"; break;
default:dayName = "Invalid day range";
}
return dayName;
}
}4. Write a Java program to find the number of days in a month. Test Data Write a program that uses only if statements then another program that uses only a switch statement. LOOPS A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages −
Java programming language provides the following types of loop to handle looping requirements.
The for loop SyntaxThe syntax of a for loop is − for(initialization; Boolean_expression; update) {
// Statements
}
Here is the flow of control in a for loop −
Flow Diagram | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for(num2 = 0; num2 <= 3; num2++) { for(num1 = 0; num1 <= 2; num1++) { System.out.println(num2 + " " + num1); } } |
| Memory | Screen | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Remember, in the memory, for loops will register a value one beyond (or the step beyond) the requested ending value in order to exit the loop. | 0 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Are we getting a little loopy? | ![]() |
3. Write a program in Java to display the pattern like right angle triangle with a number. USE NESTED LOOPS
Test Data
Input number of rows : 10
Expected Output :
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910
THE while loops
In the last post we discussed about for loop. In this tutorial we are going to discuss about while loop with the help of examples and flow diagrams. We will cover following topics in this article: a) Introduction to while loop b) Syntax of while loop c) Flow diagram of while loop d) While loop example e) infinite while loop.
What is while loop?
Like for loop, It also executes a block of statements repeatedly until the condition(Boolean expression) returns false.Syntax of while loop
while (Boolean expression) { statement(s) //block of statements }The logic in while loop is simple it executes the block of statements when the Boolean expression returns true. It gets terminated when the Boolean expression returns false.
Are you confused??
Don’t worry we will see the flow diagrams and example. That would make you comfortable.While loop example
class WhileLoopExample { public static void main(String args[]){ int i=10 while(i>1){ System.out.println(i); i--; } } }The output of this program is:
10
9
8
7
6
5
4
3
2
The do while loop
break Statement
continue Statement
Exercise rewrite Q1, Q2 and Q3 from the for loop and convert them into while loops.











