Skip to main content

Posts

Java for loop

Java for loop Java for loop is used to repeat execution of the statement(s) until a certain condition holds true.  for  is a keyword in Java programming language. Java for loop syntax for   ( /* Initialization of variables */   ;   /*Conditions to test*/   ;   /* Increment(s) or decrement(s) of variables */ ) {    // Statements to execute, i.e., Body of a for loop } You can initialize multiple variables, test many conditions and perform increments or decrements on many variables according to requirement. Please note that all three components of a for loop are optional. For example, following for loop prints "Java programming" indefinitely. // Infinite for loop for   (   ;   ;   )   {    System . out . println ( "Java programming" ) ; } You can terminate an infinite loop by pressing  Ctrl + C . Simple for loop example in Java Example program below uses fo...

Java if else program

Java if else program Java if else program uses if else to execute statement(s) when a condition holds true. Below is a simple program which explains the usage of if else in Java programming language. In this program, a user inputs marks obtained in an exam, and it is compared with the minimum passing marks. An appropriate message is printed on the screen based on whether the user passed the exam or not. Java programming if else statement // If else in Java code import   java.util.Scanner ;   class  IfElse  {    public   static   void  main ( String [ ]  args )   {      int  marksObtained, passingMarks ;         passingMarks  =   40 ;         Scanner input  =   new  Scanner ( System . in ) ;          System . out . println ( "Input marks scored by you" ) ;       ...

Java hello world program

Hello world Java program: Java code to print hello world. Java programming source code class HelloWorld {    public static void main(String args[])    {       System.out.println("Hello World");    } } Download Hello world program class file. "Hello World" is passed as an argument to println method, you can print whatever you want. There is also a print method which doesn't take the cursor to the beginning of next line as println does. System is a class, out is an object of PrintStream class, and println is the method. Output of program: