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

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

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

Decision Making in C

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch

Lesson 7 Part 2 Flags

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

PROGRAMMING FUNDAMENTALS

Jagannath Institute of Management Sciences Lajpat Nagar

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

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

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

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

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Java Bytecode (binary file)

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

INTRODUCTION TO COMPUTER SCIENCE - LAB

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

Java Basic Programming Constructs

Decision Making -Branching. Class Incharge: S. Sasirekha

SELECTION. (Chapter 2)

Introduction to Programming Using Java (98-388)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

DPCompSci.Java.1718.notebook April 29, 2018

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

Java Programming for Selenium

Flow Control. CSC215 Lecture

In this lab, you will learn more about selection statements. You will get familiar to

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Getting started with Java

Flow Control. Key Notion. Statement Categories. 28-Oct-10

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Selections. CSE 114, Computer Science 1 Stony Brook University

( &% class MyClass { }

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

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

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

Handout 4 Conditionals. Boolean Expressions.

Array. Array Declaration:

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

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Accelerating Information Technology Innovation

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

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section :

Key Differences Between Python and Java

Topics. Chapter 5. Equality Operators

CS 106 Introduction to Computer Science I

McGill University School of Computer Science COMP-202A Introduction to Computing 1

CONDITIONAL EXECUTION: PART 2

Introduction to Computer Science Unit 2. Notes

Lecture 5 Tao Wang 1

Important Java terminology

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

Programming and Data Structures

AP Computer Science A Summer Assignment 2017

In this chapter, you will:

Lecture 14. 'for' loops and Arrays

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

HISTORY OF C LANGUAGE. Facts about C. Why Use C?

CS302: Self Check Quiz 2

CSE 142 Su 04 Computer Programming 1 - Java. Objects

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

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.

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Review for Test 1 (Chapter 1-5)

Building Java Programs

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Midterm Examination (MTA)

(Not Quite) Minijava

Biyani's Think Tank. Concept based notes. Java Programming. (B.Tech)

Introduction to Java

Chapter 4: Making Decisions

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

5. Selection: If and Switch Controls

Building Java Programs

Chapter 4: Making Decisions

An overview of Java, Data types and variables

Control Flow Statements

Flow of Control. Chapter 3

Boolean Expressions (Conditions)

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

Flow of Control. Chapter 3 Part 3 The Switch Statement

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Object Oriented Programming with Java

Computer Science is...

Chapter 3 Selection Statements

Making Decisions. Agenda

Object Oriented Software Design

COMPUTER PROGRAMMING QUICK GUIDE COMPUTER PROGRAMMING OVERVIEW

Topic 5: Enumerated Types and Switch Statements

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-oriented programming in...

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

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Question: Total Points: Score:

Language Reference Manual

Transcription:

Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false. Following is the general form of a typical decision making structure found in most of the programming languages: Java programming language provides following types of decision making statements. Click the following links to check their detail. Statement Description if statement An if statement consists of a boolean expression followed by one or more statements. if...else statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. 1

nested if statements You can use one if or else if statement inside another if or else if statement(s). switch statement A switch statement allows a variable to be tested for equality against a list of values. If Statement in Java An if statement consists of a Boolean expression followed by one or more statements. Syntax Following is the syntax of an if statement: if(boolean_expression) { //Statements will execute if the Boolean expression is true If the Boolean expression evaluates to true then the block of code inside the if statement will be executed. If not, the first set of code after the end of the if statement (after the closing curly brace) will be executed. Flow Diagram 2

Example public class Test { public static void main(string args[]){ int x = 10; if( x < 20 ){ System.out.print("This is if statement"); This will produce the following result: This is if statement. If-else Statement in Java An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Syntax Following is the syntax of an if...else statement: if(boolean_expression){ else{ //Executes when the Boolean expression is true //Executes when the Boolean expression is false If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed. 3

Flow Diagram Example public class Test { public static void main(string args[]){ int x = 30; if( x < 20 ){ System.out.print("This is if statement"); else{ System.out.print("This is else statement"); This will produce the following result: 4

This is else statement The if...else if...else Statement An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement. When using if, else if, else statements there are a few points to keep in mind. An if can have zero or one else's and it must come after any else if's. An if can have zero to many else if's and they must come before the else. Once an else if succeeds, none of the remaining else if's or else's will be tested. Syntax Following is the syntax of an if...else statement: if(boolean_expression 1){ //Executes when the Boolean expression 1 is true else if(boolean_expression 2){ //Executes when the Boolean expression 2 is true else if(boolean_expression 3){ //Executes when the Boolean expression 3 is true else { //Executes when the none of the above condition is true. Example public class Test { public static void main(string args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); else if( x == 20 ){ System.out.print("Value of X is 20"); else if( x == 30 ){ System.out.print("Value of X is 30"); else{ System.out.print("This is else statement"); 5

This will produce the following result: Value of X is 30 Nested if Statement in Java It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement. Syntax The syntax for a nested if...else is as follows: if(boolean_expression 1){ //Executes when the Boolean expression 1 is true if(boolean_expression 2){ //Executes when the Boolean expression 2 is true You can nest else if...else in the similar way as we have nested if statement. Example public class Test { public static void main(string args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); This will produce the following result: 6

X = 30 and Y = 10 Switch Statement in Java A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. Syntax The syntax of enhanced for loop is: switch(expression){ case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements The following rules apply to a switch statement: The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums. You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon. The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. 7

A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. Flow Diagram Example public class Test { public static void main(string args[]){ //char grade = args[0].charat(0); char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); 8

break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); System.out.println("Your grade is " + grade); Compile and run the above program using various command line arguments. This will produce the following result: $ java Test Well done Your grade is a C $ The? : Operator: We have covered conditional operator? : in the previous chapter which can be used to replace if...else statements. It has the following general form: Exp1? Exp2 : Exp3; Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. To determine the value of the whole expression, initially exp1 is evaluated. If the value of exp1 is true, then the value of Exp2 will be the value of the whole expression. If the value of exp1 is false, then Exp3 is evaluated and its value becomes the value of the entire expression. 9

10