Introduction to Loop in JAVA JAVA by Ravinder Nath Rajotiya - April 16, 2021April 16, 20210 Loops in JAVA There are three types f loop structures in JAVA What is a Loop? A Loop is a squance of instructions that are continually repeated until a certain condition is reached or met. We have following three types of loop in JAVA. while() do- while() for loop Requirements of loop We require to follow the following steps for writing a loop: for every loop we need a loop control variable with some initializatio value give final test value to this variable inside the loop e.g. while(x<10) Write the body of the loop containing the task to be repeated and inside the loop modify or change the control variable to test for changed value next time the loop executes. Let us start with the loop This
Switch Case in JAVA JAVA by Ravinder Nath Rajotiya - April 15, 2021April 16, 20210 Decision Making using switch case statement Well you have seen the decision making in previos posts on if, if-else statements. switch case is yet another pwerful decision making concept. We first ask the user to input some number, alphabet or some string to be used in taking decision so that control can be transferred at some relevant place in the program. //JOptionPane.showInputDialog() gets only inputs string values, to make them numbers we need to convert it to integer as: input = JOptionPane.showInputDialog("Please enter 1, 2, or 3"); //convert to integer number=Integer.parseInt(input) //for for inputting single character, we don't convert string to letters, but read only first letter of string at position '0'. This is becaues strings are constructed using characters input = JOptionPane.showInputDialog("Please enter a, b, or
String Comparision JAVA by Ravinder Nath Rajotiya - April 13, 2021April 14, 20210 Comparing Strings String is created from String class. let us declare two string variables as name1 and name2. String name1=”Peter” String name2=”Peter”; Ravinder Rajotiya Name1 name2 Steps in string object creation in JAVA Step-1 Step-2 First the string object with name "Ravinder" createrd in memory Next the string variable name1 will be created in memory which will hold the address of the text "Ravinder" and not the text itself. Similarily steps for name2 variable creation is: Step-1 Step-2 First the string object with name "Rajotiya" createrd in memory Next the string variable name2 will be created in memory which will hold the address of the text "Rajotiya" and not the text itself. Comparing Strings: We don't compare strings using relational operators. The simple reason is that the relational operators on strings
if, if-else, if-else-if, nested if JAVA by Ravinder Nath Rajotiya - April 13, 20210 if, if-else, if-else-if, nested if The if statements are the decision structures used in JAVA. It is most useful decision structure for testing for a condition and branch to the relevant statement in the program. Simple and Poorly written program An efficient programming approach package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { int number; String input; input= JOptionPane.showInputDialog("Please enter a number"); number = Integer.parseInt(input); if(number == 5) { JOptionPane.showMessageDialog(null, "The number is exactly equal to 5"); } if(number >5) { JOptionPane.showMessageDialog(null, "The number is > 5"); } if(number > 10) { JOptionPane.showMessageDialog(null, "The number is > 10"); } if(number <>>5) { JOptionPane.showMessageDialog(null, "The number is < 5"); } } } package com.Example; import javax.swing.*; import java.util.Scanner; public class examResult { public static void main(String[] args) { int number; Scanner in =new Scanner(System.in); System.out.println("Enter a number"); number=in.nextInt(); if(number ==5) { JOptionPane.showMessageDialog(null, "The number is exactly equal
Operators in JAVA JAVA by Ravinder Nath Rajotiya - April 13, 2021April 13, 20210 Decision Structure Deceision structure are used so that the program doesn’t have to do the same thing each time a program is executed. They basically answer a question on the spot during execution. Decesion structure need a question and this is done through Boolean. As we have seen boolean are primitive data types. It represent two values true or false. They can be set directly but is usually by using a set of operators including but not limited to relational and logical operators. Operator Meaning > Is greater than < Is less than >= Greater than or equal to <= Less than or equal to == Is equal to != Is not equal to We can use these operatots in Boolean expressions. Table belows gives some common ways of using the relational operators : Operator Meaning X > Y Is X