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

Similar documents
9/9/2017. If ( condition ) { statements ; ;

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

Arrays in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

2 The Decision Control Structure

Structures in C. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

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

Chapter 3 Structured Program Development

Conditional Statement

Decision Making -Branching. Class Incharge: S. Sasirekha

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

Kadi Sarva Vishwavidyalaya, Gandhinagar

Chapter 2, Part III Arithmetic Operators and Decision Making

DEPARTMENT OF ELECTRICAL AND ELECTRONIC ENGINEERING PROGRAMMING TECHNIQUES I EE271

DBMS and its Architecture

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Structured Program Development

Unit 3 Decision making, Looping and Arrays

Control Structures in Java if-else and switch

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

DB Basic Concepts. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Lecture 3 Tao Wang 1

Introduction to C Programming

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

Control Statements. If Statement if statement tests a particular condition

Decision Making and Branching

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

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

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

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

QUIZ: What value is stored in a after this

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

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

Computers Programming Course 7. Iulian Năstac

5. Control Statements

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

DECISION MAKING STATEMENTS

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

Introduction. C provides two styles of flow control:

Control Structures in Java if-else and switch

Introduction to Programming

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

Solutions: Program 1: Program 2: //To calculate the Gross salary of an Employee

Chapter 3 Selection Statements

LECTURE 04 MAKING DECISIONS

Unit 8. Structures and Unions. School of Science and Technology INTRODUCTION

Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS Institute of Information Technology. Mobile Communication

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

TheCrazyProgrammer.com Page 1

Chapter 4: Making Decisions

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

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

Chapter 4: Making Decisions

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

Quiz 1: Functions and Procedures

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

Selection Statements. Pseudocode

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

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

Questions Bank. 14) State any four advantages of using flow-chart

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

Part II IT Infrastructure (Computer Software)

Conditions and Logical Expressions. C Programming

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

3 The L oop Control Structure

5. Selection: If and Switch Controls

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

The following expression causes a divide by zero error:

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

Computer Science & Engineering 150A Problem Solving Using Computers

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Selection Structures II

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

CS 115 Lecture 8. Selection: the if statement. Neil Moore

BCA-105 C Language What is C? History of C

The C++ Language. Arizona State University 1

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

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

Decision making with if Statement : - Control Statements. Introduction: -

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Chapter 2. Section 2.3 if and if-else statements. CS 50 Hathairat Rattanasook

Problem Solving with Decisions. T.Fatin Alhila

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Maltepe University Computer Engineering Department. Algorithms and Programming. Chapter 4: Conditionals - If statement - Switch statement

Outline. Review Choice Statements. Review Sequential Flow. Review Choice Before Loops. Review Choice After Loops

Software Development & Education Center C Programming

Relational operators (1)

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

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS

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

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Software Reference Sheet: Inserting and Organizing Data in a Spreadsheet

BRANCHING if-else statements

BIL101E: Introduction to Computers and Information systems Lecture 8

Any Integer Can Be Written as a Fraction

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

Transcription:

Decision Control Structure DCS COMSATS Institute of Information Technology Rab Nawaz Jadoon Assistant Professor COMSATS IIT, Abbottabad Pakistan Introduction to Computer Programming (ICP)

Decision control structure Before this we have used sequence control structure in which, Various steps are executed sequentially, i.e. in the same order in which they appear in the program. By default the instructions in a program are executed sequentially. 2

Decision control structure Many a times, we want a set of instructions to be executed in one situation, and an entirely different set of instructions to be executed in another situation. This kind of situation is dealt in C programs using a decision control instruction. 3

Decision control structure Decision control instruction can be implemented in C using: The if statement The if-else statement The conditional operators 4

The If Statement Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this: if ( this condition is true ) execute this statement ; The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed; instead the program skips past it. 5

The If Statement We express a condition using C s relational operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other. 6

Simple Program using If statement 7

If Control Structure 8

Problem While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses??? 9

Solution (Flow Chart & Program) 10

Multiple statements within If More than one statement to be executed if the expression following if is satisfied. If such multiple statements are to be executed then they must be placed within a pair of braces. For example, If(condition) { --------- --------- } 11

Program Flow chart Program 12

Quiz #include<stdio.h> #include<conio.h> main( ) { int a = 300, b, c ; if ( a >= 400 ) { b = 300 ; c = 200 ; printf ( "\n %d %d", b, c ) ; } getche (); } What would be the output??? Nothing What would be the output, if a=500??? 300 200 13

The if-else Structure Execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false. This is done using the if-else control structure. For example, If student s grade is greater than or equal to 60 else Print Passed Print Failed 14

If-else Example 15

Program illustrating if-else 16

What would be the output of the following program??? Quiz??? x and y are equal 17

What would be the output of the following program??? Quiz??? C is WOW 18

Identify the error from the following Quiz??? } 19

Identify the errors 20

Forms of if 21

Use of logical operators C allows usage of three logical operators, namely, &&, and!. These are to be read as AND, OR and NOT respectively. two of them are composed of double symbols: and &&. Don t use the single symbol and &. These single symbols also have a meaning. They are bitwise operators. The first two operators, && and, allow two or more conditions to be combined in an if statement. 22

Working of three logical operators 23

Example The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules: Percentage above or equal to 60 - First division Percentage between 50 and 59 - Second division Percentage between 40 and 49 - Third division Percentage less than 40 - Fail Write a program to calculate the division obtained by the student. 24

Solution (Method-1) 25

Problems faced in the program This is a straight forward program. Observe that the program uses nested if-elses. This leads to three disadvantages: As the number of conditions go on increasing the level of indentation also goes on increasing. As a result the whole program creeps to the right. Care needs to be exercised to match the corresponding ifs and elses. Care needs to be exercised to match the corresponding pair of braces. Note: All these three problems can be eliminated by usage of Logical operators. (Method 2) 26

Solution (Method-2) 27

The Else if Clause There is one more way in which we can write the previous program, 28

The Else if Clause Note that the else if clause is nothing different. It is just a way of rearranging the else with the if that follows it. This would be evident if you look at the following code: 29

Problem The Else if Clause A company insures its drivers in the following cases: If the driver is married. If the driver is unmarried, male & above 30 years of age. If the driver is unmarried, female & above 25 years of age. In all other cases the driver is not insured. If the marital status, sex and age of the driver are the inputs. Write a program to determine whether the driver is to be insured or not? 30

Solution-1 31

Solution-2 32

Problem Write a program to calculate the salary as per the following table: 33

Solution 34

Cont 35

This operator reverses the result of the expression it operates on. The! Operator For example, if the expression evaluates to a nonzero value, then applying! operator to it results into a 0 and vice versa. If the expression evaluates to zero then on applying! operator to it makes it 1, a non-zero value. The final result (after applying!) 0 or 1 is considered to be false or true respectively. For Example! ( y < 10 ), y is less than 10, the! Operator returns false. 36

The! Operator The NOT operator is often used to reverse the logical value of a single variable, as in the expression if (! flag ) 37

Identify the error from the following??? 38

Conditional Operator The conditional operators?and :are sometimes called ternary operators since they take three arguments. They form a kind of foreshortened if-then-else. Their general form is, expression 1? expression 2 : expression 3 What this expression says is: if expression 1 is true (that is, if its value is non-zero), then the value returned will be expression 2, otherwise the value returned will be expression 3. 39

Examples The following program store 3 in y, if x is greater than 5, otherwise it will store 4 in y. The comparison with if else is as under, Equivalent to 40

Example 1 would be assigned to y if a >=65 && a <=90 evaluates to true, otherwise 0 would be assigned 41

Important point It s not necessary that the conditional operators should be used only in arithmetic statements. This is illustrated in the following examples: 42

The conditional operators can be nested as shown below. Cont Check out the following conditional expression: a > b? g = a : g = b ; 43

Limitation The limitation of the conditional operators is that after the? Or after the : only one C statement can occur. In practice rarely is this the requirement. Therefore, in serious C programming conditional operators aren t as frequently used as the if-else. 44

45