Lets discuss about them step by step.
Under this topic we can see two kind of control statements.(control structures in java)
1.Selection Structure
2.Repetition Structure
- Selection Structure
Under this one we can see ,
single selection - if
double selection - if else
multiple selection - switch
If statement
In here , in if statement a condition is tested, and if the condition meets the test , the relevant code is executed.
public class IfExample {
public static void main(String[]args){
int age =30;
if (age>18){
System.out.println("Age is grater than 18");
}
}
}
If Else statement
In here a different code is executed if the condition fails the test.
public class IfElseExample{
public static void main(String[]args){
int age =12;
if (age>=18){
System.out.println("You can vote");
}else{
System.out.println("You can't vote");
}
}
}
In above examples we declared a variable and initialized it. That means we created a variable called "age" int type and assign value 18 to it.
Can't we do this using keyboard input?
Yes. We can.
For that you should import under java.util.package classes , java.util.Scanner class .
Let's see how we write if-else statement using Scanner.
import java.util.Scanner;
public class Vote{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.println("Enter your age : ");
// you should give keyboard input at here. The cursor will wait for you when you run your program until you enter int value as your age.
int age = s.nextInt();
// In here we convert our input to int value whether it is String .We can talk about these things further under Wrapper Class topic
if (age>=18){
System.out.println("You are eligible for voting");
}else{
System.out.println("You are ineligible for voting");
}
}
}
If-Else-If statement
In here a different codes are executed if the condition fails again and again.
import java.util.Scanner;
public class IfElseIfExample{
public static void main(String[]args){
Scanner s = new Scanner (System.in);
System.out.println("Enter your marks : ");
int marks = s.nextInt();
if ((marks<55 ) {
System.out.println("You have no grade");
}
else if ((marks>= 55) && (marks<65) ) {
System.out.println("Your grade is C");
}
else if ((marks>= 65) && (marks<75) ) {
System.out.println("Your grade is B");
}
else if ((marks>= 75) && (marks<=100) ) {
System.out.println("Your grade is A");
}
else ((marks>100 ) {
System.out.println("Invalid input");
}
}
}
Nested If Statement
In here we can see if block within another if block. Also you can use else part here.
import java.util.Scanner;
public class NestedIfExample{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.println("Enter your age : ");
System.out.println("Enter your weight : ");
int age = s.nextInt();
int weight = s.nextInt();
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood ");
}else{
System.out.println("Your weight is not enough for donate blood");
}
else {
System.out.println("Your age is not enough for donate blood");
}
}
}
For multiple selection you can use switch statement.
In here if the value of the control expression does not match any of the values then the default case is executed.
Switch Statement
import java.util.Scanner;
public class SwitchExample{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
System.out.println("enter value of the day");
int day = s.nextInt();
switch(day){
case 1: System.out.println("Today is monday");break;
case 2: System.out.println("Today is tuesday");break;
case 3: System.out.println("Today is wednesday");break;
case 4: System.out.println("Today is thursday");break;
case 5: System.out.println("Today is friday");break;
case 6: System.out.println("Today is saturday");break;
case 7: System.out.println("Today is sunday");break;
default: System.out.println("invalid");
}
}}
- Repetition Structure
Sometimes we want to repeat certain things many times.
ex: Printing a list of names.
Generating a series of random numbers
Under this topic we can identify loops.Let's see about them.
For Loop
for(initiate action; boolean expression;action after each iteration)
{Statements to be repeated ; }
Inside main method , try to get output of the following for loops.
ex 1 : for(int i=0;i<5;i++){
System.out.println("loop "+i);
}
for(int i=5;i>0;i--){
System.out.println("loop "+i);
}
for(char ch='a';ch <='z';ch++){
System.out.println(ch);
}
int array[] ={34,67,78};
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
While Loop
This one continually executes a block of statements while a particular condition is true.
int i=2;
while(i<7){
System.out.println("while loop " + i);
i++;
}
Do while Loop
In here the statements within the do block are always executed at least once.
int i=10;
do{
System.out.println("do while loop " + i);
i--;
}
while(i>0);
Break
This one is used to exit from loop.
for(int i=10;i>0;i--){
System.out.println("loop " +i);
if(i==5){
break;}
}
In above example it will print from loop 10 to loop 5 without others. Because we have put break when i ==5 . So loop will exit from that place.
No comments:
Post a Comment