Control statements
------------------
Alter the flow of executon of a program.
we have the follwoing control statements
i)decision making statement.
ii)loop control statement.
iii)case control statement.
i)decision making statement.
----------------------------
It checks whether a given a condition is true or false.
we have 2 types
if
if-else
if
--
An if statement checks whether a condition is true/false,of condition is true then if block gets executed otherwise execution of if block is skipped.
syntax
------
if(condition)
{
//statements
}
Types of if
-----------
There are 3 types of if statement
i)simple if
ii)multiple if
iii)nested if
simple if
---------
declaring only one if statement in a program is known as simple if.
Multiple if
-----------
declaring more than one if statement in a program is known as multiple if statement.
syntax
------
if(condition1)
{
//statements
}
if(condition2)
{
//statements
}
concatenation operator(+)
-------------------------
In between 2 numeric operands always an arithmetic(+) is called while performing addition.
ex:
int a=10,b=5;
a+b
string+anything=string
anything+string=string
string+string=string
if we apply (+) operator between a string operand and any other operand always a concatenation(+) is called which simply combines operands.
ex:
int a=10,b=5;
String s="satya";
a+b+s-->15satya
s+a+b-->satya105
a+b+s+a+b-->15satya105.
if-else
-------
It is used to check a condition.
if condition is true,if block is executed.
otherwise,else block is executed.
syntax
------
if(condition)
{
//statements
}
else
{
//statements
}
Multiple if-else
----------------
Declaring id-else more than once is known as multiple if-else
syntax
------
if(condition1)
{
//statements
}
else
{
//statements
}
if(condition2)
{
//statements
}
else
{
//statements
}
.
.
.
else-if ladder
--------------
It is used to check condition.
syntax
------
if(condition1)
{
//statements
}
else if(condition2)
{
//statements
}
else if(condition2)
{
//statements
}
..
..
..
else
{
}
Here if condition1 is true then that if block is executed otherwise condition2 is executed otherwise condition3 is executed and so on.
If all the conditions are false then else block is executed.
greatest of 3 numbers
grades of a student
s1 s2 s3
avg>=75-->distinction
avg>=60 && avg<75-->First class
avg>=50 && avg<60-->second class
avg>=40 && avg<50-->Third class
otherwise
fail
Nested if
----------
An if within another if is known as nested if.
syntax
------
if(condition1){
if(condition2){
if(condition3){
//statements
}
}
}
class Test
{
public static void main(String args[])
{
int a=400,b=3000,c=4200;
if(a>b)
{
if(a>c){
System.out.println(" a is big "+a);
}
}
else if(b>c)
{
System.out.println(" b is big "+b);
}
else{
System.out.println(" c is big "+c);
}
}
}
Loop control statements
-----------------------
Loops perform a repititive task.
we have the follwing loops
i)while
ii)do-while
iii)for
iv)foreach
Any loop requires
-----------------
Initialization
condition
inc/dec
Initialization
--------------
It specifies where loop should begin.
ex:
int i=1;
condition
---------
It specifies how many times a loop should iterate.
ex:
i<=10
inc/dec
-------
increments or decrements a loop
i++;/i--;
while
-----
It performs a repititive task.
syntax
------
while(condition)
{
//statements
inc/dec;
}
Program to print numbers from 1 to 10
-------------------------------------
class Loop{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
Program to print numbers from 10 to 1
-------------------------------------
class Loop{
public static void main(String args[])
{
int i=10;
while(i>=1)
{
System.out.println(i);
i--;
}
}
}
Program to print even numbers
-----------------------------
//10 to 1
class Loop{
public static void main(String args[])
{
int i=2;
while(i%2==0&&i<=100)
{
System.out.println(i);
i=i+2;
}
}
}
sum of 10 natural numbers
-------------------------
class Test{
public static void main(String args[])
{
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
}
System.out.println(sum);
}
}
Print even numbers from 1 to 100
--------------------------------
class Test{
public static void main(String args[])
{
int i=1,sum=0;
while(i<=10)
{
if(i%2==0){
sum=sum+i;
}
i++;
}
System.out.println(sum);
}
}
Print factors of a number
-------------------------
6
1 6%1==0
2 6%2==0
3 6%3==0
4 6%4==0
5 6%5==0
6 6%6==0
class Test{
public static void main(String args[])
{
int i=1,n=8;
while(i<=n)
{
if(n%i==0){
System.out.println(i);
}
i++;
}
}
}
sum of factors
--------------
class Test{
public static void main(String args[])
{
int i=1,n=8,sum=0;
while(i<=n)
{
if(n%i==0){
sum=sum+i;
}
i++;
}
System.out.println(sum);
}
}
perfect number
-------------
sum of the factors if a given number excluding itself must be equivalent to the given number.
6 ->1
2
3
6
Prime or composite
------------------
class Test
{
public static void main(String args[])
{
int i=1,n=2,count=0;
while(i<=n)
{
if(n%i==0)
{
count++;
}
i++;
}
if(count==2)
{
System.out.println("prime");
}
else
{
System.out.println("composite");
}
}
}
do-while
--------
It performs a repititive task until condition is false.
syntax
------
do
{
//statements
}
while(condition);
In do-while body is executed for one time even if condition is false.
class Test
{
public static void main(String args[])
{
int i=10;
do{
System.out.println(i);
i--;
}while(i>=1);
}
}
for loop
--------
repeatitive statamet
syntax
------
for(initialization;condition;inc/dec)
{
//statements
}
class Test
{
public static void main(String args[])
{
int i;
for(i=1;i<=100;i++)
{
if(i%2==0)
System.out.println(i);
}
}
}
switch
------
It is a case control statement.
switch is collection of cases.
syntax
------
switch(expression)
{
case value1:statements;
break;
case value2:statements;
break;
case value3:statements;
break;
...
...
default:statements;
}
In a switch case the value of expression is matched with the value of a case,whichever case is matching the value of expression that case gets executed.
break causes to stop the execution of switch
if none of the cases are matched then default statement gets executed.
Note:
switch doesn't accept decimal and long values
------------------
Alter the flow of executon of a program.
we have the follwoing control statements
i)decision making statement.
ii)loop control statement.
iii)case control statement.
i)decision making statement.
----------------------------
It checks whether a given a condition is true or false.
we have 2 types
if
if-else
if
--
An if statement checks whether a condition is true/false,of condition is true then if block gets executed otherwise execution of if block is skipped.
syntax
------
if(condition)
{
//statements
}
Types of if
-----------
There are 3 types of if statement
i)simple if
ii)multiple if
iii)nested if
simple if
---------
declaring only one if statement in a program is known as simple if.
Multiple if
-----------
declaring more than one if statement in a program is known as multiple if statement.
syntax
------
if(condition1)
{
//statements
}
if(condition2)
{
//statements
}
concatenation operator(+)
-------------------------
In between 2 numeric operands always an arithmetic(+) is called while performing addition.
ex:
int a=10,b=5;
a+b
string+anything=string
anything+string=string
string+string=string
if we apply (+) operator between a string operand and any other operand always a concatenation(+) is called which simply combines operands.
ex:
int a=10,b=5;
String s="satya";
a+b+s-->15satya
s+a+b-->satya105
a+b+s+a+b-->15satya105.
if-else
-------
It is used to check a condition.
if condition is true,if block is executed.
otherwise,else block is executed.
syntax
------
if(condition)
{
//statements
}
else
{
//statements
}
Multiple if-else
----------------
Declaring id-else more than once is known as multiple if-else
syntax
------
if(condition1)
{
//statements
}
else
{
//statements
}
if(condition2)
{
//statements
}
else
{
//statements
}
.
.
.
else-if ladder
--------------
It is used to check condition.
syntax
------
if(condition1)
{
//statements
}
else if(condition2)
{
//statements
}
else if(condition2)
{
//statements
}
..
..
..
else
{
}
Here if condition1 is true then that if block is executed otherwise condition2 is executed otherwise condition3 is executed and so on.
If all the conditions are false then else block is executed.
greatest of 3 numbers
grades of a student
s1 s2 s3
avg>=75-->distinction
avg>=60 && avg<75-->First class
avg>=50 && avg<60-->second class
avg>=40 && avg<50-->Third class
otherwise
fail
Nested if
----------
An if within another if is known as nested if.
syntax
------
if(condition1){
if(condition2){
if(condition3){
//statements
}
}
}
class Test
{
public static void main(String args[])
{
int a=400,b=3000,c=4200;
if(a>b)
{
if(a>c){
System.out.println(" a is big "+a);
}
}
else if(b>c)
{
System.out.println(" b is big "+b);
}
else{
System.out.println(" c is big "+c);
}
}
}
Loop control statements
-----------------------
Loops perform a repititive task.
we have the follwing loops
i)while
ii)do-while
iii)for
iv)foreach
Any loop requires
-----------------
Initialization
condition
inc/dec
Initialization
--------------
It specifies where loop should begin.
ex:
int i=1;
condition
---------
It specifies how many times a loop should iterate.
ex:
i<=10
inc/dec
-------
increments or decrements a loop
i++;/i--;
while
-----
It performs a repititive task.
syntax
------
while(condition)
{
//statements
inc/dec;
}
Program to print numbers from 1 to 10
-------------------------------------
class Loop{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
Program to print numbers from 10 to 1
-------------------------------------
class Loop{
public static void main(String args[])
{
int i=10;
while(i>=1)
{
System.out.println(i);
i--;
}
}
}
Program to print even numbers
-----------------------------
//10 to 1
class Loop{
public static void main(String args[])
{
int i=2;
while(i%2==0&&i<=100)
{
System.out.println(i);
i=i+2;
}
}
}
sum of 10 natural numbers
-------------------------
class Test{
public static void main(String args[])
{
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
}
System.out.println(sum);
}
}
Print even numbers from 1 to 100
--------------------------------
class Test{
public static void main(String args[])
{
int i=1,sum=0;
while(i<=10)
{
if(i%2==0){
sum=sum+i;
}
i++;
}
System.out.println(sum);
}
}
Print factors of a number
-------------------------
6
1 6%1==0
2 6%2==0
3 6%3==0
4 6%4==0
5 6%5==0
6 6%6==0
class Test{
public static void main(String args[])
{
int i=1,n=8;
while(i<=n)
{
if(n%i==0){
System.out.println(i);
}
i++;
}
}
}
sum of factors
--------------
class Test{
public static void main(String args[])
{
int i=1,n=8,sum=0;
while(i<=n)
{
if(n%i==0){
sum=sum+i;
}
i++;
}
System.out.println(sum);
}
}
perfect number
-------------
sum of the factors if a given number excluding itself must be equivalent to the given number.
6 ->1
2
3
6
Prime or composite
------------------
class Test
{
public static void main(String args[])
{
int i=1,n=2,count=0;
while(i<=n)
{
if(n%i==0)
{
count++;
}
i++;
}
if(count==2)
{
System.out.println("prime");
}
else
{
System.out.println("composite");
}
}
}
do-while
--------
It performs a repititive task until condition is false.
syntax
------
do
{
//statements
}
while(condition);
In do-while body is executed for one time even if condition is false.
class Test
{
public static void main(String args[])
{
int i=10;
do{
System.out.println(i);
i--;
}while(i>=1);
}
}
for loop
--------
repeatitive statamet
syntax
------
for(initialization;condition;inc/dec)
{
//statements
}
class Test
{
public static void main(String args[])
{
int i;
for(i=1;i<=100;i++)
{
if(i%2==0)
System.out.println(i);
}
}
}
switch
------
It is a case control statement.
switch is collection of cases.
syntax
------
switch(expression)
{
case value1:statements;
break;
case value2:statements;
break;
case value3:statements;
break;
...
...
default:statements;
}
In a switch case the value of expression is matched with the value of a case,whichever case is matching the value of expression that case gets executed.
break causes to stop the execution of switch
if none of the cases are matched then default statement gets executed.
Note:
switch doesn't accept decimal and long values
No comments:
Post a Comment