C Programming Decision Making

Similar documents
Decision Making -Branching. Class Incharge: S. Sasirekha

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

Control Structure: Selection

Problem Solving and 'C' Programming

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

SELECTION STATEMENTS:

Chapter 5: Control Structures

DECISION CONTROL AND LOOPING STATEMENTS

Decision Making in C

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

Flow Control. CSC215 Lecture

Prepared by: Shraddha Modi

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

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

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

Module 4: Decision-making and forming loops

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

Fundamentals of Computer Programming Using C

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Computers Programming Course 7. Iulian Năstac

CONDITIONAL EXECUTION: PART 2

Chapter 4 - Notes Control Structures I (Selection)

OER on Loops in C Programming

BRANCHING if-else statements

Full file at C How to Program, 6/e Multiple Choice Test Bank

Control Structures in Java if-else and switch

Introduction. C provides two styles of flow control:

EE 121. Office suite. Lecture 3: Introduction to programming and C programming language

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

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CHAPTER : 9 FLOW OF CONTROL

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

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

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

Lecture 8. Daily Puzzle

Computer Programming: C++

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Decision Making and Loops

Subject: PIC Chapter 2.

Conditional Statement

Control Statements. If Statement if statement tests a particular condition

C: How to Program. Week /Mar/05

Control Structures in Java if-else and switch

Conditional Statement

MODULE 2: Branching and Looping

Chapter 2 - Introduction to C Programming

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Conditional Control Structures. Dr.T.Logeswari

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

M1-R4: Programing and Problem Solving using C (JAN 2019)

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

DELHI PUBLIC SCHOOL TAPI

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #13. Loops: Do - While

Control Structure: Loop

8. Control statements

Logic & program control part 3: Compound selection structures

Computer Science & Engineering 150A Problem Solving Using Computers

ME30 Lab3 Decisions. February 20, 2019

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

Introduction to C/C++ Lecture 3 - Program Flow Control

AMCAT Automata Coding Sample Questions And Answers

Accelerating Information Technology Innovation

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

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

CS240: Programming in C

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

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

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

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

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

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

Programming Style. Quick Look. Features of an Effective Style. Naming Conventions

Comments. Comments: /* This is a comment */

Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

Chapter 2, Part III Arithmetic Operators and Decision Making

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

Operators and Control Flow. CS449 Fall 2017

COE608: Computer Organization and Architecture

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

CS16 Exam #1 7/17/ Minutes 100 Points total

Lesson 04. Control Structures I : Decision Making. MIT 31043, VISUAL PROGRAMMING By: S. Sabraz Nawaz

CS4215 Programming Language Implementation

CHAPTER 9 FLOW OF CONTROL

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Principles of Computer Science

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

G Programming Languages - Fall 2012

n Group of statements that are executed repeatedly while some condition remains true

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

DEBUGGING: STATIC ANALYSIS

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Transcription:

1 P a ge C Programming, Decision Making C Programming Decision Making Introduction to Decision Making OR Control Statement OR Branching Statement: - We know that the execution of program is done instruction by instruction. After executing one instruction your computer will execute another instruction. This flow of the instruction execution is known as sequential execution. But some time you need to change the flow of the instruction based on the certain condition. To change the flow based on the condition we are using the Decision making statement or control statement. C supports following control statements or branching statements or decision-making statements: 1. if statement 2. if... statement 3. Nested if... statement 4. Ladder if statement 5. switch statement 6. Conditional Operator statement 7. goto statement if Statement: - It is most powerful and widely used statement used for decision making. The syntax of the if statement is: if( condition) Statement-2;... First your computer will check the condition. If the condition is true then computer put non-zero value and execute the Block Statement-1, after executing block statement it will execute Statement-2. If condition is false then computer put zero value and will not execute Block Statement-1 but directly execute Statement-2. condition Block Statement-1 Statement-2 The example of the if statement is: if(marks < 40) printf( You are fail\n ); printf( Total Marks = %d\n,marks); Here first the if statement is executed. Your computer will check whether marks is less than 40 or not and if it is that then it will print message You are fail and then print the total marks otherwise it directly print the total marks.

2 P a ge C Programming, Decision Making if... Statement: - It is extension of the if statement. It s format is: if( condition) Block Statement-2; Statement-3; First your computer will check the condition. If the condition is true then computer execute the Block Statement-1. If condition is false then computer will execute Block Statement-2. After executing either Block Statement-1 or Block Statement-2 it will execute the Statement-3. Block Statement-1 condition Block Statement-2 Statement-3 The example of the if... statement is: if(marks < 40) printf( You are fail\n ); printf( You are pass\n ); printf( Total Marks = %d\n,marks); Here first the if statement is executed. Your computer will check whether marks is less than 40 or not and if it is that then it will print message You are fail otherwise it will print the message You are pass. After print pass or fail it will print total marks. Nested if... Statement: - When we are using if... statement within one if... statement then it will make the nested if... statement. The format of nested if... statement is: if( condition-1) if( condition-2) Block Statement-2;

3 P a ge C Programming, Decision Making Block Statement-3; Statement-4;... Here first your computer will check the condition-1. If the condition is true then computer check another condition-2. If condition-2 is true then it will execute Block Statement-1 otherwise execute the Block Statement-2. If -condition1 is false then it will execute Block Statement-3. After completing the nested if... statement execution computer will execute Statemetn-4. condition-1 Block Statement-3 condition-2 Block Statement-2 Block Statement-1 Statement-4 The example of the nested if... is: if(no1>=no2) if(no1>=no3) printf( Number1 is Maximum\n ); printf( Number3 is Maximum\n ); if(no2>=no3) printf( Number2 is Maximum\n ); printf( Number3 is Maximum\n ); Here first the if statement is to check whether no1>=no2. If condition is true then it will check whether no1>=no3. If second condition is true then it will print message Number1 is Maximum otherwise print the message Number3 is Maximum.

4 P a ge C Programming, Decision Making If condition no1>=no2 is false then computer will check the condition no2>=no3 and it true then it will print message Number2 is Maximum otherwise print the message Number3 is Maximum. if... if Statement (ladder if Statement) : - It is used to make multipath decisions. Multipath decision involves chain of if with if. The format of this statement is: if( condition-1) if( condition-2) Block Statement-2; if( condition-3) Block Statement-3; Block Statement-4; Statement-5;... Here first your computer will check the condition-1. If the condition is true then computer will execute Block Statement-1 otherwise it will check the condition-2. If the condition is true then computer will execute Block Statement-2 otherwise it will check the condition-3. If condition is true then it will execute Block Statement-3 otherwise it will execute Block Statement-4. condition-1 condition-2 Block Statement-1 condition-3 Block Statement-2 Block Statement-3 Block Statement-4 Statement-5

5 P a ge C Programming, Decision Making The example of the this statement is: if(percentage >=70) printf( Distinction\n ); if(percentage >= 60) printf( First Class\n ); if(percentage >= 50) printf( Second Class\n ); if(percentage >=40) printf( Pass Class\n ); printf( Fail\n ); In above example first computer check whether percentage>=70 or not. If it is true then it will print message Distinction otherwise check another condition percentage>=60. If it is true then print message First Class and in the same at last it will print the message Fail if above all condition is fail. switch Statement: - When there are multiple conditions in the if statement, it becomes difficult to read and understand program. In this case we can use switch statement, which allows checking the value of variable against the multiple case value. The format of this statement is: switch(variablename) case v1 : case v2 : Block Statement-2; case v3 : Block Statement-3;... default : Block Statement-4; Statement-5; Here if the value of variable= = v1 then Block Statement-1 will be executed. If variable= = v2 then Block Statement-2 will be executed. In similar way if at last no match found for the variable then default statement is executed. Here we have to put the break statement to complete the execution of switch statement after matching one case.

6 P a ge C Programming, Decision Making variable for match = =V1 = =V2 = =V3 default Block Statement-1 Block Statement-2 Block Statement-3 Block Statement-4 Statement-5 Consider the following example of the switch statement to accept the month in number and print into word. switch (month) case 1 : printf( January ); case 2 : printf( February ); case 5 : printf( May ); case 10 : printf( October ); default : printf( You have entered wrong value ); In above program if you enter month equal to 1 then print January, month equal to 2 then February, month equal to 5 then May, month equal to 10 then October. If no any match is found for month then print, You have entered wrong value. Similar to nested if... you can use nested switch statement. You can use only 15 levels of nested switch. Also you can take only 257 cases into switch statement. Conditional Operator (?: Operator) : - C provides the facility of the operator to check the condition by using the conditional operator having three operands. The format of the condition: (Condition)? Statement1: Statement2; If condition is true then statement1 will be executed otherwise statement2 will be executed. Here you can also represent the condition for assignment operation: Variable = (Condition)? Value1: Value2;

7 P a ge C Programming, Decision Making Here if condition is true then variable has assigned the value1 otherwise it has assigned the value2. Consider the following example to understand the conditional operator: commission = (sale > 50000)? 2000: 0; In above example if your sale is greater than 50000 then your commission value is 2000 otherwise it is zero. Above condition you can represent using if statement as under: if (sale > 50000) commission=2000; commission =0; The advantage of the conditional operator is that you can represent the condition in shorter form. But some times it is difficult to understand when you are using multiple conditional operators. goto Statement: - goto is one kind of jump statement in the C language. It transfers your control from one statement to another statement randomly. In goto statement there is one label is used to indicate at which position you want to make the jump. Label is end with the colon (:). Whenever you have to make the jump you have to write statement goto Label. There are two kind of jumps performed by goto: 1. Forward jump: Here the jump is made to the next following statements. If label is after the goto statement then this kind of situation is arised. goto LABEL; LABEL : 2. Backward jump: Here the jump is made to the previous statements. When label is before the goto statement then this kind of situation is arised. The syntax of backward jump is, LABEL : goto LABEL; Consider the following example to understand goto statement, main( ) int a, b, c; CAL : printf( Enter a & b : ); scanf( %d%d,&a,&b); c=a + b; printf( c = %d\n,c); if(c<=0) goto CAL;

8 P a ge C Programming, Decision Making Here the program is used to calculate the c value up to the value is not going to be larger then zero. Generally goto statement is used to read the data again in your program. It is also used in the loop to break the loop, while(condition).. goto OUT;. OUT : B We have to avoid the use of goto statement because of 1. Some compiler generates less efficient code if you are using goto statement. 2. Your program becomes unreadable. 3. Program logic becomes complicated and program becomes fewer under-stables.