Java Comparison Operators
Comparison
Operators दी
हुई दो values
को compare
करके condition
के according
boolean value true / false value return करते हैं।
Java
में Comparison
Operators को
कुछ इस तरह से use
कर सकते हैं।
Operator |
Name |
Example |
Explanation |
== |
Equa |
x = = y |
checks if x is equal to y. |
!= |
Not equal |
x !=y |
checks if x is not equal to y. |
< |
Less than |
x < y |
checks if x is less than y. |
> |
Greater than |
x > y |
checks if x is greater than y. |
<= |
Less than or equal |
x <= y |
checks either x is less than or equal to y. |
>= |
Greater than or equal |
x >= y |
checks either x is greater than or equal to
y. |
Java Logical Operators
Logical
Operators , एक
या एक से अधिक expression
के according
Boolean value return करते हैं। जैसे -
Operator |
Name |
Example |
Explanation |
&& |
And |
x && y |
returns True if Both operands (x and y) are
true; |
! |
Not |
!x |
Returns True if x is not true; |
|| |
Or |
x || y |
returns True if either x or y is true; |
प्रोग्रामिंग लैंग्वेज में कण्ट्रोल स्टेटमेंट का प्रयोग किसी भी प्रोग्राम को कण्ट्रोल करने के लिए किया जाता है. किसी भी प्रोग्राम फ्लो में किस स्टेटमेंट को किस कंडीशन के आधार पर एक्सीक्यूट कराना है अथवा कितनी बार एक्सीक्यूट कराना है यह कार्य कण्ट्रोल स्टेटमेंट द्वारा किया जाता है.
कंडीशनल स्टेटमेंट (Conditional Statement)
किसी
भी प्रोग्राम में किसी कंडीशन के आधार पर निर्णय लेने के लिए कंडीशनल स्टेटमेंट का प्रयोग किया जाता है. किन्ही दो ऑप्शन में से कंडीशन के आधार पर एक ऑप्शन को सिलेक्ट करने के लिए यह आवश्यक होता है.
Java if
Statement
If statement , सबसे basic
decision making statement है जिसमें किसी particular
condition के
true होने पर ही code
of block run होता
है।
if(condition)
{
//
code of block.
}
Java if Example
public class IfElse {
public static void main(String[] args) {
int age = 25;
if(age >= 18) {
System.out.println("Yes ! You are eligible to vote.");
}
}
}
Output
javac
IfElse.java
java
IfElse
Yes
! You are eligible to vote.
किसी expression
द्वारा boolean
value (true / false) return करने लिए Comparison
और Logical
Operators का
use किया जाता है। Comparison
Operators दी
हुई दो values
को compare
करके condition
के according
boolean value true / false value return करते हैं।
Operator |
Name |
== |
Equal |
!= |
Not
equal |
< |
Less
than |
> |
Greater
than |
<= |
Less
than or equal |
>= |
Greater
than or equal |
&& |
And |
! |
Not |
|| |
Or |
Java If Else
जब हमें Condition true होने पर कोई दूसरा code
run करना
हो और condition false होने पर कुछ और तब हम If
else statement का
use करते हैं।
Syntax
if (condition) {
// code if condition is true
}
else {
// code if condition is false
}
public class IfElse {
public static void main(String[] args) {
int age = 17;
if(age >= 18) {
System.out.println("Yes ! You are eligible to vote.");
}
else {
System.out.println("Sorry ! You are not eligible to vote.");
}
}
}
Output
javac
IfElse.java
java
IfElse
Sorry
! You are not eligible to vote.
Important
अगर if
else के
code of block में कोई single
line code है
तो आप if
और else
के बाद use
होने वाले curly
brackets को
skip कर सकते हैं। लेकिन ध्यान रहे सिर्फ single
line के
code के लिए ,
एक से ज्यादा lines
होने पर error
आ जाएगी।
int
age = 25;
if(age
>= 18)
System.out.println("Yes ! You are
eligible to vote.");
else
System.out.println("Sorry ! You are not
eligible to vote.");
कंडीशनल स्टेटमेंट else...if
else...if Syntax
if (condition1) {
// code 1
} else if (condition2){
// code 2
} else {
// code 3
}
public class
IfElse {
public static void main(String[] args) {
int age = 25;
String result = null;
// use ternary operators to check
condition.
result = (age >= 18) ? "Yes ! You
are eligible to vote." : "Sorry ! You are not eligible to
vote.";
System.out.println(result);
}
}
Output
javac
IfElse.java
java
IfElse
Yes
! You are eligible to vote.
Loops Statements
पिछले topic
में आपने Java
में if else के बारे में पढ़ा और समझा ,
लेकिन क्या हो अगर हमें किसी particular
code of block को repeatedly run करना हो ,
तो क्या है उतनी बार ही उस code
को लिखेगे ?
नहीं वहां पर हम Loop use करेंगे।
किसी
भी प्रोग्राम में लूप के उपयोग से प्रोग्राम कोड को आवश्यकतानुसार रिपीट किया जा सकता है. यह प्रोग्रामिंग में समय की बचत एवं गलतियों को कम करने के लिए प्रयोग किया जाता है.
Actually किसी code of block को repeatedly run करने का सबसे easy way looping है , Java में different -
different Looping Statements हैं -
·
while Statement
·
do while Statement
·
for Statement
·
switch Statement
- While Statement
While Syntax
while
(expression)
{
Statement1
Statement2
Statement3....
}
Program
public
class WhileLoop
{
public static void main(String[] args)
{
int num = 1;
while (num <= 10)
{
System.out.println(num);
// increase value by one using post
increment.
num++;
}
}
}
Output
javac
WhileLoop.java
java
WhileLoop
1
2
3
4
5
6
7
8
9
10
Explanation
- सबसे पहले एक integer variable num initialize
किया
जिसकी
value 1 है।
- after
that while loop में entry करने से पहले num की value check की, कि इसकी value 10 या 10 से कम ही हो।
- अगर num की value 10 से कम है तो loop में entry की , num की value print करायी।
- उसके बाद num की value 1 से increment की।
Curly braces are
not required for a single-line body
int num = 1;
while (num <= 5)
System.out.println(num++);
.
Output
:
1
2
3
4
5
do..while Loop Statement
जावा में do...while
लूप का फ्लो चार्ट निम्नानुसार है:
do while loop syntax
do {
Statement1
Statement2
Statement3....;
} while (expression) ;
{
document.write("Current
Count : " + count + ");
count++;
public class DoWhileLoop {
public static void main(String[] args) {
int num = 1;
do {
System.out.println(num);
// increase value by one using post increment.
num++;
}
while (num <= 10);
}
}
Output
javac DoWhileLoop.java
java DoWhileLoop
1
2
3
4
5
6
7
8
9
10
पिछले topic
में while
loop के
same example को do
while loop के
through किया है ,
आप output
में देख सकते हैं कि output same ही आया है ,
बस program
का structure
change हो
गया है। well
, do while loop हम वहां पर use
करते हैं ,
जब हमें किसी loop
के अंदर का code
of block कम
से कम एक बार तो code
of block run करना
ही हो।
Difference between while loop And do while
loop
while
loop और
do while loop में यही main difference भी है , While Loop में सबसे पहले condition
ही check
होती है उसके बाद ही code
of block run होता
है ,
अगर condition false है तो loop
में entry
ही नहीं होगी ,
उसके उलट do while loop में सबसे पहले code of block run होगा और सबसे end
में condition
check होती
है ,
इससे कोई फर्क नहीं पड़ता कि condition
सही है या गलत ,
Loop को
एक बार Run
होना ही है।
Java Infinitive do-while Loop
do
while Loop किसी
भी तरह के Loop
को infinite
run करने
के लिए pass
होने वाली condition true होनी चाहिए। do
while Loop में
आपको कोई भी ऐसा expression
देना है जो कि हमेशा true value return करता हो।
do
{
//code of block to be executed
}
while(true);
हालाँकि कभी भी किसी भी loop
को infinite
run नहीं
करना चाहिए अलग run
करते भी तो उस program
को आप ctrl + c से terminate
कर सकते हो।
public class InfiniteLoop {
public static void main(String[] args) {
do {
System.out.println("it will run infinite...");
}
while (true); // pass true so that it can run infinite times.
}
}
Output
javac InfiniteLoop.java
java InfiniteLoop
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
it will run infinite...
ctrl+c
for Loop Statements
for लूप किसी लूप कण्ट्रोल स्टेटमेंट का सबसे कॉम्पैक्ट फॉर्म है. for
लूप कण्ट्रोल स्टेटमेंट में कोई भी स्टेप उस समय तक रिपीट किया जाता है जब तक कि वह दी गई कंडीशन को पूरा ना कर ले, कंडीशन गलत (false)
होने पर लूप कण्ट्रोल समाप्त हो जाता है।
for(initialization
; condition ; increment / decrement)
{
//code of block
}
तो
Syntax में आप देख सकते हैं कि for
loop में
, हम तीन Expression
देते हैं ,
जो कुछ इस तरह से run
होते हैं।
- first expression : for loop में initial expression हैं जहाँ हम किसी variable को define करते हैं। और यह for loop में सिर्फ एक बार ही run होता है।
- second expression conditional expression होता है और हर iteration में second expression execute होता , condition true होने पर ही loop में entry होती है otherwise हम loop से बाहर हो जाते हैं।
- सबसे last में third expression रन होता है , जहां पर हम किसी variable को increment / decrement करते हैं। यह हर iteration के last में ही execute होता है। हालाँकि यह Optional होता है , यह variable हम loop के अंदर भी increment / decrement कर सकते हैं।
Java for loop
example
while loop और do while loop में हमने जो example लिया था 1 से 10 numbers तक print करने का , वही same example हम यह भी लेंगे ताकि आपको अच्छे से क्लियर हो सके।
public
class ForLoop
{
public static void main(String[] args) {
for(int num = 1; num <= 10; num++) {
System.out.println(num);
}
}
}
Output
javac
ForLoop.java
java
ForLoop
this is
1st expression, and it will run only 1 time.
1
2
3
4
5
Java nested for loop
ठीक
Nested if else या nested
while loop की
तरह ही हम nested for loop भी use
कर सकते हैं। मतलब for
loop के
अंदर एक और for
loop.
Java nested for loop Example
public class ForLoop {
public static void main(String[] args) {
int num = 1;
for(int x=1;x<=5; x++) {
for(int y=1; y<=x; y++) {
System.out.print(y + " ");
}
// for line break.
System.out.print("\n");
}
}
}
Output
javac ForLoop.java
java ForLoop
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Java switch-case
Java
में switch
loop, किसी
matched expression के लिए code
of block Run करता
है ,
यह लगभग else if की तरह ही work
करता है जहा हम कई सारी conditions
में से true
condition वाला
statement ही run
होता था, और अगर एक भी condition
match नहीं
होती तो else
part (default) run होता था।
switch में हम case clause use करते हैं ,
और जो case
expression से
match करता है वही statement execute करता है। और कोई case
match न
होने पर default statement execute होता है।
Java switch Syntax
switch
(expression)
{
case valueN:
// code of block
break;
case valueN:
// code of block
break;
default:
// default case
}
- expression - दिया गया expression, case में दी गयी value से match होता है।
- case
clause -
एक switch
loop में कितने ही case clause हो सकते हैं , और case में दी जाने वाली value दिए गए expression से match करती है अगर expression match होता है ,तो उस case से associated code of block रन हो जाता है।
- break - expression match होने पर उस case से associated code of block run होने के बाद break statement switch loop को terminate करता है।
- default - अगर कोई भी expression match नहीं होता है तो default statement run होता है।
Java switch Example
public class SwitchExample {
public static void main(String[] args) {
int month_no = 4;
switch(month_no) {
case 1:
System.out.println("Month : January");
break;
case 2:
System.out.println("Month : February");
break;
case 3:
System.out.println("Month : March");
break;
case 4:
System.out.println("Month : April");
break;
case 5:
System.out.println("Month : May");
break;
default:
System.out.println("Month after the May..");
}
}
}
Output
javac SwitchExample.java
java SwitchExample
Month : April
Note : Java programming
language में
ध्यान रहे कि आप ,
जिस data
type की
value pass कर रहे हैं switch case
clause में
match होने वाली values
भी उस type
की होनी चाहिए। ऐसा नहीं हो सकता है कि
match की जाने वाली values
integer हैं
और switch
में आप String
value pass कर
रहे हैं ,
ऐसा करने पर error
आएगी।
Java switch without
break
अगर आप break statement का use नहीं करते हैं तो , जिस case से condition match होती है , वहां से सभी cases का block of code run होता है।
public class SwitchExample {
public static void main(String[] args) {
int month_no = 3;
switch(month_no) {
case 1:
System.out.println("Month : January");
case 2:
System.out.println("Month : February");
case 3:
System.out.println("Month : March");
case 4:
System.out.println("Month : April");
case 5:
System.out.println("Month : May");
default:
System.out.println("Month after the May..");
}
}
}
Output
javac SwitchExample.java
java SwitchExample
Month : March
Month : April
Month : May
Month after the May..
default case in Java switch-case
जैसा कि आपने ऊपर पढ़ा कि अगर कोई case match नहीं होता है तो default
case हो
run होता है।
public class SwitchExample {
public static void main(String[] args) {
int month_no = 3;
switch(month_no) {
case 1:
System.out.println("Month : January");
break;
case 2:
System.out.println("Month : February");
break;
default:
System.out.println("Default case is running.");
}
}
}
Output
javac SwitchExample.java
java SwitchExample
Default case is running.
Java
Array
अभी तक आपने को भी examples
देखे , उनमे हमें variables को अलग - अलग define करना पड़ता था , लेकिन variables में एक problem थी कि वह सिर्फ single value को store कर सकता है। Let's suppose की हमें integer type के 5 variables use करने हैं तो सभी define करने पड़े तो code बड़ा हो जयगा और manage करना भी मुश्किल होगा , इस problem को solve करने के लिए Java में एक data structure provide किया : Array
Array
Java
में array same data types
values का एक collection है। means हम एक array variable में same data type की एक ज्यादा values को store कर सकते हैं।
Java
में array को उसके type के साथ brackets का use करके define किया जाता है। type बताता है कि , array किस type की values को store करेगा ।
type[] array_name;
or
type[] array_name = {comma separated elements};
Note - Java में array define करने के लिए हमें array का type define करने की जरूरत होती है।
array example
string[] users = {"Rahul", "Ravi",
"Raju"};
चूंकि array में values एक से ज्यादा हैं , तो हर value की एक position होती है जिसे index कहते हैं। Array में value की indexing 0 से n-1 होती है। यह n कुल elements की सख्यां है। इन्ही index के bases पर array values को insert / update किया जाता है। For Example ऊपर दिए गए example में values की indexing कुछ इस तरह से होगी।
Index |
0 |
1 |
2 |
Values |
Rahul |
Ravi |
Raju |
access array element
तो जैसे कि आपने अभी पढ़ा कि index के bases पर array values को insert / update किया जाता है तो ऊपर define किये गए array elements को कुछ इस तरह से access करेंगे।
users[0] // Output : Rahul
users[1] // Output : Ravi
users[2] // Output : Raju
update array values
इसी तरह index का use करके आप directly new value assign करके array value को update कर सकते हैं , जैसे -
users[0] = "New value";
public class Main {
public static void main(String[] args) {
// define array.
String[] users = {"Rahul",
"Ravi", "Raju"};
// now access array elements
System.out.println(users[0]);
System.out.println(users[1]);
System.out.println(users[2]);
}
}
Output
javac
ArrayExample.java
java
ArrayExample
Rahul
Ravi
Raju
Java में Array को define और access करते हैं।
** ध्यान रहे एक बार array define करने के बाद आप दूसरे type की value insert नहीं कर सकते हैं। ऐसा करने पर error आएगी।
public class Main {
public static void main(String[] args) {
// define array with other types of
elements.
String[] users = {"Rahul", false,
3};
}
}
Array length
हर array
की एक length होती है जो बताती है कि , उस array में कितने elements हैं। Array length पता करने के लिए length property
का use किया जाता है।
public class
ArrayExample {
public static void main(String[] args) {
int[] numbers = {23,23,234,234,234,23,42,4,234,2342};
// print array length
System.out.println("Total numbers
inside numbers[] array : "+ (numbers.length) );
}
}
Output
javac
ArrayExample.java
java
ArrayExample
Total numbers
inside numbers[] array : 10
0 Comments