Le L c e t c ur u e e 3 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Control Statements

Similar documents
Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Introduction. C provides two styles of flow control:

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

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

PROGRAMMING FUNDAMENTALS

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

ECE 122. Engineering Problem Solving with Java

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

Control Flow Statements

Expression statements are formed by adding a semicolon to the end of certain types of expressions.

Flow Control. CSC215 Lecture

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

COMPUTER PROGRAMMING LOOPS

Introduction to Programming Using Java (98-388)

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

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

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

DECISION CONTROL AND LOOPING STATEMENTS

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming with Java

Review for Test 1 (Chapter 1-5)

Chapter 7. Additional Control Structures

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

Introduction to the Java Basics: Control Flow Statements

Principles of Computer Science

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

More Control Structures

Module 4: Decision-making and forming loops

CS1004: Intro to CS in Java, Spring 2005

CompSci 125 Lecture 11

Java Programming: from the Beginning. Chapter 8 More Control Structures. CSc 2310: Principle of Programming g( (Java) Spring 2013

Prepared by: Shraddha Modi

REPETITION CONTROL STRUCTURE LOGO

Language Reference Manual

Object-Oriented Programming

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

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

G. Tardiani RoboCup Rescue. EV3 Workshop Part 1 Introduction to RobotC

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

Computational Expression

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Unit 3 Decision making, Looping and Arrays

Values in 2 s Complement

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Loops. CSE 114, Computer Science 1 Stony Brook University

Conditional Execution

Java Control Statements

Lecture 7: General Loops (Chapter 7)

Following is the general form of a typical decision making structure found in most of the programming languages:

Computer Programming I - Unit 5 Lecture page 1 of 14

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

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

Decision Making and Loops

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

204111: Computer and Programming

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

13 th Windsor Regional Secondary School Computer Programming Competition

C++ Programming: From Problem Analysis to Program Design, Third Edition

CHAPTER : 9 FLOW OF CONTROL

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

CSE 113 A. Announcements - Lab

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Introduction to C/C++ Lecture 3 - Program Flow Control

CSC Java Programming, Fall Java Data Types and Control Constructs

Control Structures in Java if-else and switch

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

The Basic Parts of Java

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

The Arithmetic Operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

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

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

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

SOFTWARE DEVELOPMENT 1. Control Structures 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

LECTURE 5 Control Structures Part 2

Advanced Computer Programming

SELECTION. (Chapter 2)

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

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

STUDENT LESSON A12 Iterations

CS110D: PROGRAMMING LANGUAGE I

Unit 2: Java in the small

CS111: PROGRAMMING LANGUAGE II

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Java+- Language Reference Manual

Course Outline. Introduction to java

Introduction to Java & Fundamental Data Types

3chapter C ONTROL S TATEMENTS. Objectives

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

(Refer Slide Time: 00:26)

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Transcription:

Course Name: Advanced Java

Lecture 3 Topics to be covered Control Statements

Introduction The control statement are used to control the flow of execution of the program. This execution order depends on the supplied data values and the conditional logic. Java contains the following types of control statements: Selection Statements Repetition Statements Branching Statements

Selection Statement Selection statements can be of three types: If statement If-else statement Switch statement

if--statement if This is a control statement to execute a single statement or a block of code, when the given condition is true and if it is false then it skips if block and rest code of program is executed. Syntax: if(conditional_expression) { <statements>;...;...; }

Example of ifif-statement Example: If n%2 evaluates to 0 then the "if" block is executed. Here it evaluates to 0 so if block is executed. Hence "This is even number" is printed on the screen. int n = 10; if(n%2 = = 0) { System.out.println("This is even number"); }

ifif--else Statement The "if-else" statement is an extension of if statement that provides another option when 'if' statement evaluates to "false i.e. else block is executed if "if" statement is false. Syntax: if(conditional_expression) { <statements>;...;...; } else { <statements>;...;....; }

Example of ifif-else statement Example: If n%2 doesn't evaluate to 0 then else block is executed. Here n%2 evaluates to 1 that is not equal to 0 so else block is executed. So "This is not even number" is printed on the screen. int n = 11; if(n%2 = = 0) { System.out.println("This is even number"); } Else { System.out.println("This is not even number"); }

Switch Statement This is an easier implementation to the if-else statements. The keyword "switch" is followed by an expression that should evaluates to byte, short, char or int primitive data types,only. In a switch block there can be one or more labeled cases. The expression that creates labels for the case must be unique. The switch expression is matched with each case label. Only the matched case is executed,if no case matches then the default statement (if present) is executed.

Syntax for Switch Statement Syntax: switch(control_expression) { case expression 1: <statement>; case expression 2: <statement>;...... case expression n: <statement>; default: <statement>; }//end switch

Example of Switch Statement Example: Here expression "day" in switch statement evaluates to 5 which matches with a case labeled "5" so code in case 5 is executed that results to output "Friday" on the screen. int day = 5; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thrusday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Invalid entry"); break; }

Repetition Statements In java, there are three types of Repetition statements: While loop statements Do-while loop statements For loop statements

While loop statements This is a looping or repeating statement. It executes a block of code or statements till the given condition is true. The expression must be evaluated to a boolean value. It continues testing the condition and executes the block of code. When the expression results to false control comes out of loop. Syntax: while(expression) { <statement>;...;...; }

Example of while loop Here expression i<=10 is the condition which is checked before entering into the loop statements. When i is greater than value 10 control comes out of loop and next statement is executed. So here i contains value "1" which is less than number "10" so control goes inside of the loop and prints current value of i and increments value of i. Now again control comes back to the loop and condition is checked. This procedure continues until I becomes greater than value "10". So this loop prints values 1 to 10 on the screen. int i = 1; //print 1 to 10 while (i <= 10){ System.out.println("Num " + i); i++; }

Do--while loop statements Do This is another looping statement that tests the given condition past so you can say that the do-while looping statement is a past-test loop statement. First the do block statements are executed then the condition given in while statement is checked. So in this case, even the condition is false in the first attempt, do block of code is executed at least once. Syntax: do{ <statement>;...;...; }while (expression);

Example of dodo-while loop Here first do block of code is executed and current value "1 is printed then the condition i<=10 is checked. Here "1" is less than number "10" so the control comes back to do block. This process continues till value of i becomes greater than 10. int i = 1; do{ System.out.println("Num: " + i); i++; }while(i <= 10);

for loop statements This is also a loop statement that provides a compact way to iterate over a range of values. From a user point of view, this is reliable because it executes the statements within this block repeatedly till the specified conditions is true. Syntax: for (initialization; condition; increment or decrement) { <statement>;...;...; } initialization: The loop is started with the value specified. condition: It evaluates to either 'true' or 'false'. If it is false then the loop is terminated. increment or decrement: After each iteration, value increments or decrements.

Example of for loop Here num is initialized to value "1", condition is checked whether num<=10. If it is so then control goes into the loop and current value of num is printed. Now num is incremented and checked again whether num<=10.if it is so then again it enters into the loop. This process continues till num>10. It prints values 1 to10 on the screen. for (int num = 1; num <= 10; num++) { System.out.println("Num: " + num); }

Branching Statements In Java, there are three types of branching statements: Break statement Continue statement Return statement

Break statement The break statement is a branching statement that contains two forms: labeled and unlabeled. The break statement is used for breaking the execution of a loop (while, do-while and for). It also terminates the switch statements. Syntax: break; // breaks the innermost loop or switch statement. break label; // breaks the outermost loop in a series of nested loops.

Example of break statement When if statement evaluates to true it prints "data is found" and comes out of the loop and executes the statements just following the loop.

Continue statement This is a branching statement that are used in the looping statements (while, do-while and for) to skip the current iteration of the loop and resume the next iteration. Syntax: continue;

Example of continue statement

Return statement It is a special branching statement that transfers the control to the caller of the method. This statement is used to return a value to the caller method and terminates execution of method. This has two forms: one that returns a value and the other that can not return. The returned value type must match the return type of method. Syntax: return; return values; return; //This returns nothing. So this can be used when method is declared with void return type. return expression; //It returns the value evaluated from the expression.

Example of return statement Here Welcome() function is called within println() function which returns a String value "Welcome to roseindia.net". This is printed to the screen.