Java: Java Programming For Beginners a simple Start To Java Programming (Written By a software Engineer)


Download 0.82 Mb.
Pdf ko'rish
bet90/95
Sana09.01.2022
Hajmi0.82 Mb.
#261112
1   ...   87   88   89   90   91   92   93   94   95
Bog'liq
Java Programming For Beginners - A Simple Start to Java Programming ( PDFDrive )

Implementing Code Loops
Looping is an important and commonly used construct of any programming language. You
will be faced by several situations when you need to perform the same set of instructions,
a given number of times. In order to implement this scenario, loops have to be used. Loop
constructs available in JavaScript include for, while and do-while.
The  while  loop  includes  a  condition  and  as  long  as  the  condition  remains  true,  the  loop
continues  to  execute.  The  do  –  while  loop  is  a  modification  of  the  while  loop.  If  the
condition in the while is false, the while loop will not execute at all. On the other hand,
even if the while condition is false, the do-while loop executes at least once.
Syntax for while loop:
while(condition)
{
//code
}
Syntax for do-while loop:
do
{
//code


}
while(condition)
The  last  type  of  loop  available  in  JavaScript  is  for  loop.  The  for  loop  allows  you  to
initialize the looping variable, check for condition and modify the looping variable in the
same statement.
Syntax:
for(initialize; condition; modify)
{
//code
}
Sample code:
for(i=0; i<10; i=i+1)
{
//code
}
This loop will run 10 times.
Note:
1.  If at any point in time, you wish the loop to break, you can use the break statement.
2.  If you do not specify a condition or specify a condition that is always true, the loop
will run infinitely.
Error Handling
Exceptions  are  expected  to  occur  at  several  points  in  your  code.  Therefore,  it  is  best  to


implement a mechanism that can help you deal with these exceptions and avoid crashing.
An exception can be described as an illegal operation or any condition that is unexpected
and not ordinary. A few examples of exceptions include unauthorized memory access.
You  can  perform  exception  handling  at  your  own  level  by  validating  the  values  of
variables before performing any operations. for instance, before performing division, it is
advisable  to  check  if  the  value  of  the  denominator  is  equal  to  zero.  Any  operation  that
involves division of a number by zero raises the divide-by-zero exception.
However, there are other situations that cannot be handled in this manner. For instance, if
the network connection breaks abruptly, you cannot do anything to pre-emptively handle
the situation. Therefore, for situations like these, try, catch and finally keywords are used.
The code that is expected to throw an exception is put inside the try block. This exception,
upon its occurrence, is caught by the catch block, which executes code that is supposed to
be executed immediately after an exception is caught. The catch may also be followed by
the  finally  block,  which  performs  the  final  cleanup.  This  block  is  executed  after  the
execution of try and catch blocks.
Syntax:
try
{
//code
}
catch(exception name)
{
//code


}
finally
{
//code
}

Download 0.82 Mb.

Do'stlaringiz bilan baham:
1   ...   87   88   89   90   91   92   93   94   95




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling