5/17/20151. 2 Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
Arrays, Conditionals & Loops in Java. Arrays in Java Arrays in Java, are a way to store collections of items into a single unit. The array has some number.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
JAVA Control Statement.
UNIT II Decision Making And Branching Decision Making And Looping
L EC. 03: C ONTROL STATEMENTS Fall Java Programming.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
DAT602 Database Application Development Lecture 5 JAVA Review.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Control Structures if else do while continue break switch case return for.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Compound Statements If you want to do more than one statement if an IF- else case, you can form a block of statements, or compound statement, by enclosing.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 4. Controlling Execution SPARCS JAVA Study.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Statements and Control Flow The programs we have seen so far do exactly the same list of instructions every time What if we want to do different things.
Application development with Java Lecture 6 Rina Zviel-Girshin.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 3 Selection Statements
Branching statements.
Core Java Statements in Java.
Lecture 4b Repeating With Loops
Control Structures.
Ch 7: JavaScript Control Statements I.
Expressions and Control Flow in JavaScript
JavaScript: Control Statements.
Chapter 5: Control Structures II
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
LRobot Game.
An Introduction to Java – Part I, language basics
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 2 Lecture-2
while while (condition) { statements }
PROGRAM FLOWCHART Selection Statements.
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Presentation transcript:

5/17/20151

2 Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while - for loops - switch statements

5/17/20153 if - else construct... The if-else construct executes some operation based on successful completion of a condition, that returns only Boolean values. If the condition is not satisfied, then the set of instructions under ‘else’ are executed. Begin Result When condition is true False True Result When condition is false if(condition) { //Statements if condition is true } else { //Statements if condition is false } if(condition) { //Statements if condition is true } else { //Statements if condition is false } If (condition)

5/17/20154 if - else construct... The if-else construct executes some operation based on successful completion of a condition, that returns only Boolean values. If the condition is not satisfied, then the set of instructions under ‘else’ are executed. Begin If (condition) Begin if(condition) { //Statements if condition is true } else { //Statements if condition is false } if(condition) { //Statements if condition is true } else { //Statements if condition is false }

02/10/105 Example: if-else... class Constructs { public static void main(String arg[]) { int a=10; if(a<10) { System.out.println(“a is greater than 10”); } else { System.out.println(“a is less than 10”); } } class Constructs { public static void main(String arg[]) { int a=10; if(a<10) { System.out.println(“a is greater than 10”); } else { System.out.println(“a is less than 10”); } }

5/17/20156 Ternary (conditional) operator... The ternary operator is considered to be an alternative to the if- else construct. test ? Pass : Fail ; test – the condition to be tested Pass - value given here is returned when the test is satisfied Fail – value given here is returned when the test is failed.

02/10/107 Example: ternary operator... class Constructs { public static void main(String arg[]) { int avg=75; char result; result=avg>50 ? ‘A’ : ‘F’ System.out.println(result); } class Constructs { public static void main(String arg[]) { int avg=75; char result; result=avg>50 ? ‘A’ : ‘F’ System.out.println(result); }

5/17/20158 Nested if - else construct....dfssdf dsdfsdf Begin If (condition) Begin if(condition) { //Statements if condition is true } else { //Statements if condition is false } if(condition) { //Statements if condition is true } else { //Statements if condition is false }

5/17/20159 Example: Nested if-else... Modify your program, so that it is able to find the grade of each student based on the average. You must use following criteria to obtain the grades. Example 2 (cont): Store information of students Range of AverageGrade 85 to 100A 75 to 84B 65 to 74C 55 to 64S 45 to 54w <45F

02/10/1010 While loop... The while loop executes some operation repeatedly, until the condition returns false. while(condition) { //body of the loop } while(condition) { //body of the loop } When the condition fails, then the loop terminates.

02/10/1011 Example: While loop... class Constructs { public static void main(String arg[]) { int i=1; while(i<=10) { System.out.println(i); i++; } class Constructs { public static void main(String arg[]) { int i=1; while(i<=10) { System.out.println(i); i++; } Write a JAVA program to display consecutive numbers from 1 to 10, using while loops.

02/10/1012 Do-While loop... The do-while loop is similar to the while loop except the condition to be evaluated is given at the end. do { //body of the loop } while(condition); do { //body of the loop } while(condition); The loop is executed at least once even when the condition is false.

02/10/1013 Example: Do-While loop... class Constructs { public static void main(String arg[]) { int i=1; do { System.out.println(i); i++; } while(i<=10) ; } class Constructs { public static void main(String arg[]) { int i=1; do { System.out.println(i); i++; } while(i<=10) ; } Write a JAVA program to display consecutive numbers from 1 to 10, using do-while loops.

02/10/1014 For loop... A for loop repeats a set of statements, until a condition is satisfied. There are three major parts in a for loop namely; initialization, test (condition) and the expression. for(initialization; test; expression) { //Body of the for loop } for(initialization; test; expression) { //Body of the for loop } Initialization- A variable is initialized to a value. Test – This is the condition of the loop and it returns a Boolean value. Expression – The expression is evaluated every time that the loop is executed.

02/10/1015 Example: For loop... class Constructs { public static void main(String arg[]) { for(int i=0;i<10;i++) { System.out.println(i+1); } class Constructs { public static void main(String arg[]) { for(int i=0;i<10;i++) { System.out.println(i+1); } Write a JAVA program to display consecutive numbers from 1 to 10, using for loops.

02/10/1016 Switch statement... The switch statement dispatches different parts of the code based on the value of a single variable or expression. The value of expression is compared with each of the literal values in the case statements. If they match, the corresponding instructions are executed. Otherwise an optional default statement is executed. switch(test) { case value1: //Statement1 break; ……………………………………………………………………………………………………. default: //Statement } switch(test) { case value1: //Statement1 break; ……………………………………………………………………………………………………. default: //Statement } Test is a variable or an expression that evaluates to byte, char or int. It cannot be float, double, long, string or any other object

02/10/1017 Example: Switch Statement... int num=2; switch(num) { case 1: System.out.println("January"); break; case 2: System.out.println(“February"); break; …………………………………………………………………………………………………………………………………………………………………. default: System.out.println("Invalid Number"); } int num=2; switch(num) { case 1: System.out.println("January"); break; case 2: System.out.println(“February"); break; …………………………………………………………………………………………………………………………………………………………………. default: System.out.println("Invalid Number"); } Write a JAVA program to display the name of the month when the number is given.

02/10/1018 Break & Continue... The break keyword halts the execution of current loop and forces control out of the loop. The continue keyword starts the next iteration of the loop rather than halting the execution of entire loop. for(int i=0;i<10;i++) { if(i==5) continue; System.out.println(i); } for(int i=0;i<10;i++) { if(i==5) continue; System.out.println(i); } for(int i=0;i<10;i++) { if(i==5) break; System.out.println(i); } for(int i=0;i<10;i++) { if(i==5) break; System.out.println(i); }

02/10/1019 Exercises... Write JAVA programs to display following outputs. You must use programming constructs.