Menu

5w33t45 Archive

Java Break and Continue in Loops

These two keywords help us control the loop from within it. break will cause the loop to stop and will go immediately to the next statement after the loop: continue will stop the current iteration and will move to the next one. Notice that inside a for loop, it will still run the third section.

Java Foreach Loop

Another version of for, is the foreach. The keyword we use is still for, but when we want to iterate on the elements inside an array we can simply use it: This is a short version and equivalent to: Notice that if you want to use the index of the element inside the loop, you have

Java While Loop

The syntax is very similar to the previous for we looked at: The condition will run for the first time when entering and every time the loop is done. If it returns false, the loop will not run. If we want the loop to always run at least one, we can use do-while Notice the ; in

Java For Loop

The for loop has three sections: First section runs once when we enter the loop. Second section is the gate keeper, if it returns true, we run the statements in the loop, if it returns false, we exit the loop. It runs right after the first section for the first time, then every time the loop is

Java If-else Statement

The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java. if statement if-else statement if-else-if ladder nested if statement Java if Statement The Java if statement tests the condition. It executes the if block if condition is true. Example: The Java

Search for files using find

The find command is used in Linux to search for files in the directory tree starting from the location specified. It searches your filesystem live, which causes the command to run slower than the locate command. The syntax of the command is: find PATH EXPRESSION The expression is a way of specifying what you want to find. You can

Search for files using locate

The locate command searches a database of filenames in Linux. Unlike the find command, which can find files by permissions, owner, file size, etc, the locate command finds files only by their name.  This command does not search your system live. Instead, it has its own database that it usually updated once a night or once a week. This means that locate may

Split the output of a program

The tee command splits the output of a program, so that it can be both displayed on the screen and saved in a file. This command is usually used when you want the output of the program to be both stored and displayed at the terminal. For example, if we want to display the output of the ls command on

Display first lines of a text file

The head command displays, by default, the first 10 lines of a text file in Linux. This command is often used to get an idea of the kind of text file you’re looking at; the first 10 lines are usually enough to determine what a file is. Example: As with tail, you can specify the number of lines

Display last lines of a text file

The tail command displays, by default, the last 10 lines of a text file in Linux. This command can be very useful when examining recent activity in log files. Example: In the picture above you can see that the last 10 lines of the /tmp/example.txt file were displayed. The tail command can be used with various options. For example, if you would like to