0% found this document useful (0 votes)
5 views26 pages

Controlstatement 1

The document provides an overview of control statements in programming, including decision-making, looping, and jumping statements. It details various types of decision-making statements such as if, if-else, and switch, along with examples for each. Additionally, it covers looping constructs like while, do-while, and for loops, as well as jumping statements like break and continue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views26 pages

Controlstatement 1

The document provides an overview of control statements in programming, including decision-making, looping, and jumping statements. It details various types of decision-making statements such as if, if-else, and switch, along with examples for each. Additionally, it covers looping constructs like while, do-while, and for loops, as well as jumping statements like break and continue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Control statements

---------------------------------
------
-----
---------
(condition)
{
//statement
}

types:
-----------
1)Decision Making Statements
2)looping.
3)Jumping statements

1)Decision Making Statements


----------------------------------------------------
1)if
2)if..else(*)
3)if..else..if(*)
4)nested..if
5)switch(*)

if
----
--->if condition is true to print if block statements.

syntax:
------------------
if(condition)
{
//statements
}

EX:
------
package Mypack1;

import java.util.Scanner;

public class Myclass1


{

public static void main(String[] args)


{

int n;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the number");

n=obj.nextInt();
if(n>=10)
{
System.out.println("The number is greater than 10");
}
}

}
-----------------------------------------------------------------------------------
EX:
-----
package Mypack1;

import java.util.Scanner;

public class Myclass1


{

public static void main(String[] args)


{

int n;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the number");

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;

public class Myclass1


{

public static void main(String[] args)


{

int n;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the number");

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");
}

1)A Person is eligible for voting


-----------------------------------------------------
package Mypack1;

import java.util.Scanner;

public class Myclass1


{

public static void main(String[] args)


{

int age;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the number");

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;

public class Myclass1


{

public static void main(String[] args)


{

int a,b;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the numbers");

a=obj.nextInt();
b=obj.nextInt();

if(a>b)
{
System.out.println(b + " is small");
}

else
{
System.out.println(a + "is small");
}

3)Number is Even or odd


-----------------------------------------------
package Mypack1;

import java.util.Scanner;

public class Myclass1


{
public static void main(String[] args)
{

int a;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the number");

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
}

check whether a character is alphabet,digits or special character.


-----------------------------------------------------------------------------------
----------------------
package Mypack1;

import java.util.Scanner;

public class Myclass1


{

public static void main(String[] args)


{

char data;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Character");

data=obj.next().charAt(0);

if(data>='a' && data<='z' || data>='A' && data<='Z')


{
System.out.println(data+" is a Alphabet");

}
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;

public class Myclass1


{

public static void main(String[] args)


{

int number;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Number");


number=obj.nextInt();

if(number>=0 && number<10)//0 to 9


{
System.out.println("Number is One Digit");
}
else if(number>=10 && number<100)//10 to 99
{
System.out.println("Number is Two Digit");
}
else if(number>=100 && number<1000)//100 to 999
{
System.out.println("Three Digit");
}
else if(number>=1000)
{
System.out.println("Above 1000");
}
else
{
System.out.println("Invalid 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

else --->Invalid Mark


-----------------------------------------------------------------------
Nested...if
---------------------
An if condition inside another if condition.

syntax:
-----------------
if(condition)
{
if(condition)
{
//statements
}
else
{
//statements
}
}
else
{
//statements
}

age>=18,weight>=50 --->eligible for Blood Donate.

EX:
------
package Mypack1;

import java.util.Scanner;

public class Myclass1


{

public static void main(String[] args)


{

int age,weight;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Age and 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;

public class Myclass1


{
public static void main(String[] args)
{

int age,weight;

Scanner obj=new Scanner(System.in);

System.out.println("Enter the Age and Weight");

age=obj.nextInt();
weight=obj.nextInt();

if(age>=18 && weight>=50)


{
System.out.println("Eligble For Blood Donate");
}

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;

public class Myclass1


{

public static void main(String[] args)


{

int option;

Scanner obj=new Scanner(System.in);

System.out.println("***Welcome to Besant Technologies***");


System.out.println("1.C\n 2.C++\n 3.java\n 4.Python");
System.out.println("Enter the 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 day name for the given option


----------------------------------------------------------------------
1)sunday
2)Monday
3)tuesday
4)wednesday
5)Thursday
6)Friday
7)Saturday

---------------------------------------
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;

public class Myclass1


{

public static void main(String[] args)


{

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;

public class Myclass1


{

public static void main(String[] args)


{

int a=10;

while(a<=50)
{
System.out.println(a);
a=a+3;
}

EX:
-----
package Mypack1;

import java.util.Scanner;

public class Myclass1


{

public static void main(String[] args)


{

int a=10;

while(a>=0)
{
System.out.println(a);
a=a-1;
}

}
EX:
-----
package Mypack1;

import java.util.Scanner;

public class Myclass1


{

public static void main(String[] args)


{

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;

public class Myclass1


{

public static void main(String[] args)


{

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

I.simple for loop


--------------------------------
syntax:
---------------
1 ,5 2,6 4,8
for(initialization;condition;increment/decrement)
{
//statements---->3,7
}

EX:
----
package Mypack1;

import java.util.Scanner;

public class Myclass1


{

public static void main(String[] args)


{

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;

public class Myclass1


{

public static void main(String[] args)


{

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;

public class Myclass2 {

public static void main(String[] args) {


//int a=10;

int a[]= {12,56,89,23,75,23};

// System.out.println(a);

for(int x:a)
{
System.out.println(x);
}

}
----------------------------------------------------------------------------
Infinite loop
---------------------
syntax:
------------------
for(; ;)
{
//statements
}

package Mypack1;

public class Myclass2 {

public static void main(String[] args) {


for(;;)
{
System.out.println("This is Infinite for loop");
}

}
-----------------------------------------------------------------------------------
-
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;

public class Myclass2 {

public static void main(String[] args)


{
int a,b;

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;

public class Myclass2 {

public static void main(String[] args)


{
int i;

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;

public class Myclass2 {

public static void main(String[] args)


{
int i;

for(i=1;i<=10;i++)
{
if(i==7)
{
continue;
}
System.out.println(i);
}
}
}

EX:
-----
package Mypack1;

public class Myclass2 {

public static void main(String[] args)


{
int i;

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;

public class Myclass2 {

public static void main(String[] args)


{
int n,s=0,a;
Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");


n=sc.nextInt();

while(n!=0)
{
a=n%10;
s=s*10+a;
n=n/10;
}

System.out.println("Reverse number is "+s);


}
}
-----------------------------------------------------------------------------------
-------
palindrome
-------------------------
121----->121

343 --->343

Ex:
-------
package Mypack1;

import java.util.Scanner;

public class Myclass2 {

public static void main(String[] args)


{
int n,s=0,a,a1;
Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");


n=sc.nextInt();

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

2)count the digits


-------------------------------
EX:
-----
1213233
Output
-------------
7
--------------------------------------------------------------------------------
1)sum and average of 1 to 10
2)find the cube between 1 to 5
3)find the even number upto 1 to 25 and their sum.
4)factorial of a number
5)multiplication table
-----------------------------------------------------------------------------------
----

You might also like