Please enable JavaScript.
Coggle requires JavaScript to display documents.
JAVA MODULE 2, TAKING USER INPUT, WHILE LOOPS, FOR LOOPS, CONDITIONALS,…
-
TAKING USER INPUT
-
- import the java.util.Scanner class.
- create a Scanner object: Scanner sc = new Scanner(System.in);
- Use the corresponding method of the Scanner to take input, for example: int num = sc.nextInt();
Some programs need user input. For example, a game may ask the user for a nickname and show it in the game, or a converter can ask for a value that you want to convert.
To take input from the user, we first must import the corresponding class.
That is done using the following line: import java.util.Scanner;
This line should be written at the very top of your code, before the class declaration.
This imports the Scanner class, which we will use for taking input.
After importing the Scanner class, we need to create a Scanner object: Scanner sc = new Scanner(System.in);
Now we are ready to take input from the user and assign it to a variable.
For example, to take a String input, we need to use the following: String name = sc.nextLine();
Similarly, we can take a integer as input using nextInt(): int age = sc.nextInt();
This will accept an integer input from the user and assign it to the age variable There are similar methods available to take other types as input: nextDouble(), nextFloat(), nextBoolean().
You can use the same Scanner to take multiple inputs.
For example, let's take the name and age as input and output them Ex String name = sc.nextLine();
WHILE LOOPS
-
- The code in the while loop runs as long as the condition is true.
- The ++ and -- operators are used to increase and decrease the value of a variable by one.
- Java provides shorthand operators to perform mathematical operations on a variable, for example x = x 9; can be written as x = 9.
- The do-while loop is similar to a while loop, but it is guaranteed to run at least once.
As it is common to decrease or increase a variable by 1 during loops, Java provides increment and decrement operators.
-
For example, the statement x=x-1; can be simplified to x--;
A while loop statement repeatedly executes a target statement as long as a given condition is true. Ex while(x > 0) {
The while loops check for the condition x > 0. If it evaluates to true, it executes the statements within its body. Then it checks for the statement again and repeats.
The line x = x-1; is important, as without it the condition would never become false and the loop would run forever.
Each time the loop runs, 1 is subtracted from x.
Similarly, the increment operator ++ is used to increase the value of a variable by one
Some loops require to increase or decrease the value of a variable by a different number.
For example, lets output only the even numbers from 0 to 10. Surprise! There is also a shorter way for x = x+2; It can be written as x+=2; Similarly, there are shorthand operators for other mathematical operations, such as -= for subtraction, *= for multiplication, etc.
-
FOR LOOPS
-
-
You can have any type of condition and any type of increment statements in the for loop.
The example below prints only the even values between 0 and 10:
-
-
Here is one example use case of break:
For example, you are making a calculator and need to take numbers from the user to add together and stop when the user enters "stop".
Another control statement is continue.
It makes the loop skip the current iteration and go to the next one.
-
CONDITIONALS
-
-
-
-
-
-
= greater than or equal to
An if statement can be followed by an optional else statement, which executes when the condition evaluates to false
-
Instead of using nested if-else statements, you can use the else if statement to check multiple conditions.
-
Conditional statements are used to perform different actions based on different conditions.
For example, a billing program can apply a discount to the total only if the amount is greater than a threshold.
The if statement is one of the most frequently used conditional statements.
If the if statement's condition is true, the block of code inside the if statement is executed. Ex if (condition) {, if(age >= 18) {
Remember that you need to use two equal signs (==) to test for equality, since a single equal sign is the assignment operator.
-
The Switch statement
-
Instead of many if else statements, which become hard to read, we can use a switch statement.
-
- Each case needs a break statement, or the code of the other cases will continue to get executed.
- The default case can be used to run code if none of the cases match.
-
- Remember, that each case is followed by a value and a colon.
-
-
You can have any number of case statements within a switch. Each case is followed by the comparison value and a colon
It is important to have a break statement for each case.
If no break appears, the program will continue to execute the next case in the switch, even if the value does not match the variable that is switched on.
A switch statement can have an optional default case.
The default case can be used for performing a task when none of the cases is matched.
MULTIPLE CONDITIONS
-
- The AND operator && combines two conditions and checks if both of them are true.
- The OR operator || check if any of the conditions if true.
- The NOT operator ! reverses the condition.
In some cases we need to combine multiple conditions, for example, let's say we want to check if the age value is greater than 18 and less than 50.
This can be done using the && the logical AND operator.. Ex : if (age > 18 && age < 50) {
-
The NOT (!) logical operator is used to reverse the condition.
If a condition is true, the NOT logical operator will make it false, and vice versa. Ex if(!(age > 18)) {
You can chain multiple conditions using parentheses and the logical operators. Ex : if((country == "US" || country == "GB") && (age > 0 && age < 100)) {
-