Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Similar documents
Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Operators in java Operator operands.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

JAVA OPERATORS GENERAL

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Introduction. C provides two styles of flow control:

Computer Programming I - Unit 5 Lecture page 1 of 14

CS111: PROGRAMMING LANGUAGE II

Internet & World Wide Web How to Program, 5/e by Pearson Education, Inc. All Rights Reserved.

Introduction to the Java Basics: Control Flow Statements

AP CS Unit 3: Control Structures Notes

CMPT 125: Lecture 4 Conditionals and Loops

Iteration statements - Loops

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Loops. CSE 114, Computer Science 1 Stony Brook University

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 3. Selections

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Control Statements. Musa M. Ameen Computer Engineering Dept.

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Prof. Navrati Saxena TA: Rochak Sachan

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Lecture 14. 'for' loops and Arrays

Control Structures in Java if-else and switch

CONDITIONAL EXECUTION

Building Java Programs Chapter 2

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

CS111: PROGRAMMING LANGUAGE II

CT 229 Java Syntax Continued

Recap: Assignment as an Operator CS 112 Introduction to Programming

Object-Oriented Programming

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Course Outline. Introduction to java

CS 106 Introduction to Computer Science I

CS 112 Introduction to Programming

Operators. Java operators are classified into three categories:

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Java I/O and Control Structures

Programming for Engineers Iteration

Repetition, Looping. While Loop

Programming with Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

Conditionals, Loops, and Style

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Condi(onals and Loops

Java Review. Java Program Structure // comments about the class public class MyProgram { Variables

AP Programming - Chapter 6 Lecture

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

CompSci 125 Lecture 11

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java I/O and Control Structures Algorithms in everyday life

Conditionals and Loops

More Programming Constructs -- Introduction

PROGRAMMING FUNDAMENTALS

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Topics. Chapter 5. Equality Operators

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Selenium Class 9 - Java Operators

LECTURE 5 Control Structures Part 2

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Looping. Arizona State University 1

Example: Monte Carlo Simulation 1

Control Structures in Java if-else and switch

Algorithms and Conditionals

Output :: /* display results */ System.out.print( Call Duration: );

Conditionals, Loops, and Style. Control flow thus far. if statement. Control flow. Common branching statement Evaluate a boolean expression

Control Structures (Deitel chapter 4,5)

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

CT 229 Java Syntax Continued

CSCI 136 Data Structures & Advanced Programming. Fall 2018 Instructors Bill Lenhart & Bill Jannen

Flow Control. CSC215 Lecture

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

CS313D: ADVANCED PROGRAMMING LANGUAGE

ECE 122. Engineering Problem Solving with Java

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

Object Oriented Programming Lecture (2.1) Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University

Building Java Programs

Chapter 4: Control structures. Repetition

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

The Java language has a wide variety of modifiers, including the following:

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Java Coding 3. Over & over again!

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

Top-Down Program Development

true false Imperative Programming III, sections , 3.0, 3.9 Introductory Programming Control flow of programs While loops: generally Loops

Transcription:

Control Statements

Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure

Sequence structure The computer executes java statements one after the other in the order in which they are written. Add grade to total Add 1 to counter Total = total +grade; Counter = counter + 1;

Selection statements If statement (single-selection statement) If else statement (double-selection statement) Switch statement (multiple-selection statement)

If & If else statements If single-selection statement If else double-selection statement It select or ignores a single action. Ex: if(grade >= 60) System.out.println( passed ); It select between two different actions Ex: if(grade >= 60) System.out.println( passed ) ; else System.out.println( failed ) ;

Conditional operator(?:) Conditional operator(?:) can be used in place of an if else statement. This operator is called ternary operator this mean it takes three operands. System.out.println( grade>=60? passed : failed ); Boolean expression Value if the boolean expression is true Value if the boolean expression is false

Example using conditional operator public static void main (String args[]) { int n1=3,n2=4; System.out.println( n2> n1? 5+1 : 3+2); System.out.println((n2 <n1? 2 : 3) * 4); } output: 6 12

A program can test multiple cases by placing if else statements inside other if else statements to create nested if else statements Ex: if(grade>=90) System.out.println( A ); else if(grade>=80) System.out.println( B ); else if (grade >=70) System.out.println( C ); else if (grade >= 60) System.out.println( D ); else System.out.println( F );

dangling-else problem the java compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces({ }) if (x > 5) if( y > 5) System.out.println ( x and y are > 5 ); else System.out.println ( x is <= 5 ); if (x > 5) { if( y > 5) System.out.println ( x and y are > 5 ); } else System.out.println ( x is <= 5 );

Switch statements perform different action based on the possible values of an integer variable or expression. switch (num) { Case1: System.out.println ( 1 ); Case 2: System.out.println ( 2 ); Case 3: System.out.println ( 3 ); Default: System.out.println ( nothing ); } Num =1 num=3 1 3 2 nothing 3 nothing

Example using switch statement { } switch (num) Default: System.out.println ( nothing ); Case1: System.out.println ( 1 ); break; Case 2: System.out.println ( 2 ); break; Num =1 num=3 1 nothing 1 switch (num) { Case 1: Case 2: Case 3: System.out.println ( ok ); } Num =1 ok num=3 ok

Conditional AND Operator (&&) Conditional OR Operator ( ) Logical Negation Operator (!) Logical operators Boolean Logical AND (&) Boolean Logical OR ( ) Boolean Logical Exclusive OR (^)

Conditional AND operator(&&) Expression 1 Expression 2 Expression 1 && expression 2 false false false false true false true false false true true true

Conditional OR Operator( ) Expression 1 Expression 2 Expression 1 && expression 2 false false false false true true true false true true true true

Example using AND/OR Operator int a =5, b=7; if (a==10 && b>=3) a=a + b; else ++a; // a=6 int a=5, b=7; if (a==5 b<3) a=a + b; // a =12 Note: The parts of an expression containing &&/ operator are evaluated only until it is known whether the condition is true or false. This feature is called short-circuit evaluation

Boolean Logical AND(&) and OR( ) Operator This operators work identically to the (&&/ )operators, with one exception: the boolean logical operators always evaluate both of their operands (they don t perform short-circuit evaluation). This is useful if the right operand of the boolean logical (&/ ) has a required a modification of a variable s value. For example, the expression (birthday == true) (++ age >=65) Guarantees that the condition (++ age >= 65) will be evaluated. Thus the variable age is increment in the preceding expression, regardless of whether the overall expression is true or false.

Example using &/ operator int x=4,y=9; if (x>= 5 && ++y == 10) System.out.println ( ok ); System.out.println (y); int x=4,y=9; if (x>= 5 & ++y == 10) System.out.println ( ok ); System.out.println (y); 9 10 int i = 1, j = 5; if (i>= 1 j++ > 5) System.out.println (j); int i = 1, j = 5; if (i>= 1 j++ > 5) System.out.println (j); 5 6

Boolean Logical Exclusive OR (^) Expression 1 Expression 2 Expression 1 && expression 2 false false false false true true true false true true true false This operator is also guaranteed to evaluate both of its operand.

Logical Negation Operator(!) expression! expression false true true false if(!( grade == 60)) is equivalent to If ( grade!= 60 ) ex: if(!( grade == 60)) System.out.println(grade);

Repetition statements It enables program to perform statements repeatedly as long as a condition remains true There are three repetition statements (looping statements) While statement Do While statement For statement

While/Do While statements While statements Do While statements int product = 3; While (product <= 100) Product = 3*product; int counter = 1; Do { System.out.println(counter); ++counter; } while (counter <= 10);

Do while repetition statements is similar to the while statements whereas: In the while statement, the program tests the condition at the beginning of the loop before executing the loops body. If the condition is false, the body never executes, and so the action in the body will be performed zero or more times. Do While statement tests the condition after executing the loop s body; therefore, the body always executes at least once. When a do while statement terminates, execution continues with the next statement in sequence.

For statements It specifies the counter controlled repetition details in a single line of code. Initialization Loop condition Increment for ( int i = 1 ; i <= 10 ; i++) System.out.println(i); Statement

In most cases, for statement can be represented with an equivalent while statement Initialization; While (loop condition) { Statement; Increment; } Example: int i = 1; While (i<= 10) { System.out.println(i); i++; } If the initialization expression in the for header declare the variable, the variable can be used only in that for statements ( it will not exist outside the for statement), and this is called the variable scope

Examples using the for statements Print the variable over the following sequence of values: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0. For (int i = 99; i >= 0; i -= 11) System.out.print( i +, ); summing the even integers from 2 to 20 public static void main(string args[]) { int total = 0; For ( int num =2;num<=20;num+=2) Total+=num; System.out.println(total); }

Increment & decrement operators operator called Sample expression explanation ++ Prefix increment ++a Increment (a) by 1, then use the new value of (a) in the expression in which (a) resides. ++ Postfix increment a++ Use the current value of (a) in the expression in which (a) resides, then increment (a) by 1. -- Prefix decrement --b decrement (b) by 1, then use the new value of (b) in the expression in which (b) resides. -- Postfix decreent b-- Use the current value of (b) in the expression in which (b) resides, then decrement (a) by 1.

Public static void main(string args[]) { Int c=5; System.out.println(c++); // print 5 System.out.println(++c); // print 7 System.out.println(c--); // print 7 System.out.println(--c); // print 5 }

Break statement The break statement when executed in a while, for, do while or switch, causes immediate exit from that statement. Public static void main(string arg[]) { int count; For (count=1;count<=10;count++) { if (count==5) Break; System.out.print (count); } } 1234

Continue statement The continue statement, when executed in a while, for, do while, skip the remaining statements in the loop body and proceeds with the next iteration of the loop. Public static void main(string arg[]) { int count; For (count=1;count<=10;count++) { if (count==5) continue; System.out.print (count); } } 1234678910