Control Statements: Part1  if, if…else, switch 1.

Slides:



Advertisements
Similar presentations
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Advertisements

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter4: Control Statements Part I.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Introduction to Java Programming, 4E Y. Daniel Liang.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
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.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter#3 Part1 Structured Program Development in C++
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Branching statements.
Introduction to Computer Programming
Chapter 4 – C Program Control
The if…else Selection Statement
Control Statements: Part 1
EGR 2261 Unit 4 Control Structures I: Selection
Java™ How to Program, 10/e Late Objects Version
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
JavaScript: Control Statements.
SELECTION STATEMENTS (1)
Chapter 4 Control Statements: Part I
Structured Program
3 Control Statements:.
Control Statements Paritosh Srivastava.
Lecture 9: Implementing Complex Logic
Control Statements:.
Presentation transcript:

Control Statements: Part1  if, if…else, switch 1

 Normally, statements in program are executed one after the other in the order in which they’re written.  This is called sequential execution  Example: calculate area of rectangle.  There are control statements enable you to specify that the next one in sequence.  This is called transfer of control. control statements  The control statements are categorized in almost two groups:  Selection control statements.  Repetition control statements. 2

 Sequential execution ◦ Statements are normally executed one after the other in the order in which they are written  Transfer of control ◦ Specifying the next statement to execute that is not necessarily the next one in order ◦ Can be performed by the goto statement  Structured programming eliminated goto statements 3

 Java has three kinds of control structures  Sequence structure  Programs executed sequentially by default  Selection structures  if, if…else, switch  Repetition structures  while, do…while, for 4

 Selection Statements ◦ if statement  Single-selection statement ◦ if…else statement  Double-selection statement ◦ switch statement  Multiple-selection statement 5

 Are used to choose among alternative courses of action.  if Single-Selection Statement ◦ Execute an action if the specified condition is true  For example: suppose the passing mark on an exam is 60. The pseudo code statement: if student’s mark is greater than or equal to 60 then Print “Passed” 6

7

 Pseudo code  If student’s grade is greater than or equal to 60  Print “Passed”  If the condition is false, the Print statement is ignored, and next pseudo code statement in order is performed.  The preceding pseudo code If in Java:  If (studentGrade>=60)  System.out.println(“Passed”); 8

 Condition ◦ Expression can be either true or false  if statement ◦ If a condition is true, then the body of the if statement executed ◦ Control always resumes after the if statement ◦ Conditions in if statements can be formed using equality or relational operators (next slide) 9

10

 Relational expression: is an expression which compares two operands and returns a true or false answer.  Examples:  a>b c<d a==b  Relational expressions are used to test the conditions in selections, and looping statements. 11

12 Decision Making: Equality and Relational operators Condition An expression that can be true or false

 Compar ison.java  (1 of 2)  1. Class Comparison 1.1 main 1.2 Declarations 1.3 Input data (nextInt) 1.4 Compare two inputs using if statements 13 Test for equality, display result using printf. Compares two numbers using relational operator <.

 Comparis on.java  (2 of 2)  Program output 14 Compares two numbers using relational operators >, =.

◦ Line 6: begins class Comparison declaration ◦ Line 12: declares Scanner variable input and assigns it a Scanner that inputs data from the standard input ◦ Lines 14-15: declare int variables ◦ Lines 17-18: prompt the user to enter the first integer and input the value ◦ Lines 20-21: prompt the user to enter the second integer and input the value 15

◦ if statement to test for equality using (==)  If variables equal (condition true)  Line 24 executes  If variables not equal, statement skipped  No semicolon at the end of line 23  Empty statement  No task is performed ◦ Lines 26-27, 29-30, 32-33, and  Compare number1 and number2 with the operators !=,, =, respectively if ( number1 == number2 ) 24 System.out.printf( "%d == %d\n", number1, number2 );

 Confusing the equality operator, ==, with the assignment operator, =, can cause a logic error or a syntax error. The equality operator should be read as “is equal to,” and the assignment operator should be read as “gets” or “gets the value of.” To avoid confusion, some people read the equality operator as “double equals” or “equals equals.” 17

18

20

22  Ternary conditional operator ( ?: ) ◦ Three arguments (condition, value if true, value if false )  Code could be written: ◦ System.out.println( grade >= 60 ? “Passed” : “Failed” ); ConditionValue if trueValue if false

 Nested if…else statements ◦ if…else statements can be put inside other if…else statements  Dangling-else problem ◦ elses are always associated with the immediately preceding if unless otherwise specified by braces { }  Blocks ◦ Braces { } associate statements into blocks ◦ Blocks can replace individual statements as an if body  Empty statements ◦ Represented by placing a semicolon ( ; ) where a statement would normally be Can be used as an if body 23

24

 Can test multiple cases by placing if … else statements inside other if… else statements to create nested if…. Else statements  Pseudo code:  Example  If student’s grade is greater than or equal to 90 ◦ Print “A” ◦ Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else  Print “F” 25

◦ This pseudo code may be written in Java as  if ( studentGrade >= 90 ) System.out.println("A“); else if (studentGrade >= 80 ) System.out.println("B“); else if (studentGrade >= 70 ) System.out.println("C“); else if ( studentGrade >= 60 ) System.out.println("D“); else System.out.println("F“); 26

27

28

29

30

 Logical operators ◦ Allows for more complex conditions ◦ Combines simple conditions into complex conditions  Java operators ◦ & (logical AND) ◦ | (logical OR) ◦ ! (logical NOT) 31

32

 Logical AND (&) Operator ◦ Consider the following if statement  if (( gender == 1) & (age >= 65 ))  seniorFemales++; ◦ Combined condition is true  If and only if both simple conditions are true ◦ Combined condition is false  If either or both of the simple conditions are false 33

34

 Logical OR (|) Operator ◦ Consider the following if statement  if ( ( semesterAverage >= 90 ) |( finalExam >= 90 ))  Syste.out.println ( “Student grade is A” ); ◦ Combined condition is true  If either or both of the simple conditions are true ◦ Combined condition is false  If both of the simple conditions are false 35

36

 Short-Circuit Evaluation of Complex Conditions ◦ Parts of an expression containing && or || operators are evaluated only until it is known whether the condition is true or false ◦ Example  (( gender == 1 ) && ( age >= 65 ))  Stops immediately if gender is not equal to 1  Since the left-side is false, the entire expression must be false 37

 A for exam marks greater than or equal 90 and less than or equal 100  B for exam marks greater than or equal 80 and less than 90  C for exam marks than or equal to 70 and less than 80  D for exam marks than or equal to 60 and less than 70  F for all other marks If (marks>=90 && marks<=100) System.out.println(“A”); Else If (marks>=80 && marks<90) System.out.println(“B”); Else If (marks>=70 && marks<80) System.out.println(“C”); Else If (marks>=60 && marks<70) System.out.println(“D”); Else System.out.println(“F”); 38

int a=7; int b=8; if ((a>10) & (++b>7)) { System.out.print(" that is right” ); System.out.print(b); } 39 int a=7; int b=8; if ((a>10) & (++b>7)) System.out.print("that is right \n“); System.out.print(b); No output 9 int a=7; int b=8; if ((a>10) &&(++b>7)) Sytem.out.print("that is right \n”); System.out.print(b); 8

int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System.out.println("A"); } else System.out.println("B"); 40

 Logical Negation (!) Operator ◦ Unary operator ◦ Returns true when its operand is false, and vice versa ◦ Example  if ( !( grade == sentinelValue ) ) System.out.println( "The next grade is " +grade); is equivalent to: if ( grade != sentinelValue ) System.out.println("The next grade is " +grade );  Stream manipulator boolalpha ◦ Display bool expressions in words, “true” or “false” 41

42

43

44

45

46

PART TWO SWITCH STATEMENT 47

48

Selection Statements: Switch Statement The switch control statement that allows us to make a decision from the number of choices. Expression: It could be an integer constant like 1,2 or 3, or an expression that evaluates to an integer. Constant: is a specific value.

32 50

51

52

53

54

55 Wrong: Case >=50 Case a+b

char ch=‘’; Switch (ch) { case ‘a’: System.out.println(“a”); break; case ‘b’: System.out.println(“b”); break; case ‘c’: System.out.println(“c”); break; default: System.out.println(“Default”); }

int i=3; Switch (i) { case 1: System.out.println(“Welcome”); break; case 2: System.out.println(“Hello”); break; case 3: System.out.println(“JAVA”); break; default: System.out.println(“Default”); }

 مراجع :  بعض الشرائح تمت الإستعانة بها من محاضرات :  د. حكمت جابر  أ. محمد إبراهيم الدسوقي