Skip to main content

Posts

Showing posts with the label Java

Java program to print alphabets

Java program to print alphabets Java program to print alphabets on screen, i.e., a, b, c, ..., z; in lower case. Java source code class  Alphabets {     public   static   void  main ( String  args [ ] )     {        char  ch ;          for   ( ch  =   'a' ;  ch  <=   'z' ;  ch ++ )           System . out . println ( ch ) ;     } } You can easily modify the above java program to print alphabets in upper case. Download  Alphabets  program class file. Output of program: Printing alphabets using a while loop (Only the body of the main method is shown): char  c  =   'a' ;   while   ( c  <=   'z' )   {    System . out . println ( c ) ;   c ++; } Using a do while loop: char  c  =...

Java while loop

Java while loop Java while loop is used to execute statement(s) until a condition is true. In this tutorial, we will learn to use while loop with examples. First of all, let's discuss while loop syntax: while (condition(s)) { // Body of loop } 1. If the condition(s) holds true then the body of the loop is executed, after execution of the loop body condition is tested again and if the condition is still true then the body of the loop is executed again, and the process repeats until the condition(s) becomes false. The condition evaluates to true or false and if it is a constant, for example, while (c) {…}, where c is a constant, then any non zero value of c is considered to be true, and zero is considered false. 2. You can test multiple conditions such as while   ( a  >  b  &&  c  !=   0 )   {   // Loop body } Loop body is executed till value of variable a is greater than value of...

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: