OBJECTIVE: Demonstrate the ability to write Java code for the four basic program
ID: 3910566 • Letter: O
Question
OBJECTIVE: Demonstrate the ability to write Java code for the four basic programming structures and be introduced to Object Oriented Programming, OOP, and Classes. Demonstrate correct usage of syntax, format, comments and naming conventions. ASSIGNMENT: Complete the classic HELLO WORLD program. Show simple example code for each of the four basic programming structures. o Sequence If-Then If-Then-Else ° Looping Use the Scanner class and instantiate an object to accept user input from the keyboardExplanation / Answer
(Ans- )
Following are the simple java code for the basic programming structures:-
1. Sequence:
public class sequence
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Following is the sequence: ");
int N = in.nextInt();
int tall = 1;
for(int i = 0; i <=45; i++){
System.out.println(" " + i);
}
}
}
2. IF Statement :
OUTPUT:
3.IF-THEN-ELSE-STATEMENT :-
OUTPUT:
4. looping in JAVA :
WHILE LOOP
class while_Loop
{
public static void main(String args[])
{
int a = 1;
while (a <= 4)
{
System.out.println("Value of a:" + a);
a++;
}
}
}
OUTPUT:
FOR LOOP
class for_Loop
{
public static void main(String args[])
{
for (int a = 2; a <= 4; a++)
System.out.println("Value of a:" + a);
}
}
Output:
DO-WHILE LOOP :
class Do_While_Loop
{
public static void main(String args[])
{
int a = 21;
do
{
System.out.println("Value of a:" + a);
a++;
}
while (a < 20);
}
}
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.