Controlstatement 1
Controlstatement 1
---------------------------------
------
-----
---------
(condition)
{
//statement
}
types:
-----------
1)Decision Making Statements
2)looping.
3)Jumping statements
if
----
--->if condition is true to print if block statements.
syntax:
------------------
if(condition)
{
//statements
}
EX:
------
package Mypack1;
import java.util.Scanner;
int n;
n=obj.nextInt();
if(n>=10)
{
System.out.println("The number is greater than 10");
}
}
}
-----------------------------------------------------------------------------------
EX:
-----
package Mypack1;
import java.util.Scanner;
int n;
n=obj.nextInt();
if(n>=10)
{
System.out.println("The number is greater than 10");
}
System.out.println("Thank you!!!");
}
-------------------------------------------------------------------------------
if..else
----------
--->if condition is true to print if block statements,otheriwise else to be
printed.
syntax:
-----------------
if(condition)
{
//statements
}
else
{
//statements
}
EX:
-----
package Mypack1;
import java.util.Scanner;
int n;
n=obj.nextInt();
if(n>=10)
{
System.out.println("The number is greater than 10");
}
else
{
System.out.println("The number is less than 10");
}
import java.util.Scanner;
int age;
age=obj.nextInt();
if(age>=18)
{
System.out.println("Eligble for Voting");
}
else
{
System.out.println("Not Eligible for Voting..");
}
}
-----------------------------------------------------------------------------------
-----
2)smallest of two number
-------------------------------------------
package Mypack1;
import java.util.Scanner;
int a,b;
a=obj.nextInt();
b=obj.nextInt();
if(a>b)
{
System.out.println(b + " is small");
}
else
{
System.out.println(a + "is small");
}
import java.util.Scanner;
int a;
a=obj.nextInt();
if(a%2==0)
{
System.out.println(a + " is Even");
}
else
{
System.out.println(a + " is Odd");
}
}
-----------------------------------------------------------------------------------
-------
1)A number is Divisible by 5 or not
2)largest of two numbers
3)A year is leap year or not
if..else..if
----------------
---->It check multiple condition in a Program.
syntax:
-----------------
if(condition)
{
//statements
}
else if(condition)
{
//statements
}
else if(condition)
{
//statements
}
else
{
//statements
}
import java.util.Scanner;
char data;
data=obj.next().charAt(0);
}
else if(data>='0' && data<='9' )
{
System.out.println(data+" is a Digit");
}
else
{
System.out.println(data+" is a Special Character");
}
}
--------------------------------------------------------------------------
Check whether a number is one digit,or two digit or three digit,..
-----------------------------------------------------------------------------------
------------
package Mypack1;
import java.util.Scanner;
int number;
-----------------------------------------------------------------------------------
--
4)check whether a character is uppercase or lowercase
5)chack whether a character is vowel or consonent.
6)A number is positive or negative or zero.
7)Enter the mark
---------------------------------
91-100 --->S Grade
81-90 ---->A Grade
71-80- ---->B Grade
61-70 --->C Grade
51-60 --->D Grade
41-50 --->S Grade
0-40 --->Fail
syntax:
-----------------
if(condition)
{
if(condition)
{
//statements
}
else
{
//statements
}
}
else
{
//statements
}
EX:
------
package Mypack1;
import java.util.Scanner;
int age,weight;
age=obj.nextInt();
weight=obj.nextInt();
if(age>=18)
{
if(weight>=50)
{
System.out.println("Eligble For Blood Donate");
}
else
{
System.out.println("Weight is less than 50");
}
}
else
{
System.out.println("Age is less than 18");
}
}
EX:
-----
package Mypack1;
import java.util.Scanner;
int age,weight;
age=obj.nextInt();
weight=obj.nextInt();
else
{
System.out.println("Not Eligible For Blood Donate");
}
}
}
----------------------------------------------------------
switch
---------------
--->multiway branch statement
syntax:
--------------------
switch(option)
{
case value1:
//statements
break
case value1:
//statements
break
case value1:
//statements
break
default:
//statememts
}
EX:
------
choose the course name for the Student choice
--------------------------------------------------------------------------------
1.C
2.C++
3.Java
4.Python
EX:
-----
package Mypack1;
import java.util.Scanner;
int option;
option=obj.nextInt();
switch(option)
{
case 1:
System.out.println("C");
break;
case 2:
System.out.println("C++");
break;
case 3:
System.out.println("Java");
break;
case 4:
System.out.println("Python");
break;
default:
System.out.println("Invalid Option");
---------------------------------------
Choose the grade
-------------------------------
A (or) a---->Average
E(or)e --->Excellent
F(or)f---->fail
G(or)g --->Good
V (or)v --->VeryGood
----------------------------------------------------------------------------------
looping
-------------
types
----------
1)while loop
2)do..while loop
3)for loop
I.while loop
--------------------------
--->Entry check loop
syntax:
-------------
initialization
while(condition)
{
//statements
incre/decre
}
Ex:
------
package Mypack1;
import java.util.Scanner;
int a=1;
while(a<=5)//1<=5//2<=5//3<=5//4<=5//5<=5//6<=5--->false
{
System.out.println(a);//1//2//3//4//5
a++;//++a//a=a+1//a+=1
//1++
//2++
//3++
//4++
//5++
EX:
------
package Mypack1;
import java.util.Scanner;
int a=10;
while(a<=50)
{
System.out.println(a);
a=a+3;
}
EX:
-----
package Mypack1;
import java.util.Scanner;
int a=10;
while(a>=0)
{
System.out.println(a);
a=a-1;
}
}
EX:
-----
package Mypack1;
import java.util.Scanner;
int a=100;
while(a>=50)
{
System.out.println(a);
a=a-5;
}
Hint:
-----------
start---->lowest ,end--->Highest (<,<=)---->(incremental)
start--->highest,end--->lowest(>,>=) ---->(decremental)
=====================================================
do..while
------------------
--->Exit check loop
syntax:
--------------
initalization
do
{
//statements
increment/decrement
}
while(condition);
EX:
-----
package Mypack1;
import java.util.Scanner;
int i=1;
do
{
System.out.println(i);
i++;
}while(i<=5);
}
}
-----------------------------------------------------------------------------------
-
for....loop
--------------------
types:
--------------
1)simple for loop
2)Enhanced for loop (or) for-Each loop
3)nested for loop
4)infinite for loop
EX:
----
package Mypack1;
import java.util.Scanner;
int x;
for(x=1;x<=10;x++)
//x=1;1<=10
//x=2;2<=10
//x=3;3<=10
//...
//x=11;11<=10--->false
{
System.out.println(x);
//1
//2
//3
//...
//10
}
}
EX:
------
package Mypack1;
import java.util.Scanner;
int x;
for( x=1;x<=10;x++)
//x=1;1<=10
//x=2;2<=10
//x=3;3<=10
//...
//x=11;11<=10--->false
{
System.out.println(x);
//1
//2
//3
//...
//10
System.out.println(x);
}
}
-----------------------------------------------------------------------
for-Each (or) Enhanced for loop
------------------------------------------------
syntax:
---------------
for(var:datatype var)
{
//statements
}
EX:
-----
package Mypack1;
// System.out.println(a);
for(int x:a)
{
System.out.println(x);
}
}
----------------------------------------------------------------------------
Infinite loop
---------------------
syntax:
------------------
for(; ;)
{
//statements
}
package Mypack1;
}
-----------------------------------------------------------------------------------
-
Nested for loop
----------------------------
syntax:
-----------------
1 ,10 2 9
for(initalization;condition;incre/decre)
{
3 7 4 8-->F 6
for(initalization;condition;incre/decre)
{
//statements---->5
}
}
*****
*****
*****
*****
*****
EX:
-------
package Mypack1;
for(a=1;a<=3;a++)
//a=1;1<=3
//a=2;2<=3
//a=3;3<=3
{
for(b=1;b<=3;b++)
//b=1;1<=3//b=1;1<=3//b=1;1<=3
//b=2;2<=3//b=2;2<=3//b=2;2<=3
//b=3;3<=3//b=3;3<=3//b=3;3<=3
//b=4;4<=3--->False//b=4;4<=3 --->False//b=4;4<=3-->False
{
System.out.println(a+" "+b);
//1 1
//1 2
//1 3
//2 1
//2 2
//2 3
//3 1
//3 2
//3 3
}
}
}
/*
a=1,b=1,2,3
b=2,b=1,2,3
c=3,b=1,2,3
*/
------------------------------------------------
Jumping statements
--------------------------------------
1)break
2)continue
break
------------
--->it is used to stop the flow of a program.
syntax:
---------------
break;
Ex:
-------
package Mypack1;
for(i=1;i<=10;i++)
{
if(i==7)
{
break;
}
System.out.println(i);
}
}
}
---------------------------------------------------------------
continue
------------------
--->it is used to skip the flow of a program.
syntax:
---------------
continue
EX:
-----
package Mypack1;
for(i=1;i<=10;i++)
{
if(i==7)
{
continue;
}
System.out.println(i);
}
}
}
EX:
-----
package Mypack1;
for(i=1;i<=10;i++)
{
if(i==7 || i==3 || i==8 || i==5)
{
continue;
}
System.out.println(i);
}
}
}
-----------------------------------------------------------------------------------
----------------------------
looping Programs
--------------------------------
1)Reverse number
--------------------------------
EX:
------
input---->12345
output---->54321
n=12345
s=0
while(n!=0)
{
a=n%10
s=s*10+a
n=n/10
}
while(12345!=0)
{
a=12345%10 --->5
s=0*10+5 --->5
n=12345/10 --->1234
}
while(1234!=0)
{
a=1234%10---->4
s=5*10+4 --->54
n=1234/10 --->123
}
while(123!=0)
{
a=123%10---->3
s=54*10+3 --->543
n=123/10 -->12
}
while(12!=0)
{
a=12%10 --->2
s=543*10+2 ---->5432
n=12/10--->1
}
while(1!=0)
{
a=1%10 --->1
s=5432*10+1 ---->54321
n=1/10---->0
}
while(0!=0)
{
//false
}
EX:
-----
package Mypack1;
import java.util.Scanner;
while(n!=0)
{
a=n%10;
s=s*10+a;
n=n/10;
}
343 --->343
Ex:
-------
package Mypack1;
import java.util.Scanner;
a1=n;
while(n!=0)
{
a=n%10;
s=s*10+a;
n=n/10;
}
if(a1==s)
{
System.out.println("given number is Palindrome");
}
else
{
System.out.println("Not a Palindrome number");
}
}
}
-------------------------------------------------------------------
1)sum of the digits
EX:
-----
1234
1+2+3+4=10