top of page
hv

Conditionals and Iteration

Conditionals & Iterations


So far, only predefined tasks could be accomplished. But with the help of conditional statements, programming can be made so much more powerful. It is the ability to test a certain condition and provide a corresponding outcome. To know whether a condition is true or false, a data type known as booleans is employed. They permit the user to perform logical operations which can be evaluated as True or False.


Java programming is a class-based, object-oriented programming language (learn about it in our previous article: Programming Basics) that is designed to have few dependencies in implementation. It is a general-purpose programming language that allows application developers to write once, run anywhere (WORA), which means that compiled Java programs can run on all platforms that support it without the need for recompilation. An important syntax to know in Java Programming is the input and output. The input statement can be written using a package named scanner, where the input can be taken from the user, and “System.out.println” is used to output the result to the user.


Conditionals


In Java programming, conditionals can be employed in order to allocate certain outputs to a certain condition. They utilize conditionals such as If-Else and Switch statements.

Java supports the following logical conditions from mathematics:

  1. Less than: a<b

  2. Less than or equal to: a<=b

  3. Greater than: a>b

  4. Greater than or equal to: a>=b

  5. Equal to: a==b

  6. Not equal to: a!=b

If-Else Statements


The If-Else conditional statement uses the keywords “if”, “else” and “else if”, which allows the utilisation of multiple conditions in order to make the output more streamlined.


Switch Statements


Java programming also uses Switch statements in order to provide a multi-situation program to be built. Unlike the If-Else and If-Then-Else statements, the Switch-Case statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types as well as enumerated types, the string class, character, byte, short and integer (refer to Programming Basics)


Iteration


Iteration is a programming method that simplifies an algorithm by allowing the program to repeat certain steps until told otherwise. This makes designing algorithms quicker and easier as they don’t have to include a lot of unnecessary steps, such as manually typing out the code for each repeat required. The loops that are used in Java programming are the while loop and the for loop.


While Loops


The while loop loops through a block of code as long as a given condition is true. The code within the loop will run, over and over again, as long as the condition is valid. There is also a variant of the while loop, known as the do-while loop. This loop will execute the code block once before checking if the condition is true and then repeat the loop as long as the condition is true.


For Loops


The For loop is a control flow statement that is used specifically for iteration. When the user knows exactly how many times they want to loop a block of code, they can use the for loop instead of the while loop. It enables a particular set of conditions to be executed repeatedly until a condition is satisfied. Imagine that you needed to write a program to print the first 100 integers. Would you write the print statement 100 times? Or would it be simpler to write a 3-line code?


Article was written for CyberClubNPSi

1 view

Recent Posts

See All

Comments


bottom of page