Вы находитесь на странице: 1из 51

Content

Pre increment and Post increment


Substring in java
Nested Loop
While loop
Do while loop
Break and continue

Pre & Post increment


increment & Decrement Operators

x++

Post-increment : add 1 to the value.


The value is returnedbeforethe increment is made, e.g.
x = 1;
y = x++;
Then y will hold 1 and x will hold 2

x--

Post-decrement : subtract 1 from the value.


The value is returnedbeforethe decrement is made, e.g. :
x = 1;
y = x--;
Then y will hold 1 and x will hold 0.

++x

Pre-increment : add 1 to the value.


The value is returnedafterthe increment is made, e.g.
x = 1;
y = ++x;
Then y will hold 2 and x will hold 2.

--x

Pre-decrement : subtract 1 from the value.


The value is returnedafterthe decrement is made, e.g.
x = 1;
y = --x;
Then y will hold 0 and x will hold 0.
2

***Pre & Post increment


example

Ques 1: without using variables to hold


incremented or decremented value

Ans1
4
6

int sum1 = 3;
sum1++;
jTextField1.setText(""+sum1);
++sum1;
jTextField2.setText(""+(++sum1));
Ques 2: Using variables to hold
incremented or decremented value
int sum1 = 3;
int n1=sum1++;
jTextField1.setText(""+n1);
int n2=++sum1;
jTextField2.setText(""+(++n2));

Ans2
3
6
3

Pre increment
Output question 3
// value of for loop will be incremented
or decremented once loop gets
completed.
int x,y=0;
for(x=1;x<=5;++x)
{
System.out.println(" x is after loop"
+" "+x);
y=++x;
System.out.println(" y=++x x is "
+" "+x);
System.out.println(" y=++x " +"
"+y);
--y;
System.out.println(" - -y " +" "+y);
}
System.out.println("x is"+x);
System.out.println(y);

Ans 3 :
x is after loop 1
y=++x x is 2
y=++x 2
- -y 1
x is after loop 3
y=++x x is 4
y=++x 4
- -y 3
x is after loop 5
y=++x x is 6
y=++x 6
- -y 5
x is7
5
5

Post increment
Ques 4
int x,y=0;
for(x=1;x<=5;x++)
{
System.out.println(" x is after
loop" +" "+x);
y=x++;
System.out.println(" y=x++ "
+" "+y);
-- y;
System.out.println(" - -y " +"
"+y);
}
System.out.println("x is"+x);
System.out.println(y);

Ans 4:
x is after loop 1
y=x++ 1
- -y 0
x is after loop 3
y=x++ 3
- -y 2
x is after loop 5
y=x++ 5
- -y 4
x is7
4

Q5
int sum1 = 3;
int sum2=sum1++ +2;
jTextField1.setText(""+sum2);
++sum1;
jTextField2.setText(""+(++sum1));

Ans5
5
6

Q6
int val1=5,val2=10;
for(int i=1;i<=3;i++)
{
system.out.println( +val1++ +, +
--val2);
system.out.println( +val2-- +, + +
+val1);

10

Ans5
5 ,9
9,7
7,7
7,9
9,5
5,11

11

substring in java

Output question
String str = "life is beautiful;
String val1 = str.substring(0,10);
String val2 = str.substring(0,6);
String val3 = str.substring(8);
String v3=str.substring(0);
String v4=str.substring(1);
System.out.println("substr at 0,10 position is" +"
"+val1);
System.out.println("substr at o ,6 is" +" "+val2);
System.out.println("substr at 8 is" +" "+val3);
System.out.println("substr at o is" +" "+v3);
System.out.println("substr at 1 is" +" "+v4);
0

1
0

1
1

1
2

1
3

1
4

1
5

1
6

12

1
7

substring in java

Ans
substr
substr
substr
substr
substr

at
at
at
at
at

0,10 position is life is be


o ,6 is life i
8 is beautiful
o is life is beautiful
1 is ife is beautiful

13

Nested loop
The placing of one loop inside the body of another loop is
callednesting.

the outer loop takes


control of the number of complete
repetitions of the inner loop.
When working with nested loops, the outer loop
changes only after the inner loop is completely
finished
When you "nest" twoloops,

14

Working of nested loop


Memory

Screen

int num2

for(num2 = 0; num2 <= 3;


num2++)
{
for(num1 = 0; num1 <= 2;
num1++)
{
System.out.println(num2
+ " " + num1);
}
}

int num1
0

0
1
2
3end loop

0
1
2
3end loop

0
1
2
3end loop

0
15

Nested loop
Ans :

Ques
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" "+i);

1
22
333
4444

}
System.out.println();
}

16

Nested loop
Ans :

Ques
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
System.out.print(" "+i);

1
2
3
4

1
2
3
4

1
2
3
4

1
2
3
4

}
System.out.println();
}

17

Nested loop
Ans :

Ques
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" "+j);

1
12
123
1234

}
System.out.println();
}

18

Nested loop
Ans :

Ques
for(int i=1;i<=4;i++)
{
for(int j=4;j>=i;j--)
{
System.out.print(" "+j);

4321
432
43
4

}
System.out.println();
}

19

Nested loop
Ques TRY YOURSELF
for(int i=2;i<=5;i++)
{
System.out.print(i+ "=>");
for(int j=1;j<=3;j++)
{
System.out.print(i*j +",");
}
System.out.println();
}

20

Nested loop
Ans :
2=>2,4,6,
3=>3,6,9,
4=>4,8,12,
5=>5,10,15,

21

Output using while loop


Ques :
int j=1,s=0;
while(j<5)
{
System.out.print(j+ "+");
s=s+j;
j=j+j%3;
}
System.out.println("=" +s);

22

Ans 6:
1+2+4+=7

23

Q7
int a=10;
int b=12;
int x=5;
int y=6;
while (a<=b)
{
if (a%2==0)
{
x=x + y;
a=a+1;
}
else
{
x=x-y;
a=a+1; }
System.out.println(x);
}

24

Ans.
11
5
11

25

Output using do while loop


Ques :
int i,j,n;
n=0;i=1;
do{
n++;
i++;
}while(i<=5);
System.out.println(n);
System.out.println(i);
Ans
5
6

26

JUMP Statement
(i) break : The break is used to break from an enclosing
do,whilefor,or switch statement.
Syntax:
break;
()A break statement skips the rest of the loop and jumps
over to the statement following the loop.
()If a break statement appers in a nested loop , then it
causes an exit from only the very loop it appears in.

27

break statement
int i ,c;
int a=Integer.parseInt(jTextField1.getText());
int b=Integer.parseInt(jTextField2.getText());
for(i=0;i<20;i++)
{
if(b==0)
break;
else
c=a/b;
System.out.println("Quotient ="+c);
}
28

Jump Statement

(ii) continue: The continue statement stops the execution of the


current iteration and causes control to begin with next iteration.

Syntax:
continue;
For the for loop continue causes the nex iteration by updating the
variable and then causing the test expressions evaluation .
For the while and do while loops , the program control passes to the
conditional tests.

29

int i ,c;
int a=Integer.parseInt(jTextField1.getText());
int b=Integer.parseInt(jTextField2.getText());
for(i=0;i<20;i++)
{
if(b==0)
{
System.out.println("the denominator can't be zero enter again");
continue;
}
else
{ c=a/b;
System.out.println("Quotient ="+c);
}
}
System.out.println("code over!");

30

Jump Statement
(iii) return : Return is used to return
value from the method
Syntax:
Return <value>;

31

To be continued
RDBMS MYSQL
Constraints
Web applications <HREF> , <A>
tag

32

Web Application

33

Hyperlink

The link between web pages is known as hyperlink. It is the link


that leads from one document to another.

Using Anchor tag <a> Hyperlink can be be created between web


pages or link between webpage and image .<a> tag is used with
href to define path .

Purpose of using the attribute HREF in tag <A>?


Ans. HREF is used with tag A to specify the Hyper text Reference
link to some HTML page/Website.

34

Hyperlink

<html>
<head>
<title> web1 </title>
</head>
<body>
<!webpage>
<a href="d:\hills.jpg">click </a>
0<sub>2 </sub>
z<sup>2</sup>
</body>
</html>

35

BODY tag attributes


Attribute: Link
This sets the colour of all of the non-visited links on the page.
We can set this to any color of our choice. The default setting
for a non-visited link is usually blue.
Syntax: Link="colorname" OR LINK = "#rrggbb"
Attribute: Alink
This sets the colour of active links on the page. An active link is
a link that has just been keypressed by the user
Alink="colorname" OR ALINK = "#rrggbb
Attribute: Vlink
This changes the colour of a visited link on the page. We can set
this to any color. The default setting for a visited link is usually
violet.
Vlink="colorname" OR VLINK = "#rrggbb"
36

By default, links will appear as follows in all browsers:


An unvisited link is underlined and blue
A visited link is underlined and purple
An active link is underlined and red

<html>
<head>
<title> web1 </title>
</head>
<body link="green" alink="red" vlink="blue">
<!webpage>
<a href="d:\hills.jpg">click </a>
0<sub>2 </sub>
z<sup>2</sup>
</body>
</html>
37

Icomment line in HTML code


In HTML code, the comment line is declared as : <! - Any text - >
Shown in the last slide
Sub Script & Super Script in HTML
Using <sub >. </sub> tag
Using <sup> . </sup>tag
External linking and Internal linking
External linking links a document with a separate HTML file
whereas the Internal linking links a section in the same HTML file.
Extension of JPEG:- Joint Pictures Expert Group
common graphics file formats that most browsers
recognize
The two common graphics file formats are: .gif and .jpg
<basefont>Tag in HTML : Specify a default font-color and fontsize for text on page:
<basefont color="red" size="5">

38

To Write in Hindi (and other regional


languages) on the Web Page
lang attribute with a suitable value is added in the <HTML>
tag, the whole web page is displayed in the specified
language.
<html lang="hi">
Instead of "hi use "ks for Kashmiri, "kok for
Konkani, "nefor Nepali "mr for Marathi , "sa" for
Sanskrit
respectively. If we want to use multiple languages on the
same page,
we can write this attribute with the <p> tag also.
<p lang="hi">

39

RDBMS

RELATIONAL DATABASE MANAGEMENT


SYSTEM

40

Relational DataBase Management System(RDBMS) :


In a relational data model, the data is organizedinto tables (i.e.
Rows and Columns). These tables are called Relations. A row
in a table represents a relationship among a set of values. Since
table is a collection of relationships it is generally referred to
using the mathematical term Relation.
Different Data Models :
Relational data model :In this model data is organized into tabular
structures called relations
Hierarchical data model : data is organized in Tree form
Network data model: data is organized using link
Object Oriented data model

41

SQL Constraints/ Integrity Constraints


Data constraints are the rules that are defined when a table is
created.
They can also be defined or modified after creating the tables.
When constraints are defined any data entering in the table is
first checked to satisfy the condition specified in particular
constraint if it is, only then table data can be updated. If data
updation/ insertion is violating the defined constraints, database
rejects the data
(entire record is rejected).
When a constraint is applied to a single column, it is called a
column level constraint but if a constraint is applied on a
combination of columns it is called a table constraint.

42

Following constraints can be defined on


a table in SQL:

ENUM :Defines a set of values as the column domain. So any


value in this column will be from the specified values
only.
SET :Defines a set of values as the column domain. Any value
in this column will be a subset of the specied set only.
43

SQL Constraints
Primary Key :No two rows of a table can have the same primary.
If in any situation you need to have two primary key , key
If you
declare two primary key in the table in the following way
CREATE TABLE bills
(Order_Num INT(4) PRIMARY KEY,
cust_code VARCHAR(4) PRIMARY KEY,
bill_Date DATE, Bill_Amt DECIMAL(8,2));
it will not accept. It will give error as a table can have at most one
primary
So you need to declare combination of columns a primary
key
CREATE TABLE bills
(Order_Num INT(4), cust_code VARCHAR(4),
bill_Date date, Bill_Amt DECIMAL(8,2),
PRIMARY KEY(Order_Num, cust_code));

44

SQL Constraints
Primary Key :No two rows of a table can have the same primary.
If in any situation you need to have two primary key , key
If you
declare two primary key in the table in the following way
CREATE TABLE bills
(Order_Num INT(4) PRIMARY KEY,
cust_code VARCHAR(4) PRIMARY KEY,
bill_Date DATE, Bill_Amt DECIMAL(8,2));
it will not accept. It will give error as a table can have at most one
primary
So you need to declare combination of columns a primary
key
CREATE TABLE bills
(Order_Num INT(4), cust_code VARCHAR(4),
bill_Date date, Bill_Amt DECIMAL(8,2),
PRIMARY KEY(Order_Num, cust_code));

45

To add primary key using Alter Add


ALTER TABLE shop ADD PRIMARY KEY(code);

Suppose you want to delete primary key and want to set new set of
column as primary key then
ALTER TABLE Shoes DROP PRIMARY KEY;
ALTER TABLE Shoe ADD PRIMARY KEY (Name, Size);

Not Null Constraints


A column with aNOT NULLconstraint, cannot have NULL values.
mysql> CREATE TABLE People(Id INTEGER, LastName varchar(10) NOT
NULL, FirstName varchar(10) NOT NULL, City VARCHAR(55));
Query OK, 0 rows affected (0.07 sec)
mysql> INSERT INTO People VALUES(1, NULL, 'Marianne', 'Chicago');
ERROR 1048 (23000): Column 'LastName' cannot be null
In MySQL, it is not possible to add or drop NOT NULL constraint
explicitly after the table but it can be done using MODIFY clause of
ALTER TABLE command

ALTER TABLE bills MODIFY bill_date DATE NOT NULL;


46

UNIQUE constraint
TheUNIQUEconstraint ensures, that all data are unique in a column.

mysql> CREATE TABLE Brands(Id INTEGER, BrandName VARCHAR(30)


UNIQUE);
Query OK, 0 rows affected (0.08 sec)
mysql> INSERT INTO Brands VALUES(1, Maza');
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO Brands VALUES(2, Rasna');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO Brands VALUES(3, Rasna');
ERROR 1062 (23000): Duplicate entry Rasna' for key 'BrandName
Note : Primary keys cannot be NULL, unique keys can be.

47

ENUM constraint
AnENUMis a string object with a value chosen from a list of permitted values.
They are enumerated explicitly in the column specification at table creation
time

48

SET constraint
A SET can have zero or more values. Each of the values must be chosen from a
list of permitted values. It is different from theENUMconstraint, where you
can have only one distinct value from the list of permitted values.

49

HAVING

Clause

When we do not want to see the whole output


produced by a statement with GROUP BY clause.
When we want to put some condition on individual groups
(and not on individual records) then having is used
SELECT type, SUM(qty) FROM shoes
GROUP BY type HAVING SUM(qty) > 1500;
In these statements if we try to put the condition using
WHERE instead of HAVING, we shall get an error. Another
way of remembering this is that whenever a
condition involves an aggregate function, then we
use HAVING clause in conjunction with GROUP BY
clause.

50

Extra

SELECT CONCAT(KDAV,NULL,Reliance Greens);


Ans NULL

51

Вам также может понравиться