Basic Operators and Loop
Control in Java.
•Arithmetic Operators?.
•Logical Operators?.
•Relational Operators?.
•Bit wise Operators?.
•Assignment Operators?.
•For, while, do whole loop in java?
•Break and Continue Statements?
4/10/2019 1Jamsher Bhanbhro(F16CS11)
Basic Operators
• Arithmetic Operators: These are same which are
used in mathematical expressions.
• These are (+,-,*,/,%,++,--).
• Relational Operators: These are used to relate
expressions methomatically
• These are (==, >=, <=, >, <, !=) used in java.
• Bitwise Operator: Operations performed on bits
• These are (&’bitwise AND’, II’or’, ^’XOR’,
<<‘LEFTSHIFT’, >>’rightshift’, >>>’zeroshift’ ).
4/10/2019 Jamsher Bhanbhro(F16CS11) 2
Basic Operators.
• Logical Operators: These are used when
there are two condition in a statement.
• These are (&&, II, !).
• Assignment Operators: These are operators
which are used to assign the values.
• These are(=,=+, -=, *=, /=, %=,<<=, >>=)
4/10/2019 Jamsher Bhanbhro(F16CS11) 3
Write a program to demonstrate all
basic operators in java.
4/10/2019 Jamsher Bhanbhro(F16CS11) 4
Program Cont…
• Guess the output of the above program.
• Note: Write a program to use all the types of
each basic operator.
4/10/2019 Jamsher Bhanbhro(F16CS11) 5
Loops
• Loops are used to execute a code of block for several times.
• A loop Consists of a conditional code and a condition.
• The code of block will run till the condition is true.
There are three loops in java.
If true
else
4/10/2019 Jamsher Bhanbhro(F16CS11) 6
Condition Execute
Code
1. While Loop
• Continues or execute the code till the condition
is true.
• Tests condition before executing the code.
• Syntax: while(condition){}
• E.g: int a=4, b=11;
while(b<12){
System.out.println(“output=”+(a+b));
}
4/10/2019 Jamsher Bhanbhro(F16CS11) 7
While Syntax
While(bool exp)
if true
else
4/10/2019 Jamsher Bhanbhro(F16CS11) 8
Condition
Boolean
Expression
Execute
Code
2. For Loop
• Executes the code multiple times
• Approximately same as while but there is some
difference.
• In this loop we will give initialization, condition
and increment values.
• for(init, cond, update){code body}
e.g: int a=11;
for(int i=0; i<=a; i++){
System.out.println(“Hello”);
}
4/10/2019 Jamsher Bhanbhro(F16CS11) 9
For Syntax
While(bool exp)
if true
else incr
4/10/2019 Jamsher Bhanbhro(F16CS11) 10
Condition
Boolean
Expression
Execute
Code
Update
Cond
3. Do While Loop
• Same as while loop but..
• Executes at least once without checking
condition.
• do{}while(cond)
int a=1, b=3;
do{
System.out.println(a*b);
}while(a!=b)
4/10/2019 Jamsher Bhanbhro(F16CS11) 11
3. Do Syntax
While(bool exp)
if true
else
4/10/2019 Jamsher Bhanbhro(F16CS11) 12
Condition
Boolean
Expression
Execute
Code
Why Loops are used write the
advantages.
• Loops are used to reduce the code.
• Loops provide flexibility to the code.
• Loops are highly used in the programming
specially when we are related with arrays.
• Although it is very important to remember the
syntax and the phenomenon of the loops
structures.
4/10/2019 Jamsher Bhanbhro(F16CS11) 13
Break and Continue Statements
• When the break statement is encountered inside a
loop.
• It breaks the statement and gives the current flow
to the next level.
• Switch() in the cases this break is used.
Eg: int a=11;
if(a>10){
break;
}
4/10/2019 Jamsher Bhanbhro(F16CS11) 14
Continue Statement
• Continue statement is used when we want to continue our
block of code.
• Used for jumping purpose.
• In the for loop continue is used to jump to update statement.
• In while continue used to jump to a Boolean expression.
• Syntax: continue;
int a=15;
if(a>11){
continue;
}
4/10/2019 Jamsher Bhanbhro(F16CS11) 15
Tasks
Write a program to draw shape of
• Triangle
• Rectangle
• Pyramid
• Diamond
• Write a program to make a simple calculator of
at least six functions using break and continue
statement.
4/10/2019 Jamsher Bhanbhro(F16CS11) 16

Lect7

  • 1.
    Basic Operators andLoop Control in Java. •Arithmetic Operators?. •Logical Operators?. •Relational Operators?. •Bit wise Operators?. •Assignment Operators?. •For, while, do whole loop in java? •Break and Continue Statements? 4/10/2019 1Jamsher Bhanbhro(F16CS11)
  • 2.
    Basic Operators • ArithmeticOperators: These are same which are used in mathematical expressions. • These are (+,-,*,/,%,++,--). • Relational Operators: These are used to relate expressions methomatically • These are (==, >=, <=, >, <, !=) used in java. • Bitwise Operator: Operations performed on bits • These are (&’bitwise AND’, II’or’, ^’XOR’, <<‘LEFTSHIFT’, >>’rightshift’, >>>’zeroshift’ ). 4/10/2019 Jamsher Bhanbhro(F16CS11) 2
  • 3.
    Basic Operators. • LogicalOperators: These are used when there are two condition in a statement. • These are (&&, II, !). • Assignment Operators: These are operators which are used to assign the values. • These are(=,=+, -=, *=, /=, %=,<<=, >>=) 4/10/2019 Jamsher Bhanbhro(F16CS11) 3
  • 4.
    Write a programto demonstrate all basic operators in java. 4/10/2019 Jamsher Bhanbhro(F16CS11) 4
  • 5.
    Program Cont… • Guessthe output of the above program. • Note: Write a program to use all the types of each basic operator. 4/10/2019 Jamsher Bhanbhro(F16CS11) 5
  • 6.
    Loops • Loops areused to execute a code of block for several times. • A loop Consists of a conditional code and a condition. • The code of block will run till the condition is true. There are three loops in java. If true else 4/10/2019 Jamsher Bhanbhro(F16CS11) 6 Condition Execute Code
  • 7.
    1. While Loop •Continues or execute the code till the condition is true. • Tests condition before executing the code. • Syntax: while(condition){} • E.g: int a=4, b=11; while(b<12){ System.out.println(“output=”+(a+b)); } 4/10/2019 Jamsher Bhanbhro(F16CS11) 7
  • 8.
    While Syntax While(bool exp) iftrue else 4/10/2019 Jamsher Bhanbhro(F16CS11) 8 Condition Boolean Expression Execute Code
  • 9.
    2. For Loop •Executes the code multiple times • Approximately same as while but there is some difference. • In this loop we will give initialization, condition and increment values. • for(init, cond, update){code body} e.g: int a=11; for(int i=0; i<=a; i++){ System.out.println(“Hello”); } 4/10/2019 Jamsher Bhanbhro(F16CS11) 9
  • 10.
    For Syntax While(bool exp) iftrue else incr 4/10/2019 Jamsher Bhanbhro(F16CS11) 10 Condition Boolean Expression Execute Code Update Cond
  • 11.
    3. Do WhileLoop • Same as while loop but.. • Executes at least once without checking condition. • do{}while(cond) int a=1, b=3; do{ System.out.println(a*b); }while(a!=b) 4/10/2019 Jamsher Bhanbhro(F16CS11) 11
  • 12.
    3. Do Syntax While(boolexp) if true else 4/10/2019 Jamsher Bhanbhro(F16CS11) 12 Condition Boolean Expression Execute Code
  • 13.
    Why Loops areused write the advantages. • Loops are used to reduce the code. • Loops provide flexibility to the code. • Loops are highly used in the programming specially when we are related with arrays. • Although it is very important to remember the syntax and the phenomenon of the loops structures. 4/10/2019 Jamsher Bhanbhro(F16CS11) 13
  • 14.
    Break and ContinueStatements • When the break statement is encountered inside a loop. • It breaks the statement and gives the current flow to the next level. • Switch() in the cases this break is used. Eg: int a=11; if(a>10){ break; } 4/10/2019 Jamsher Bhanbhro(F16CS11) 14
  • 15.
    Continue Statement • Continuestatement is used when we want to continue our block of code. • Used for jumping purpose. • In the for loop continue is used to jump to update statement. • In while continue used to jump to a Boolean expression. • Syntax: continue; int a=15; if(a>11){ continue; } 4/10/2019 Jamsher Bhanbhro(F16CS11) 15
  • 16.
    Tasks Write a programto draw shape of • Triangle • Rectangle • Pyramid • Diamond • Write a program to make a simple calculator of at least six functions using break and continue statement. 4/10/2019 Jamsher Bhanbhro(F16CS11) 16