











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of various Java programming concepts, including conditional operators, operator precedence, control statements, and arrays. It explains the use of the ternary operator, operator precedence rules, and different types of control statements such as selection statements (if, nested if, if-else-if ladder, and switch statement), iteration statements (while, do-while, and for), and jump statements (break, continue, and return). Additionally, it covers array declaration, creation, indexing, and initialization.
What you will learn
Typology: Slides
1 / 19
This page cannot be seen from the preview
Don't miss anything!
(^) Conditional Operator (^) Operator Precedence (^) Control Statements (^) Arrays
(^) An arithmetic expression without any parentheses will be calculated from left to right using the rules of precedence of operators. There are two priority levels of arithmetic operators are as follows: (a) High priority (* / %) (b) (b) Low priority (+ -) The evaluation process includes two left to right passes through the expression. During the first pass, the high priority operators are applied as they are encountered For example: Z=A-B/3+C3-1 When A=10, B=13, C= First pass: Z=10-(13/3) + (33)-1 Z=10-4+3- Second pass: Z=6+3- Z=
a. if statement: ‘if’ is a selection statement that is used to choose two choices in a program. Syntax: if(condition) statement1; else statement2; ‘condition’ is any expression that returns a Boolean value (i.e., true/false). Statement is a single or multiple statements. If the condition is true then the control goes to the statement1 and it is executed. Otherwise, the statement2 is
b. Nested if statement: It means an if statement under another if statement. Syntax : if(condition) { if(condition) statement; } c. if-else-if ladder: if-else-if statement is a sequence of if-else statements. Syntax: if(condition) statement1; else if(condition) statement2; else if(condition) statement3;
.. else statement2; In if-else-if ladder if a condition is not met then the control flows from top to bottom until the last if statement.
These statements allows the part of the program to repeat one or more times until some condition becomes true. There are 3 iteration statements: while, do-while and for a. while statement: ‘while’ is an iteration statement, that repeats a statement or block of statements until some condition is true. Syntax: while(condition) { // body of the loop } where the condition may be any boolean expression. The body of the loop will be executed as long as the condition is true. Once the condition becomes false, the control passes to the statement immediately after the while loop.
b. do-while statement: ‘do-while’ statement is very much similar to while statement with little difference. In while statement, if the condition is initially false then the body of loop will not be executed at all. Where as in do-while statement, body of the loop will be executed at least once since the condition of do-while is at the bottom of the loop. Syntax: do { // body of the loop } while(condition); Each iteration of do-while executes the body of loop first and evaluates the condition later. If condition is true, the loop repeats. Otherwise, the loop terminates.
Jump statements allows the control to jump to a particular position. There are 3 types of jump statements. a. break statement: in java, there are 3 uses of break statement. i. It helps to terminate a statement sequence in switch statement. ii. It can be used to exit or terminate the loop iii. It can be used as another form of goto
b. continue statement: The continue statement starts the next iteration of the immediately enclosing iteration statements(while, do-while or for). When the control goes to continue statement, then it skips the remaining statements and starts the next iteration of enclosing structure.
An array is a group of liked-typed variables referred to by a common name, with individual variables accessed by their index. Arrays are:
After declaration, no array actually exists. In order to create an array, we use the new operator: type array-variable[]; array-variable = new type[size]; This creates a new array to hold size elements of type type, which reference will be kept in the variable array-variable Array Indexing: Later we can refer to the elements of this array through their indexes: array-variable[index] The array index always starts with zero! The Java run-time system makes sure that all array indexes are in the correct range, otherwise raises a run time error.