Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
JAVA Control Statement.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
DAT602 Database Application Development Lecture 5 JAVA Review.
DiagrammaticRepresentation Iteration Construct False True Condition Exit from Statement (s) loop Sequence construct Selection construct Statement 1 Statement.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Expressions An expression is a series of variables, operators, and method calls (constructed according to the syntax of the language) that evaluates to.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Session 2 Session 2 Java Language Fundamentals 9:17 AM.
Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Control statements Mostafa Abdallah
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Programming Principles Operators and Expressions.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Information and Computer Sciences University of Hawaii, Manoa
Java Fundamentals 4.
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Selections Java.
Sachin Malhotra Saurabh Choudhary
Control Structures.
Expressions and Control Flow in JavaScript
OPERATORS (2) CSC 111.
Starting JavaProgramming
Arithmetic operations, decisions and looping
Java Tokens & Data types
elementary programming
Control Structure Chapter 3.
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
Control Structure.
Presentation transcript:

Session 2 Operators, Decisions and Loops

Objectives Operators Casting data Decision marking structures Loops break, continue, return

Operators Arithmetic Operators +, -, *, /, %, ++, -- Bitwise Operators &, |, ^, ~ Shift operators >>, << Relational Operators &&, ||, ! Logical Operators >=,, <, ==, != Conditional Operators exp1 ? exp2 : exp3; Assignment operators =, +=, -=, *=, /=, %= instanceof

Operator Precedence

Type Casting In type casting, a data type is converted into another data type. Example float c = f; int b = (int)c + 10;

Automatic type and Casting There are two type of data conversion Implicit or coercion : automatically done by compiler Explicit or casting Implicit casting The two types are compatible The destination type is larger than the source type Explicit casting Syntax: (datatype) exp; Example: float c; int d = (int) c;

Type Promotion Rules All byte and short values are promoted to int type. If one operand is long, the whole expression is promoted to long. If one operand is float then the whole expression is promoted to float. If one operand is double then the whole expression is promoted to double.

Decision making Java supports decision making (control statements) are if statement if…else statement switch…case statement

if statement Evaluate the statements based on a given condition is true or false Syntax: if(condition){ actions; }

if-else statement The if-else statement tests the result of a condition, and performs appropriate actions based on the result. It can be used to route program execution through two different paths. The format of an if-else statement is very simple and is given below: if (condition){ action1; }else{ action2; }

Example class CheckNum { public static void main(String [] args) { int num = 10; if (num % 2 == 0) System.out.println(num + " is an even number"); else System.out.println(num + " is an odd number"); }

Multiple if statement Also call if-else-if statement Syntax: if(condition){ actions; }else if(condition){ actions; }else{ actions; }

switch – case statement The switch – case statement can be used in place of if-else-if statement. It is used in situations where the expression being evaluated results in multiple values. The use of the switch-case statement leads to simpler code, and better performance. The expression must be evaluated to a byte, short, char or int

Example class SwitchDemo{ public static void main(String[] args){ int day = 4; String str; switch (day){ case 0: str = "Sunday"; break; case 1: str = "Monday"; break; case 2: str = "Tuesday"; break; case 3: str = "Wednesday"; break; case 4: str = "Thursday"; break; case 5: str = "Friday"; break; case 6: str = "Saturday"; break; default: str = "Invalid day"; } System.out.println(str); }

Loop

while Loop while loops are used for situations when a loop has to be executed as long as certain condition is True. The number of times a loop is to be executed is not pre-determined, but depends on the condition. The syntax is: while (condition){ actions;. }

Example class FactDemo { public static void main(String [] args) { int num = 5, fact = 1; while (num >= 1) { fact *= num; num--; } System.out.println("The factorial of 5 is : " + fact); }

do – while Loop The do-while loop executes certain statements till the specified condition is True. These loops are similar to the while loops, except that a do-while loop executes at least once, even if the specified condition is False. The syntax is: do { actions;. } while (condition);

for Loop All loops have some common features: a counter variable that is initialized before the loop begins, a condition that tests the counter variable and a statement that modifies the value of the counter variable. The for loop provides a compact format for incorporating these features. Syntax: for (init statements; condition; inc/dec statements) { action statements;. }

Example class ForDemo { public static void main(String [] args) { int count = 1, sum = 0; for (count = 1; count <= 10; count += 2) { sum += count; } System.out.println("The sum of first 5 odd numbers is : " + sum); } The sum of first 5 odd numbers is : 25

Jump Statements Using jump statement To skip the loop based on the condition Transfer control to another part of program Jump statements are: break continue return Learn Java By Example 21/38

Example class BrDemoAppl { public static void main(String [] args){ for (int count = 1; count <= 100; count++){ if (count == 10) break; System.out.println("The value of num is : " + count); } System.out.println("The loop is over"); } The value of num is : 1 The value of num is : 2 The value of num is : 3 The value of num is : 4 The value of num is : 5 The value of num is : 6 The value of num is : 7 The value of num is : 8 The value of num is : 9 The loop is over