MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

Similar documents
AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

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

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

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

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

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

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

Computational Expression

Multiple-Subscripted Arrays

Introduction to C Programming

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

CT 229 Java Syntax Continued

BIL101E: Introduction to Computers and Information systems Lecture 8

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

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

CS302: Self Check Quiz 2

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

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Lecture 5 Tao Wang 1

Control Structures in Java if-else and switch

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

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

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Introduction to Programming

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

Conditionals and Loops

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

SELECTION. (Chapter 2)

Software Design & Programming I

Fundamentals of Programming. Lecture 6: Structured Development (part one)

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Chapter 4: Control Structures I (Selection)

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

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

Program Elements -- Introduction

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Chapter 3 Structured Program Development

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

JavaScript: Control Statements I

Structured Program Development

Control Structures: if and while A C S L E C T U R E 4

CS111: PROGRAMMING LANGUAGE II

Chapter 3. Selections

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

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Logical Operators and switch

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

Conditional Statements

BRANCHING if-else statements

ECE 122 Engineering Problem Solving with Java

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CS 106 Introduction to Computer Science I

Flow of Control. Chapter 3

Decision Making in C

Flow of Control. Chapter 3. Chapter 3 1

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

Control Structures in Java if-else and switch

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

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

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

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

1007 Imperative Programming Part II

Selections. CSE 114, Computer Science 1 Stony Brook University

Accelerating Information Technology Innovation

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Chapter 17. Fundamental Concepts Expressed in JavaScript

Program Development. Chapter 3: Program Statements. Program Statements. Requirements. Java Software Solutions for AP* Computer Science A 2nd Edition

5. Selection: If and Switch Controls

CS 105 Lecture 5 Logical Operators; Switch Statement. Wed, Feb 16, 2011, 5:11 pm

Chapter 3: Program Statements

4 Programming Fundamentals. Introduction to Programming 1 1

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Flow Control. So Far: Writing simple statements that get executed one after another.

In this chapter you will learn:

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

The Vic Class. a1 b e g end. a d2 end. a3 b e g3 h3 end. a d4 e4 end. GarthB LyleL stack

Algorithms and Conditionals

Chapter 2 - Control Structures

Branches, Conditional Statements

Introduction. C provides two styles of flow control:

Chapter 4: Making Decisions

Full file at

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

C: How to Program. Week /Mar/05

APCS Semester #1 Final Exam Practice Problems

Flow of Control. Chapter 3 Part 3 The Switch Statement

QUIZ: What value is stored in a after this

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Chapter 4: Making Decisions

Most of the class will focus on if/else statements and the logical statements ("conditionals") that are used to build them. Then I'll go over a few

CONDITIONAL EXECUTION: PART 2

Chapter 7: Javascript: Control Statements. Background and Terminology. Background and Terminology. Background and Terminology

Transcription:

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1 March 28-30, 2007 if Single-Selection Statement The conditional statement in Java has the form if (condition) statement The condition must be surrounded by parentheses. In Java, as in most programming languages, you will often want to execute multiple statements when a single condition is true. In this case, you use a block statement that takes the form statement 1 statement 2... Programs use selection statements to choose among alternative courses of action. For example, suppose that the passing grade on an exam is 60. The pseudocode statement If student's grade is greater than or equal to 60 Print "Passed" determines whether the condition "student's grade is greater than or equal to 60" is true or false. If the condition is true, "Passed" is printed, and the next pseudocode statement in order is "performed." (Remember that pseudocode is not a real programming language.) If the condition is false, the Print statement is ignored, and the next pseudocode statement in order is performed. The indentation of the second line of this selection statement is optional, but recommended, because it emphasizes the inherent structure of structured programs. The preceding pseudocode If statement may be written in Java as if ( studentgrade >= 60 ) System.out.println( "Passed" ); 1 Does not reflect the actual class coverage. 1

if... Double-Selection Statement The if single-selection statement performs an indicated action only when the condition is true; otherwise, the action is skipped. The if... double-selection statement allows the programmer to specify an action to perform when the condition is true and a different action when the condition is false. For example, the pseudocode statement If student's grade is greater than or equal to 60 Print "Passed" Else Print "Failed" prints "Passed" if the student's grade is greater than or equal to 60, but prints "Failed" if it is less than 60. In either case, after printing occurs, the next pseudocode statement in sequence is "performed." The preceding If...Else pseudocode statement can be written in Java as if ( grade >= 60 ) System.out.println( "Passed" ); System.out.println( "Failed" ); Note that the body of the is also indented. Whatever indentation convention you choose should be applied consistently throughout your programs. It is difficult to read programs that do not obey uniform spacing conventions. Nested if... Statements A program can test multiple cases by placing if... statements inside other if... statements to create nested if... statements. For example, the following pseudocode represents a nested if... that prints A for exam grades greater than or equal to 90, B for grades in the range 80 to 89, C for grades in the range 70 to 79, D for grades in the range 60 to 69 and F for all other grades: If student's grade is greater than or equal to 90 Print "A" If student's grade is greater than or equal to 80 Print "B" If student's grade is greater than or equal to 70 Print "C" 2

If student's grade is greater than or equal to 60 Print "D" Print "F" This pseudocode may be written in Java as if ( studentgrade >= 90 ) System.out.println( "A" ); if ( studentgrade >= 80 ) System.out.println( "B" ); if ( studentgrade >= 70 ) System.out.println( "C" ); if ( studentgrade >= 60 ) System.out.println( "D" ); System.out.println( "F" ); Conditional Operator (?:) Java provides the conditional operator (?:) that can be used in place of an if... statement. This is Java's only ternary operatorthis means that it takes three operands. Together, the operands and the?: symbol form a conditional expression. The first operand (to the left of the?) is a boolean expression (i.e., a condition that evaluates to a boolean valuetrue or false), the second operand (between the? and :) is the value of the conditional expression if the boolean expression is TRue and the third operand (to the right of the :) is the value of the conditional expression if the boolean expression evaluates to false. For example, the statement System.out.println( studentgrade >= 60? "Passed" : "Failed" ); prints the value of println's conditional-expression argument. The conditional expression in this statement evaluates to the string "Passed" if the boolean expression studentgrade >= 60 is true and evaluates to the string "Failed" if the boolean expression is false. Thus, this statement with the conditional operator performs essentially the same function as the if... statement shown earlier in this section. The precedence of the conditional operator is low, so the entire conditional expression is normally placed in parentheses. QUESTIONS 1- The statement is used to execute one action when a condition is true and another when that condition is false. 2- if ( gender == 1 ) System.out.println( "Woman" ); ; 3

System.out.println( "Man" ); 3- Determine the output for each of the given sets of code when x is 9 and y is 11. a- if ( x < 10 ) if ( y > 10 ) System.out.println( "*****" ); b- if ( x < 10 ) if ( y > 10 ) System.out.println( "*****" ); 4- Modify the given code to produce the output shown in each part of the problem. Use proper indentation techniques. Make no changes other than inserting braces and changing the indentation of the code. if ( y == 8 ) a. Assuming that x = 5 and y = 8, the following output is produced: @@@@@ b. Assuming that x = 5 and y = 8, the following output is produced: @@@@@ &&&&& c- Assuming that x = 5 and y = 8, the following output is produced: @@@@@ $$$$$ &&&&& 5- Write equal of this conditional operator statemement.(?:) 4

System.out.println( studentgrade >= 60? "Passed" : "Failed" ); 1- if- SOLUTIONS 2- There will no ( ; ) after. 3- a- ***** b- ***** $$$$$ a- if ( y == 8 ) b- if ( y == 8 ) c- if ( y == 8 ) 5- if( studentgrade>=60) System.out.println( Passed ); 5

System.out.println( Failed ); 6