We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
Left to Right
Binary
+ ++ Inew sizeof- -- Unary Right to Left
~& delete
Binary Teft to Right
Binary Left to Right
Binary ‘Left to Right
Binary Left to Right
Binary Left to Right
Binary Left to Right
Binary Left to Right
Binary Left to Right
Binary Left to Right
ae Binary Left to Right
Ul Binary Teft to Right
x Ternary Left to Right
SFSHS ASRS <= Binary Righttoleft
Binary eft to Right
Expressions
An expression may consist of orie or more operands and
Zero or more operators to produce a value. Expressions are
of six types as follows:
1. Constant Expression It consists of only constant
values.
e.g. 22, 275, 46/36
2, Integral Expression Those expressions which
produce integer results after implementing all
automatic and explicit type conversions.
eng. X*Y=11, (X + ¥) - Z,
45 +X + int (2.5)
3. Float Expression Those expressions which produce
floating point results after all conversions.
eg. (X4¥)/37. 37#F oat (16)/11
4, Pointer Expression It produces a memory address
values.
eg. &X
5, Boolean Expression These are constructed with the
help of logical and relational operators.
eg.a =y etc.
6. Compound Expression These are made up of two or
more expressions with the help of operators.
eg. (a + b)|I(c + d)
C++ Comments
‘A comment is a text that the compiler ignores, but that is
useful for progeammers. Comments are normally used to
Scanned with CamScannerannotate code for future reference. The compiler treats
them as white space.
A C++ comment is written in one of the following ways:
(i) The /* (slash, asterisk) characters, followed by any
sequence of characters (including new lines), followed
by the */ (asterisk, slash) characters is called multi
line comment.
(ii) The // (two slashes) characters, followed by any
sequence of characters is called a single line comment,
A new line not immediately preceded by a backslash
terminates this form of comment.
Scanned with CamScanner—
Control Statements
Control statements are used in programming languages to
cause the flow of execution in advance. These are the
branch based statements where branches are depend on the
state of a program. In Java, control statements can be
divided under the following three categories:
1. Selection Statements
Selection statements are used in a program to choose
different paths of execution based upon the outcome of an
expression or the state of a variable.
if Statement
The if statement is a powerful decision making statement
and is used to control the flow of execution of statement.
Syntax
if (condition)
statement ;
Here, ee
* condition is a boolean expression.
‘« if condition is true, then the statement will execute.
* if condition is false, then the statement will be by passed.
if-else Statement ;
The if-else statement can be used to route program
execution through two different paths.
Syntax
if(condition)
statementl; //if condition is true
else
statement2; //if condition is false
Here,
* condition is a boolean expression.
* if condition is true, then statement! or if block will
execute.
* if condition is false, then statement? or else block will
execute. :
Scanned with CamScannerNested if Statement
‘The if statement can be nested in Java, which means one if
or if else statement inside another if or if else statement cai
be used.
Syntax
if (condition1)
if (condition2)
{
statement1:
}
else
{
statement2:
}
}
else
{
if(condition3)
{
statement3;
}
else
{
statement4;
}
}
Here,
* if condition! is true, then condition? will execute.
* if condition2 is true, then statement! will execute,
* if condition2 is false, then statement? will execute.
+ if conditiont is false, then in else block condition3 will be
evaluated.
+ if condition3 is true, then statement3 will execute,
* if conditions is false, then statementé will execute,
if else if Ladder
When a seties of decisions are involved, we have to use
more than one if else statement,
An if statement can be followed by an optional else if.
else statement, which is very useful to test various
conditions.
Syntax
1f(condition1)
statement]
else if(condition2)
statement2;
else if(condition3)
statement3
else
statementn +1;
Scanned with CamScannerHere,
« The if statements are executed from the top.
af er ee
«As one of the conditions controlling the if, is tru
i ted.
statement will be executed
+ If none of the conditions is'true, the final else statement
iditioy
i |. The final else acts as a default condition,
neato beaome false, then i wil be executed,
switch Statement ;
In Java, the witch statements another selection statement
that defines different paths of execution for a program. It
can be used in place of chain of if-else statements.
the
Syntax
switch (expression)
case valuel: //statement sequence
break:
case value2: //statement sequence
break;
case valueW: //statement sequence
break;
default: //default statement sequence
}
Here,
* The expression must be of type byte, short, int of char.
+ Each case value must be a unique literal (that is, it must
be a constant, not a variable).
*+ The break statement is used inside the switch to
terminate a statement sequence.
* If none of the constants matches with the value of the
expression, then the default statement will be executed.
Conditional, Operator (?.)
Conditional operator is a substitute of normal if else
statement, ie. the shorthand form of if else statement is the
conditional operator (2). Its also known as ternary
operator.
eg. if (condition)
expression1;
else
expression2;
Tt can be written as, _—
candi ion! expression! + expression?
2. Iteration Statements
‘The most basic method of looping in Java is iteration, ‘The
repetition ofa block of statements either a fixed number of
times or until a specific conditi is iteratic
ae dition occurs is called iteration
A looping process includes the follow;
Ini ion of expression,
* Evaluation of the loop condition,
ing four steps:
Scanned with CamScanner« Execution of the loop when the specified oop condition
ise. :
« Incrementing/Decrementing the expression,
Java provides three kinds of loops as follows:
for Loop
‘when fixed number of iterations are to be performed, for
Joop is used.
syntax
for(initialisation; condition: increment/decrenent)
{body of for loop
}
Here,
for loop grouped the following three common parts
together into one statement:
(initialisation The control variables is initialise firs,
using assignment statement such as i= Land count =0.
The variables i and count are known as loop-control
variables.
Initialisation expression is executed only once.
i) Condition The value of the control variable is tested
using test condition. If the condition is true, then the
body of the loop is executed, otherwise the for loop is
terminated and the execution continues with the
statement that immediately follows the for loop.
(ii) Increment or Decrement When the body of the
loop is executed, the control is transferred back to the
for statement after evaluating the last statement in the
loop. Now, the control variable is
incremented/decremented using an assignment
‘statement and the new value of the control variable is
again tested to see whether it satisfies the loop
condition or not. If the condition is satisfied, the body
of the loop is again executed. This process continues
till the value of the control variable fails to satisfy the
test condition.
While Loop
The while loop is used to repeatedly execute a block of
Statements based on a condition. The condition is evaluated
fore the iteration starts.
Syntax
whi le(condition)
//while loop -body
im.)
* The condition is evaluated and if the condition is true,
. then the body of the while loop is executed.
When the condition becomes false, the program control
S65 to the statement written immediately
While loop body.
Scanned with CamScannerdo-while Loop
The do-while loop always executes its body atleast once,
becatise its conditional expression is at the bottom of the
loop.
Syntax
do
{
‘Toody of do-while loop
}while(condition):
Here,
* Each iteration of the do-while loop first executes the
body of the loop and then evaluates the conditional
expression,
+ Ifthis expression is true, the loop will repeat.
* Otherwise, the loop will terminate.
3.Jump Statements
Java supports three jump statements, ie. break, continue
and return. These statements transfer control to another
part of the program,
break Statement
This statement is generally used to break the loop of while
do-while, for or switch statement.
It has three uses as follows:
+ It terminates a statement sequence in a switch statement.
* It can be used to exit a loop.
* It can be used as a ‘civilised’ form of goto statement.
By using break, you can force immediate termination of a
loop, by passing the conditional expression and any
remaining code in the body of the loop. The break
statement should be used to cancel a loop only when some
sort of special situation occurs.
continue Statement
In Java, continue statement is used to skip the part of loop.
Unlike break statement, it does not terminate the loop,
instead it skips the remaining part of the loop and controls
go back to check the condition again.
Continue statement performs below mentioned actions:
* In while and do-while loops, a continue statement causes
control to be transferred directly to the conditional
expression that controls the loop.
+ With the break statement, continue may specify a label to
describe which enclosing loop to continue.
+ Itis a jumping statement and is used as keyword.
Tt skips the loop and re-executes loop with new
condition.
In for loop, continue statement takes control to
increment/decrement step, i.e. after continue i++/i-- will
be get executed,
Scanned with CamScanner