Showing posts with label Netbeans Tutorials. Show all posts
Showing posts with label Netbeans Tutorials. Show all posts
Tuesday, May 29, 2018
Sunday, April 22, 2018
Thursday, April 19, 2018
Tuesday, April 17, 2018
by Admin
Inheritance in java
In the last tutorial, we talked about Object Oriented Programming, and we mention various concepts in OOP. In that same tutorial, we went into detail about the concepts of Objects and Classes in Java.
In this tutorial, we’ll be dealing with one of the concepts we mention in the last one: Java Inheritance
Before we move further let see what all topic we have covered previously
Inheritance in Java is the process where one object or class acquires all the states (or properties) and behaviour of another object/class. The class whose states and behaviours is being acquired is known as the parent class or superclass, while the one that inherits is called the subclass.
With inheritance, we can create new classes built on existing classes; allowing you to reuse methods and variables from our parent class, add new ones, and arrange our data in a hierarchal order.
Inheritance in Java represents what is known as a parent-child relationship.
What do we use Inheritance in Java?
A major reason for inheritance in Java is for code reusability, as the subclass inherits everything from its parent class, including all variables and methods.
The extends keyword
The extends keyword is used to indicate that the class being created is inheriting properties from an existing class.
An Inheritance example
The following image shows an example of inheritance in Java, whereas a class named Puppy, inherits the properties of our parent class Dog.
Main Class looks like below:
Result :
In our given program, a Dog class is created, with the dogStats method, which takes in String and int variables and return the set print statements. When the Puppy class is created, which inherits from the Dog class, a copy of the parent’s contenst is created within it. This is why we can access the members of the Dog class using the Puppy class.
Trying to access the Puppy class with the Dog class will give an error.
The superclass reference variable can hold the subclass object, but using it will only access the members of the superclass. To access both, you’ll have to create reference variables to the subclass.
Note that the subclass inherits all the members from the super class. Members refers to the fields, methods and nested classes. Constructors are not members, and so cannot be inherited. They can be invoked from the subclass, though.
The IS-A Relationship
Also known as the parent-child relationship, an IS-A relationship simply means that an object is a type of another object. This form of relationship is achieved in inheritance:
Based on the example above, we can say the following:
For example:
A class can implement one or more interfaces, eliminating the need to perform that form of inheritance.
Next tutorial, we shall deal with Method Overriding.
Hope you like this article , Please share it with your friends and colleagues.
Also Read :
Thanks for reading
Noeik
In this tutorial, we’ll be dealing with one of the concepts we mention in the last one: Java Inheritance
Before we move further let see what all topic we have covered previously
- Making Games With Java- Introduction
- Getting Everything you need for Java
- Introduction to Java Programming
- Basic data types and Variables in java
- Basic Data types and Variables in java - Continued...
- User Input using Scanner class in java
- Conditional Logic in java
- Loops in java
- Arrays in java
- Wrapper classes in java
- Methods in java
- What are Object Oriented Programming Practices
Inheritance in Java is the process where one object or class acquires all the states (or properties) and behaviour of another object/class. The class whose states and behaviours is being acquired is known as the parent class or superclass, while the one that inherits is called the subclass.
With inheritance, we can create new classes built on existing classes; allowing you to reuse methods and variables from our parent class, add new ones, and arrange our data in a hierarchal order.
Inheritance in Java represents what is known as a parent-child relationship.
What do we use Inheritance in Java?
A major reason for inheritance in Java is for code reusability, as the subclass inherits everything from its parent class, including all variables and methods.
The extends keyword
The extends keyword is used to indicate that the class being created is inheriting properties from an existing class.
An Inheritance example
The following image shows an example of inheritance in Java, whereas a class named Puppy, inherits the properties of our parent class Dog.
package animal; public class Dog { public void dogStats(String name, int age){ System.out.println("His name is " + name); System.out.println("He is " + age + " year(s) old"); } } class Puppy extends Dog{ ppublic void pupStats(String name, int age){ System.out.println("His name is " + name); System.out.println("He is " + age + " week(s) old"); } }
public static void main(String[] args){ Puppy first = new Puppy(); first.dogStats("Doug",1); first.pupStats("Junior",3); }
His Name is Doug He is 1 year(s) old His name is Junior He is 3 week(s) oldExplaining the Inheritance example
In our given program, a Dog class is created, with the dogStats method, which takes in String and int variables and return the set print statements. When the Puppy class is created, which inherits from the Dog class, a copy of the parent’s contenst is created within it. This is why we can access the members of the Dog class using the Puppy class.
Trying to access the Puppy class with the Dog class will give an error.
The superclass reference variable can hold the subclass object, but using it will only access the members of the superclass. To access both, you’ll have to create reference variables to the subclass.
Note that the subclass inherits all the members from the super class. Members refers to the fields, methods and nested classes. Constructors are not members, and so cannot be inherited. They can be invoked from the subclass, though.
The IS-A Relationship
Also known as the parent-child relationship, an IS-A relationship simply means that an object is a type of another object. This form of relationship is achieved in inheritance:
- The Animal class is the parent of the Mammal and Reptile classes.
- The Dog class is the child or subclass of the Mammal class
- The Lizard class is the child or subclass of the Reptile class
- In an IS-A relationship, we may as well say:
- Mammal IS-A Animal
- Reptile IS-A Animal
- Dog Is-A Mammal
- Lizard IS-A Animal
- The HAS-A Relationship
- Unlike the IS-A relationship, this is based mainly on usage, or behaviour, or basically what an object has. This helps to reduce bugs and duplication of code.
For example:
Basically, we’re saying that the Dog HAS-A Tail. By having a class dedicated solely to the tail, we don’t have to put all the code for the tail in the Dog class, allowing us to reuse if for other classes that may require a tail; like a Cat class for example.
In OOP, the users do not have to worry about which object is actually doing the work. So the Dog class will hide implementation details from its users. Therefore, when the Dog class is to perform an operation, it will either do it by itself or ask another class to do it.
Types of Inheritance
There are about four type of Inheritance in Java:
- Single Inheritance: This takes place when a parent class had only one sub class.
- Multi-Level Inheritance: Here, the parent class has one subclass, and that subclass has its own subclass.
- Hierarchical Inheritance: In this form of inheritance, the parent class can have two or more subclasses.
- Multiple Inheritance: The opposite of Hierarchical Inheritance, this is achieved when a class has two or more parent classes. This is not supported in Java, though, so you cannot do this:
class Mammal extends Dog, Cat{
}
Next tutorial, we shall deal with Method Overriding.
Hope you like this article , Please share it with your friends and colleagues.
Also Read :
- Program to find the Nth Element of a Singly Linked list from the end in java
- [Program]Generic Implementation of custom linked list in java
- Program of Linked List Insertion and Deletion Operations in java
- Stack Implementation program in java
- Breadth first Traversal(BFT) AND Depth First Traversal(DST) Program in java
Thanks for reading
Noeik
Saturday, April 7, 2018
by Admin
Methods in java
Methods are one of the most important part of coding , this article is all about methods in java
before more further let see previous articles in series.
- Making Games With Java- Introduction
- Getting Everything you need for Java
- Introduction to Java Programming
- Basic data types and Variables in java
- Basic Data types and Variables in java - Continued...
- User Input using Scanner class in java
- Conditional Logic in java
- Loops in java
- Arrays in java
- Wrapper classes in java
A method in Java can be defines as a bunch of code that is written to form a particular operation or function. There are quite a lot of methods that are in-built into the java programming language, an example being the System.out.println() method. Though it seems like just one simple line of code, it actually executes several statements in the background in order to do what it does.
To view these background programmes, type out a print statement, select it and right-click. Under the Navigate option, click Go to Source.
In this lesson, you will see how to create your own methods and invoke your methods into your code. This will help you to keep your code concise, organised and more readable.
Defining a Method
An average method consists of a method header and a method body. The method header is the statement that determines the values that the method returns and how easily it can be accessed in and out of a class.
The method header usually consists of the following:
- Access Modifier: This determines how easily the method can be accesses. It is not necessary to add, but is usually advisable.
- Return Type: The type of value the method can return. It’s possible for the method to return no value.
- Method Name
- Parameters: The type, order, and number of parameters a method requires to return a value. A method can have no parameters
The method body is defined by the pair of opened and closed curly brackets immediately after our method header and consists mainly of the code we want the method to run and the return keyword.
Now, let’s look at the method below:
public int add(int firstNumb , int secondNumb){ int result =0; result = firstNumb+ secondNumb; return result; }
If we’re to break it down into the components listed above:
- public : access modifier
- int: return type
- methodName: the method name
- int firstNumb, int secondNumb: parameters
- result = firstNumb + secondNumb: code
- return result: return statement
The above method, named add(), takes two integer parameters firstNumb and secondNumb, and returns their sum, which will be saved in the integer variable result.
Calling your Method
When your code is run, it will only read the code written within the main method. In order to implement our methods in the program, the method will have to be invoked or called into the main method.
The process of doing this is quite simple. When a method is called in our main method, control is transferred from the main method to the called method. The code the executes the code in the curly bracket and returns the set value.
For example, let us call the above mentioned method ‘add’ into our main method:
public class CalculationDemo { public static void main(String[] args) { CalculationDemo demo = new CalculationDemo(); System.out.println("Will Add two number 10 , and 20"); int r =demo.add(10, 20); System.out.println(r); } public int add(int firstNumb , int secondNumb) { int result =0; result = firstNumb+ secondNumb; return result; } }
Result:- Will Add two number 10 , and 20 30
Void Method
So far, we can see that the type of data you set to the method is the type of data that it returns. If you create an integer method, it returns an integer. A string method will therefore return a string, a float method returns a float, a double method a double, and so on.
But there are times you don’t want your code to return any value. You just want it to perform a function and be done with it. This method is called a void method.
Also read : Basic and tricky java Interview Questions
Let’s recreate our last method, but make it void. Outside our last method, create this new method and call it addWithoutReturn:
public void addWithoutReturn(int first , int second) { int result =first +second; System.out.println(result); }
You can notice that it’s basically the same as our original method. The only difference is the absence of the return statement. This is replaced with a print statement, which causes it to automatically print out our variable when the method is called; so we don’t have to call a print statement in the main method.
Access Modifiers
Access Modifiers are keywords that determine how easily a method can be accessed or whether you can access a method within the class, the subclass or the java package. The three major access modifiers are:
- Public
- Private
- default
- Protected
Giving a public modifier will make your method or variable visible everywhere; even outside the java package. It is good practice not to do this, though, so that your variable will not be affected by an external user. It is more advisable to give your method a private or protected modifier.
A private modifier added before a method name will make it accessible to your class only. You won’t be able to access it anywhere outside your class; not even in the package. A protected modifier, on the other hand, will still allow your method to be seen everywhere, except outside the package. This is used mostly during inheritance and other related practices.
It is usually best to make your method or variable private, so that your method will not be manipulated by an external user. You can allow indirect access to your method or variable through the use of a getter and setter function. To access this; right-click your java file, click Insert Code, then click getter and setter. Choose the variable or method you need a getter and setter for, and click OK.
Access Modifiers grouped based on their Access Levels
Modifier
|
Class
|
Package
|
Sub-Class
|
World
|
Public
|
Access
|
Access
|
Access
|
Access
|
Protected
|
Access
|
Access
|
Access
|
No Access
|
No Modifier(default)
|
Access
|
Access
|
No Access
|
No Access
|
Private
|
Access
|
No Access
|
No Access
|
No Access
|
If we give a public modifier before any variable, it is visible everywhere in your project. It is good practice not to do this, though, so that it will not be affected by any other user. It is usually more advisable to declare your variable private or protected.
When your variable or method is declared private, it is only made available in the class. You cannot access it in any other class except its own. If you try to do so, it will give you an err because that class that cannot be accessed.
Protected is the least used among all these access modifiers, and so we won’t discuss it here because it is mostly used in Java Inheritance; so we will discuss that when we start on inheritance.
Next week, we’ll go a step further by talking about how to create classes.
Thanks for reading
Noeik
Sunday, March 25, 2018
by Admin
Wrapper Classes in java
Wrapper Classes - Have you ever heard about them , if not , no issue you will get to know about it in this post.
There are some other post related to this , which can be helpful are
- Making Games With Java- Introduction
- Getting Everything you need for Java
- Introduction to Java Programming
- Basic data types and Variables in java
- Basic Data types and Variables in java - Continued...
- User Input using Scanner class in java
- Conditional Logic in java
- Loops in java
- Arrays in java
Wrapper Classes – why are they needed?
There is a class that has been dedicated to each of the 8 primitive data types in java. These primitive types are int, float, char, byte, long, short, boolean and double. The dedicated classes that “wrap” over these primitive types are called Wrapper Classes.int num = 10; // primitive type Integer num = new Integer(10); // wrapper classThe main purpose of these wrapper classes is:
- To enable primitives to perform activities that can be performed by objects. For example being added to a collection
- To be able to perform the different utility functions like converting to and from String objects, comparison on primitives
How can we create Wrapper Classes?
There are 2 ways of creating wrapper class instances in Java.Using Constructor:
Constructors of the corresponding wrapper classes can be used to create instances with the help of the new keyword.
1)Accepts the primitive type as parameter
Example: Integer num = new Integer (10);
2)Accepts string as parameter (except Character)Example: Integer num = new Integer (10);
Example: Integer num = new Integer ("10");
Using the valueOf method:
Another way of creating Wrapper class objects is by using the valueOf method.There are 3 variants of this method.
1)Accepts primitive type as parameter
This statement will return an Integer object containing the value represented by the primitive type.
2)Accepts String parameter
This returns the Integer object for the String passed as parameter.
3)Accepts two parameters a String and radix.
The radix parameter will be used to determine the value of the Integer that is returned based on the String parameter passed.
The following example shows how both these ways can be used to create wrapper class instances:
public class WrapperExample { public static void main(String args[]) { Integer numint1 = new Integer (10); //or Integer numint2 = 10; Integer numStr = new Integer ("20"); Integer withValueOf1 =Integer.valueOf(9); Integer withValueOf2 =Integer.valueOf("9"); Integer withValueOf3 = Integer.valueOf("444",16); System.out.println("numint1 = "+numint1); System.out.println("numint2 = "+numint2); System.out.println("numStr = "+numStr); System.out.println("withValueOf1 = "+withValueOf1); System.out.println("withValueOf2 = "+withValueOf2); System.out.println("withValueOf3 = "+withValueOf3); } }
As the wrapper classes come in last in the hierarchy, there is no subclass that one can get from them.
The constructor that accepts String parameters may throw NumberFormatexception if the string passed cannot be parsed into an Integer.
Using the valueOf() method seems more efficient as it returns cached objects which means that it is not always necessary to create a new object.
Autoboxing and its advantages
- When a primitive type is automatically converted to its corresponding wrapper object it is called autoboxing
- This reduces code to convert from primitive types to Wrapper equivalents thus leading to cleaner code.
Integer numint2 = 10;
The above statement is an example of auto boxing.Auto-boxing is done in the following cases:
- When a primitive is passed as a parameter where an object of the corresponding wrapper class is expected.
- When a primitive is assigned to a variable of the corresponding wrapper class as seen in the example above.
//auto boxing Character chWrapper = 'a'; // Auto-unboxing char ch = chWrapper;
Casting in Java – Implicit and explicit Casting
Taking an object of one type and converting it into another is the simplest way in which we can define Casting.But obviously there are some rules that need to be followed. You cannot just take one variable and convert it into another type.
Implicit Casting:
- Automatic type conversion or implicit casting happens if both the concerned types are compatible with each other and the target is larger/wider than the source.
- No casting is specifically required in the case of implicit casting.
short sh = 10; int num = sh; long l = num; float fl = l; double d = fl;
The implicit casting can be done in the following order only.public class CastingExample { public static void main(String args[]) { short sh = 10; int num = sh; long l = num; float fl = l; double d = fl; System.out.println("Value of sh: " + sh); System.out.println("Value of num: " + num); System.out.println("Value of l: " + l); System.out.println("Value of fl: " + fl); System.out.println("Value of d: " + d); } }
In inheritance too, implicit casting is present when we assign a child object to a parent class instance.
Example:
class Parent { public void method() { // method body } } public class Child extends Parent { public static void main(String args[]) { Parent p = new Child(); p.method(); } }
Explicit Casting:
If both the concerned types are compatible with each other but the source is larger/wider than the target then it is called explicit casting.Here the conversion will happen the other way around.
To achieve narrowing, explicit casting needs to be done
double d = 75.0; // Explicit casting float fl = (float) d; long l = (long) fl; int num = (int) l; short sh = (short) num;
Similarly, in inheritance we can explicitly cast super type to a sub type.public class CastingExample { public static void main(String args[]) { double d = 75.0; // Explicit casting float fl = (float) d; long l = (long) fl; int num = (int) l; short sh = (short) num; System.out.println("Value of sh: " + sh); System.out.println("Value of num: " + num); System.out.println("Value of l: " + l); System.out.println("Value of fl: " + fl); System.out.println("Value of d: " + d); } }
Example
class Parent { public void disp() { // method body } } public class Child extends Parent { public void disp() { // method body } public static void main(String args[]) { Parent p = new Child(); p.disp(); Child c = (Child)p; c.disp(); } }Explicit casting to child class needs to be done to achieve this as seen in the code, Child c = (Child)p;
To summarise:
- To be able to use the specific functionality of the target type, casting is done.
When converting float to long I am able to store more data in it. In the same way if we want to access some methods from a super class using the subclass variables we can use casting.- The object itself is not changed when you perform casting.
- There has to be compatibility between the source and target types when casting. I cannot cast a variable of a subclass of A to a parent of B.
- Upcasting or Implicit Casting can be automatic as it will never fail.
- Downcasting or Explicit casting on the other hand may throw a ClassCastException in some cases. This is where the instanceof check can come in handy.
If you liked this post , please share it with your friends and colleagues.
Thanks for reading
Monday, March 12, 2018
by Admin
Loops in java
You remember that last week we discussed the three forms of programming:
- Sequential Programming
- Selective Programming
- Iterative Programming
Before move further Let see the Previous Tutorials
Friday, March 9, 2018
by Admin
Conditional Logic in java
Before we see Conditional Logics lets first see the Previous topics in the series of tutorials
- Making Games With Java- Introduction
- Getting Everything you need for Java
- Introduction to Java Programming
- Basic data types and Variables in java
- Basic data types and Variables in java -Continued
- User Input using Scanner class in java
Now we’ve been running a lot of programs since I started these tutorials. That should be… a few weeks ago? Six, maybe? I’m not exactly sure. What I am sure of is that by now you’re becoming comfortable with Java programming, and you’ve probably made a few small programs of your own initiative. (If you haven’t been doing this, then you probably should start doing so. It’s great programming practice).
Since we started
these tutorials… six…seven weeks ago…that’s not important right now…we’ve only
being working with one form of programming called Sequential Programming.
Sequential Programming is basically when the code runs every line one by one,
from top to bottom. It’s a very linear form of programming, and it is built to
complete a particular task, and get one particular set of results.
Of course, not
all programs work sequentially. You might often want a code to only do
something when another thing as happened (when a condition is met). This is
done with Selective Programming. At other times, you might want your code to do
something more than once until some other conditions are met. This is done with
Iterative Programming.
For the sake of
this tutorial, we’re going to deal strictly with Selective Programming.
Selective Programming/Conditional Logic
Selective Programming/Conditional Logic
Conditional
Logic consists mainly of the use of IF statements. An if statement is basically
saying that if this is true, or this is false, then this should happen OR if
this is greater than this and less than this, then this is equal to that. (That
should be not confusing at all).
The standard
structure of IF statements in Java (and most other languages) is this:
if(condition) {
//code
}
Let’s take a
quick example. Open a new project or class and type this down:
boolean answer = true;
if (answer) {
System.out.println("The answer is true");
}
Run it and your
code should give you this:
Okay, if we’re
okay with that, let’s do a walkthrough. We started by creating a Boolean value
named answer (remember from one of our last tutorial, we said that Boolean data
types saved only two values: true or false). In this case, we saved the value
true into the Boolean. We then proceeded to write an IF statement, we put that
answer mist be equal to true in the conditions bracket. In the curly brackets,
which is where our actual program goes, there’s a print statement, that is
set to print to “The answer is true” on the output window.
All that is
computer for: “If the answer is true, then tell us that the answer is true.”
You know when
you’re playing a game, and when you make a particular decision, like moving
left, and something happens, like a boulder falls on you and kills you. Yeah,
it uses this basic concept. You could think of the coding like this (don’t type
this out):
boolean turnLeft, boulderfall, die;
if (turnLeft) {
boulderfall = true;
}
if (boulderfall) {
die = true;
}
Again, this is
basically saying that if you turn left, a boulder will fall on you, and you
will die. Note that the default value of Boolean is false, so it won’t be true
until you set that it’s true. Also, you can put an IF statement after your
previous statement. You can even put an IF statement inside an IF statement.
This is called a Nested IF.
Now, change the answer variable from true to
false, and run it again. When you’re done running it, and can see the output
window, you probably ask yourself “What? Why didn’t anything print out?”.
If
you’re asking that question, then your output window probably looks like this:
And the reason
there was nothing printed out is because there was nothing to tell the system
to print out a message if the answer was true. In fact, there was nothing to
tell the system what to do if the answer was true. If you want to do this, you
could just add another IF statement.
OR
…you could use a
form of conditional logic called…
IF…ELSE Statements
The basic
concept of this is: If the answer is true, then tell us it’s true, if not, tell
us it’s false. The standard structure of an IF…ELSE is this:
if(condition) {
//code
}
else {
//code
}
If you’re
wondering why the else doesn’t need a condition, think of it like a last
resort. If all the other conditions aren’t met, then the computer just has to
do it. Now add this to your code (the answer variable should stay false:
else{
System.out.println("The answer is not true");
}
Your code should
now look like this:
Run it, and you
should get the desired result.
Let’s try this
out with integer values this time, but before we do this, we’ll have to take a
quick look at some conditional operators (also called relational operators,
these are the symbols that relate two values to each other):
< Less Than
> Greater Than
<= Less Than or Equal to
>= Greater Than or Equal to
&& AND
|| OR
== Equals to/Has a value of
! NOT
I’m sure these
are quite self-explanatory. If there’re those who still don’t understand, It’ll
all be clear in the next example. What we’ll be making is a programming that
scans your age, and tells you if you’re over eighteen or under eighteen. Now,
comment out everything and write this out.
int age = 17;
if (age < 18){
System.out.println("You're underaged");
}
else{
System.out.println("You're an adult");
}
Now, this says: “If the age is under eighteen,
then say that the user is underaged. If it’s not (i.e. if it’s 18 and above),
then the user is an adult. Now before running it, look at the code. What do you
think the result will be? Is it the same as the output in the picture below?
Try changing up
the age variable. Make it 21, then 15, then 0, then 68; or something like that.
But what if we want to put more conditions, since the IF…ELSE can only take one
condition at a time. For example, what if we want a code that will tell if
you’re a child when you enter a value within a particular age bracket, then it
will tell you you’re an adult if you enter within another age bracket.
Well, you can enable the computer to make more
than one of such decisions, with the use of…
IF…ELSE IF Statements
The standard
structure for this is:
if(condition) {
//code
}
else if (condition) {
//code
}
else if(condition) {
//code
}
else{
//code
}
Depending on your code, an IF…ELSE IF can be as
long as it needs to be, and can even end with an ELSE, as the default if none
of the other conditions are met. Now, delete everything you’ve written and put
this:
Now, the first
IF will only run if ‘age’ is less than 13. The second IF, or the ELSE IF,
contains two conditions: if age is greater than or equal to 13, and if it is
less than or equal to 17. The presence of the double ampersand (&&)
which means ‘and’, tells the computer not to run that programme unless both of
those conditions are met. If I changed it to this (||) which means ‘or’, the
code will run if at least one of the conditions are met.
The third IF
says that the code should be run, only if the age is equal to 18 (note the
double equal signs. I’ve made that mistake too many times). The last one is
just another variation of the second IF, the only difference being the change
of values to 19 and 35, the age bracket of adults according to the code. Then
there’s the default else; if the input doesn’t meet the conditions, then the
user must be old.
Switch up the
value of age and run it. Then try modifying the conditions based on your
definitions of an adult and a child (people have different opinions of which
age determines an adult).
SWITCH STATEMENTS
Called Select
Case, for those familiar with Visual Basic, this is another form of Selective
Programming. It gives the option to test for a particular range of values, and
it’s easier than writing long complex IF…ELSE statements. This is the basic
structure:
switch (variable) {
case value:
//code;
break;
default:
//code;
break;
}
Bow every switch
statement starts with the keyword switch, followed by brackets. In the
brackets, you put in the name of the variable you want to test (in this case,
it’s age). You then put curly brackets, and write case, then a value (like 17
for example). After a colon, you put the code you want to be executed when that
condition is met. After that is the keyword break, which simply ends that
statement.
What this is
simply saying is: If age is 17, the do that. Or if it’s 18, then do something
else. The keyword default works the same way as an ELSE, in the sense that it
only runs when all the other requirements aren’t met.
Run it and you
should get this:
Now, I think
that’s enough for Conditional Logic, now it’s time to wrap this post up. I’d
advise that you build on what we’ve done today. Try using my lessons on user
input to make the IF statements more interactive. Also, experiment with other
data types.
NB: Strings
don’t use the same relationship operators as integers. Instead of this:
if
(name == “Daniel”) {
…use this:
if
(name.equal(“Daniel”) {
Next week, we’re
going to be talking about Loops. We’ll be picking up the pace a bit, so try to
keep up.
If you liked the article please share it with your friends and colleagues.
Happy Learning
Thanks for reading
Subscribe to:
Posts (Atom)








