You are on page 1of 17

1.

if statement

if is keyword used to write condition.

Syntax :

if(condition){

stmts;

}
//write a program to check a number and print if it is positive

class IfStatement {

public static void main(String[] args) {

int number = 10;

// checks if number is greater than 0

if (number > 0) {

System.out.println("The number is positive.");

System.out.println("Statement outside if block");

}
2.if else
if else are keywords used to write condition

syntax :

if(condition){

stmt 1;

else{

stmt 2;

}
Flowchart :
//write a program to check a number and print if it is positive or negative

class Main {

public static void main(String[] args) {

int number = -10;

// checks if number is greater than 0

if (number > 0) {

System.out.println("The number is positive.");

// execute this block

// if number is not greater than 0

else {

System.out.println("The number is not positive.");

System.out.println("Statement outside if...else block");

}
3. else if ladder
To write more than one condition and only one condition should be executed then we use
else if ladder.

Syntax :

if(condition){

else if{

else if{

else{

}
// write a program to take a value from user and print wheather it is positive/negative/zero

class Main {

public static void main(String[] args) {

int number = 0;

// checks if number is greater than 0

if (number > 0) {

System.out.println("The number is positive.");

// checks if number is less than 0

else if (number < 0) {

System.out.println("The number is negative.");

// if both condition is false

else {

System.out.println("The number is 0.");

}
4. Switch :

syntax :
switch(condition){

case 1:

stmts;

break;

case 2:

stmts;

break;

case 3:

stmts;

break;

case n:

stmts;

break;

default:

stmts;

}
// write a program to take a number from user, and perform operation as per his choice for
arithmetic operations

package basicPrograms;

import java.util.Scanner;

public class SwitchDemo {

public static void main(String[] args) {

int choice, num1=10, num2=5;

Scanner s = new Scanner(System.in);

System.out.println("Please enter your choice");

System.out.println("1. Addition");

System.out.println("2. Substraction");

System.out.println("3. Multiplication");

System.out.println("4. Division");

System.out.println("Please enter choice.");

choice = 3; //3
switch(choice){

case 1:

System.out.println(num1+num2);

break;

case 2:

System.out.println(num1-num2);

break;

case 3:

System.out.println(num1*num2);

break;

case 4:

System.out.println(num1-num2);

break;

default:

System.out.println("please enter correct choice");

break;

}
Looping Statements :
To execute some set of code repeatedly we use loops.

while loop :

While is entry controlled loop used to execute some set of code repeatedly.

syntax :

while(condition){

Program : write a program to print numbers from 1 to 100.

package loopPract;

public class WhileDemo {

public static void main(String[] args) {

int i=1;

while(i<=10) {

System.out.println(i);

i++; //11

}
do while loop :

do while loop is exit control loop.

syntax :

do{

}while(condition);

//program to print numbers from 1 to 10

package loopPract;

public class DoWhileDemo {

public static void main(String[] args) {

int i=1;

do{

System.out.println(i);

i++;

}while(i<=10);

}
for loop

for loop is used to execute set of statements repeatedly

syntax :

for(initialization;condition;increment/decrement){

//Program 3 : Write a program to print 1 to 100 using for loop

package basicPrograms;

public class ForDemo {

public static void main(String[] args) {

for(int i=1;i<=10;i++) { //1,2,3,4.....9,10,11


System.out.println(i); //1,2,3...9,10
}
}

/*
sequence

step 1 : initialization
step 2 : condition (if condition is true then next steps will be executed)
step 3 : body
step 4 : increment/decrement
step 5 : repeat step 2 to 4 till loop condition is true

*/
//write a program to print below pattern using nested loop

/*

**

***

****

*****

*/

package loopPract;

public class PatternDemo {

public static void main(String[] args) {

//for lines

for(int i=1;i<=5;i++) { //6

//print

for(int j=1;j<=i;j++) { //

System.out.print(j);

System.out.println();

}}
for each loop :

 It’s commonly used to iterate over an array or a Collections class (eg, ArrayList)

//for…each loop with array :

package basicPrograms;

public class ForEachDemo {

public static void main(String[] args) {

int eids[]= {10,20,30,40};

for(int e : eids) {
System.out.println(e);
}

}
//for…each loop with arraylist :

package basicPrograms;

import java.util.ArrayList;

public class ForEachDemo {

public static void main(String[] args) {

ArrayList<Integer> eids= new ArrayList();

eids.add(20);
eids.add(30);
eids.add(40);
eids.add(50);

for(int e : eids) {
System.out.println(e);
}

You might also like