Conditional statements and loops are used in Java and other languages since they simplify the codes. 

Conditional statements, also called Selection statements, consist of two big statements: If statements and Switch ~ case statements. 

Here are some examples for your better understanding. 

 

if

public class If01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		if(10>5) {
			System.out.println("Executed 1");
		}
		
        // {} can be omitted if it is a single line.
		if(10>5) System.out.println("Executed 2");
		
		if(true) {
			System.out.println("Execute unconditionally");
		}
		
		if(false) {
			System.out.println("Execution failed");
		}
		
		if(10 > 30)
			System.out.println("Failed to print out");
			System.out.println("Condition isn't applied on this execution");

	}

}

if ~ else ( Even? or Odd? )

import java.util.Scanner;

public class If02 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println("Insert one integer.");
		
		Scanner sc = new Scanner(System.in);
		
		int n1 = sc.nextInt();
		
		if (n1 % 2 == 0) {
			System.out.println(n1+" is even.");			
		}else {
			System.out.println(n1+" is odd.");
		}
	}

}
// 0 is considered as even number.

if ~ else if ~ else (Maximum and Minimum)

import java.util.Scanner;

public class If03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int n1, n2, max, min; 
		
		System.out.println("Insert two integers.");
		
		Scanner sc = new Scanner(System.in);
		
		n1 = sc.nextInt();
		n2 = sc.nextInt();
		
		if (n1 > n2) {
			max = n1;
			min = n2;
			
		}else {
			max = n2;
			min = n1;
		}	
			System.out.println(max + " is the maximum.");
			System.out.println(min +" is the mininum.");
		
		
	}

}

Random number(Dice)

public class If06 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// Random numbers 
		// 	0.0 <= Math.random() < 1.0
		//   1                      6
		System.out.println("E="+Math.E);	
		System.out.println("PI="+Math.PI);	
		System.out.println(Math.random()); 
		
		int num = (int)(Math.random()*6) + 1;
		System.out.println("num=" +num);
		
		if(num == 1) {
			System.out.println("1");			
		}else if(num == 2){
			System.out.println("2");
		}else if(num == 3){
			System.out.println("3");
		}else if(num == 4){
			System.out.println("4");
		}else if(num == 5){
			System.out.println("5");
		}else {
			System.out.println("6");
		}
	}

}

Score 

import java.util.Scanner;

public class If05 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int n1;

		System.out.println("Insert your score.");
		
		Scanner sc = new Scanner(System.in);
		
		n1 = sc.nextInt();

		if(n1 >= 90) {
			System.out.println("A");
		}else if(n1 >= 80) {
			System.out.println("B");
		}else if(n1 >= 70) {
			System.out.println("C");
		}else if(n1 >=60) {
			System.out.println("D");
		}else {
			System.out.println("F");
		}
	
	}

}

This can also be written with Switch ~ case like below. Switch ~ case and If ~ else if ~ else statements do the same role, but the syntax is slightly different. 

 

Switch ~ case (Score)

package p2022_06_23;

import java.util.Scanner;

public class Switch01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		System.out.println("Insert your score (0 ~ 100).");
		Scanner sc = new Scanner(System.in);
		
		int s = sc.nextInt();  // s = 95
		s = s / 10;            // s /= 10
		switch (s) {
			case 10 :
			case 9: System.out.println("A");
			     	 break;
			case 8: System.out.println("B");
				 	 break;
			case 7: System.out.println("C");
				 	 break;
			case 6: System.out.println("D");
				 	 break;
			default:System.out.println("F");
		}

	}

}

While these conditional statements above are runned once, there are loops if you want to write repetitive statements. There are three types of loops : For, While, and Do ~ while. 

 

For01.java

public class For01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
        
		int i; 
		for(i=1; i<=10; i++) { 
			System.out.println(i+"This is a for loop.");
		}
}
}

For02.java

public class For02 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// Sum of 1 ~ 10
		int sum = 0;  //Local Variable
		for(int i=1; i<=10; i++) { 
			sum = sum + i; 
		}
		// System.out.println("sum="+sum);
//		System.out.println("1~"+i+"="+sum);
		System.out.println("Sum="+sum);
	}
	
}

For03.java

public class For03 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int odd = 0;
		int even = 0;

		for(int i=1; i<=100; i+=2)
			odd += i;

		for(int i=0; i<=100; i+=2)
		even += i;
		System.out.println("Sum of all odd numbers from 1 to 100: "+odd);
		System.out.println("Sum of all even numbers from 1 to 100: "+even);
		
	}
}

For04.java (Multiplication Table)

import java.util.Scanner;

public class For04 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println("Insert one integer for a multiplication table.");
		Scanner sc = new Scanner(System.in);
		int dan = sc.nextInt();    // dan = 5
		
		System.out.println("["+dan+"]");
		for(int i=1; i<=9; i++)
			System.out.println(dan+"*"+i+"="+dan*i);
				
		
	}

}

Multiplication Table 

public class Multiplication Table {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		for(int i=2; i<10; i++) {
			System.out.print("["+i+"]"+"\t");
		}
		System.out.println();
		
		for(int j=1; j<10; j++) {
			for(int k=2; k<10; k++) {
				System.out.print(k+"*"+j+"="+k*j+"\t");
				
			}
			System.out.println();
		}
	}

}

While01.java

This is the same as For03.java, so you can choose one that makes your code more efficient. 

public class While01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		int i=1, odd=0, even=0;		
		while(i<=100) {				
			if(i%2 == 1) {		
				odd += i;
			}else {					
				even += i;
			}		
			i++;				
		}
		System.out.println("Sum of all odd numbers from 1 to 100: "+odd);
		System.out.println("Sum of all even numbers from 1 to 100: "+even);
	}

}

The output will be the same.

Dowhile01.java 

This is also the same as While01.java and For03.java.

public class DoWhile01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
				
		int i=1, odd=0, even=0;			
		do {
			if(i%2 == 1) {		
				odd += i;
			}else {				
				even += i;
			}
			i++;					
		}while(i<=100);				
		System.out.println("Sum of all odd numbers from 1 to 100: "+odd);
		System.out.println("Sum of all even numbers from 1 to 100: "+even);
	}

}

The result is also the same. 

As you can see, the conditional statements and the loops have multiple options for you to choose from when you make your program, but the first thing you need to consider is "Efficiency." When we code, we need to consider the processing time and the length of the codes

'Java' 카테고리의 다른 글

Java) Array  (0) 2022.09.04
Java) .length vs length()  (0) 2022.09.02
Java) Data Types  (0) 2022.08.27
Java) Operators  (0) 2022.08.26
Java (Must know)  (0) 2022.08.25

+ Recent posts