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

Similar documents
Chapter 4 - Notes Control Structures I (Selection)

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

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

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

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

In this chapter you will learn:

C++ PROGRAMMING SKILLS Part 2 Programming Structures

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

Introduction to Programming

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

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

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

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

5. Selection: If and Switch Controls

Chapter 4: Control Structures I (Selection)

Increment and the While. Class 15

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

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

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

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

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

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

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

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

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

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

REPETITION CONTROL STRUCTURE LOGO

Chapter 3 Structured Program Development

CSI33 Data Structures

Flow Control. CSC215 Lecture

Why Is Repetition Needed?

Score score < score < score < 65 Score < 50

Examination for the Second Term - Academic Year H Mid-Term. Name of student: ID: Sr. No.

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

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

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

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

Chapter 4: Making Decisions

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Chapter 4: Making Decisions

Chapter 2 - Control Structures

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

VARIABLES & ASSIGNMENTS

CHAPTER 4 CONTROL STRUCTURES

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

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

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CSCE 206: Structured Programming in C++

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

The following expression causes a divide by zero error:

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

BRANCHING if-else statements

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Introduction. C provides two styles of flow control:

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

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

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

C++ Programming Lecture 1 Software Engineering Group

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

Programming Fundamentals

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.5. for loop and do-while loop

CS110D: PROGRAMMING LANGUAGE I

To become familiar with array manipulation, searching, and sorting.

LECTURE 04 MAKING DECISIONS

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Introduction to C Programming

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

Chapter 2 - Control Structures

Exceptions, Case Study-Exception handling in C++.

Chapter 2 - Control Structures

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

Review. Relational Operators. The if Statement. CS 151 Review #4

Chapter 3. More Flow of Control

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Making Decisions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

C++ Program Flow Control: Selection

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

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

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

IS 0020 Program Design and Software Tools

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Chapter 5: Control Structures II (Repetition)

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

Chapter 8 Statement-Level Control Structure

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Control Structures in Java if-else and switch

Computer Programming : C++

Conditional Statement

Unit 7. 'while' Loops

INTRODUCTION TO PROGRAMMING

If Control Construct

Transcription:

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

Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure. Differentiae between single, double and multiple selection statements. Identify and use the C++ conditional operator (?:) Use the nested control statement and explain its meaning. Determine the problems that require the user of nested control structure.

Control Structure (Logic Structure) Used to design data flow in modules and programs as whole Basic structures: Sequence structure: Process one instruction after another. Selection structures: Decision structure Make choices by using relational or logical operators Case structure Enable to pick up one of a set of tasks Repetition structure: Enable to repeat tasks.

Control structures in C++ C++ Control Structure Sequence structure Selection Structure Repetition structure If, if, switch While, do while, for

Sequence-structure Activity Diagram Add grade to total Corresponding C++ statement: total=total + grade; Add 1 to counter Corresponding C++ statement: counter=counter + 1;

Selection Statements Three types: Single selection statement Selects or ignores a single group of actions. Double-selection statement Selects between two groups of actions. Multiple-selection statement Selects among many group of actions.

If Single Selection Statement Use the keyword if Begin with if followed by condition; then action or group of actions (compound statements or blocks) are listed. Enclose compound statements between braces {} and indent these statements If condition is true, action is performed. Otherwise, actions is ignored Example: if (grade >=60) cout<< Passed ; Pseudocode: If student grade is greater than or equal to 60 Print Passed

If Single Selection Statement activity diagram Grade >=60 Print Passed Grade<60

If Single selection statement if (condition) action; Condition can be relational or equality operators or any other expressions if (condition) { action1; action2;.... actionn; }

if Double-selection Statement Use the keyword if and Begin with if followed by condition; then action or group of actions are listed. End with then action or group of actions are listed. If condition is true, action that followed by if is performed. Otherwise, action that followed by is performed.

Double Selection Statement if (condition) action1; action2; Condition can be relational or equality operators or any other expressions

if double-selection statement activity diagram Print Failed Grade<60 Grade >=60 Print Passed

if Double-selection Statement Example: If (grade >=60) cout<< Passed ; cout<< Failed ; Pseudocode: if student's grade is greater than or equal to 60 Print passed Else Print failed

Nested if.. Statements One inside another, test for multiple cases Once condition met, other statements skipped

Example:

Conditional Operator (?:) Provide similar result of if double selection statement Ternary operator requires three operands: Condition Value when condition is true Value when condition is false Syntax: Condition? Condition s true value: condition s false value

Conditional Operator (?:) Example Grade >=60? Cout<< Passed : cout<< Failed ; Can be written as: cout<<(grade >=60? passed : failed ); Example: int i=1, j=2, max; max=(i>j?i:j);

Nested if.. Statements if (conditioin1) action1; if (condition2) action2; if(condition3) action3; actionn;

Nested if statements if (condition1) { if (condition2) action1; { } if (condtion3) action2; action3; } action4;

Nested if Statements Write the pseudocode for if statements 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.

Nested if.. statements Example: if (grade>=90) // 90 and above gets A cout<< A ; if (grade >= 80) // 80-89 gets B cout<< B ; if(grade>=70) // 70-79 gets C cout<< C ; if(grade>=60) // 60-69 gets D cout<< D ; // less than 60 gets F cout<< F ;

Dangling Problem Each associated with immediately preceding if There is exception when placing braces {} Example x=10; y=2; if (x>5) if(y>5) cout<< x and y are >5 <<endl; Logical error!! cout<< x is <=5 ;

Dangling Problem Correctness x=10; y=2; if(x>5) { if(y>5) cout<< x and y are >5 <endl; } cout<< x is <=5 ;

Example:

Using Boolean variables bool flag1, flag2; if (flag1) if( flag1 flag2)

Implicit Typecasting int x1,x2; if(x1) if(x1 x2).

Note Confusing the equality operator == with the assignment operator = results in logic errors. #include <iostream> using namespace std; int main () { int num=0,x=0; if (x=2) cout<<"x is equal to 2"; cout<<"x is not equal to 2"; This message will always be printed!! } return 0;

Switch Multiple-selection Statement Perform actions based on possible values of variable or expression Use keywords switch, case, default and break. Begin with switch followed by controlling expression. Value of expression compared to case label then go to execute action for that case. No matching, the execution go to the optional default statement. Break causes immediate exit from switch statement.

Switch Multiple-selection Statement switch (expression) { case value1: action1; break; case value2: action2; break;. case valuen: actionn; break; default: action; }

Switch Multiple-selection Statement Example: switch (number) { case 0: cout<< too small, sorry! ; break; case 5: cout<< good job! <<endl; //fall through case 4: cout<< nice pick! <<endl; //fall through case 3: cout<< excellent! <<endl; //fall through case 2: cout<< masterfull! <<endl; //fall through case 1: cout<< incredible! <<endl; //fall through break; }

Example:

Example:

Example State the output for each of the following: When x is 9 and y is 11 When x is 11 and y is 9 a. if(x<10) if(y>10) cout<< ***** <<endl; cout< ##### <<endl; cout<< $$$$$ <<endl;

Example: b. if(x<10) { if(y>10) cout<< ***** <<endl; } { cout<< ##### <<endl; cout<< $$$$$ <<endl; }

Example:

Example: Write a C++ program that compares two integers using if statements, relational operators and equality operators. Sample output:

Switch Examples:

Switch Examples: The break keyword means "jump out of the switch statement, and do not execute any more code." To show how this works, examine the following piece of code: