Decision Making -Branching. Class Incharge: S. Sasirekha

Similar documents
4.1. Structured program development Overview of C language

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

DECISION CONTROL AND LOOPING STATEMENTS

Module 4: Decision-making and forming loops

C: How to Program. Week /Mar/05

BRANCHING if-else statements

C - Basic Introduction

Chapter 2 - Introduction to C Programming

MODULE 2: Branching and Looping

Introduction. C provides two styles of flow control:

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

Chapter 3 Structured Program Development

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

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

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

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

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

Structured Program Development

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

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

Fundamentals of Computer Programming Using C

Control Statements. If Statement if statement tests a particular condition

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

DECISION MAKING STATEMENTS

Control Structure: Selection

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

Control Structure: Loop

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

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

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

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

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

AMCAT Automata Coding Sample Questions And Answers

SELECTION STATEMENTS:

C Programming Decision Making

Chapter 1 & 2 Introduction to C Language

Decision Making and Loops

Flow Control. CSC215 Lecture

Introduction to C Programming

Conditional Statement

Chapter 4 - Notes Control Structures I (Selection)

8. Control statements

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

Technical Questions. Q 1) What are the key features in C programming language?

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

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Computer System and programming in C

LESSON 6 FLOW OF CONTROL

These are reserved words of the C language. For example int, float, if, else, for, while etc.

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Chapter 4. Flow of Control

Subject: Fundamental of Computer Programming 2068

Conditional Statement

Chapter 5: Control Structures

CSE 130 Introduction to Programming in C Control Flow Revisited

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

UIC. C Programming Primer. Bharathidasan University

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

Control Structures in Java if-else and switch

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

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

CHAPTER 4 CONTROL STRUCTURES

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C library = Header files + Reserved words + main method

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

Lecture 2: C Programming Basic

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

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

CHAPTER : 9 FLOW OF CONTROL

Lecture 6. Statements

3 The L oop Control Structure

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

Syntax and Variables

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

Computer Programming. Decision Making (2) Loops

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

What we have learned so far

Unit 3 Decision making, Looping and Arrays

Subject: PIC Chapter 2.

بسم اهلل الرمحن الرحيم

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Computers Programming Course 7. Iulian Năstac

Decision Making in C

Problem Solving and 'C' Programming

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

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

Structured Program Development in C

Programming for Engineers Introduction to C

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

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Chapter 8 Statement-Level Control Structure

Chapter 2. Introduction to C language. Together Towards A Green Environment

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

Input And Output of C++

5. Selection: If and Switch Controls

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

Transcription:

Decision Making -Branching Class Incharge: S. Sasirekha

Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the flow of the sequence of instructions. C language provides statements that can alter the flow of a sequence of instructions. These statements are called control statements. These statements help to jump from one part of the program to another. The control transfer may be conditional or unconditional.

if Statement Use Depending on the result of a condition the program will either branch and execute some code, then continue with the rest of the program. OR continue with the rest of the program. Syntax 1. if ( condition ) statement 1; 2. if ( condition ) statement 1; The additional line is white space, therefore there is no ; after if ( condition ) Warning: Inserting a ; may be accepted by the compiler but will produce unexpected results.

Although not required for single statement if constructs, It is recommend that braces are used as follows, so that there is no confusion to what is included within the construct. if ( condition ) statement 1; When more than 1 statement is used then braces MUST be used. if ( condition ) statement 1; statement 2; Description Use if to implement a conditional statement. When condition evaluates to be nonzero, statement 1 executes. If condition is false (0), statement 2 executes.

Example if (count < 50) count++; This code will add 1 to the value of count is < (less than) 50

Example program Calculate the absolute value of an integer # include <stdio.h> //Include the stdio.h file void main () // start of the program int numbers; printf ("Type a number:"); scanf ("%d", &number); if (number < 0) number = -number; printf ("The absolute value is %d \n", number);

If else If choices are required then an additional if... else pair is required. Syntax if ( condition ) statement 1; else statement 2;

Description When condition evaluates to be nonzero, statement 1 executes. If condition is false (0), statement 2 executes. Example if (x < y) z = x; else z = y; Explanation If the value of x is less than the value of y then z is assigned the value of x If the value of x is NOT less than the value of y then z is assigned the value of y

#if and #else preprocessor statements The #if and #else pre-processor statements (directives) look similar to the if and else statements, but have very different effects. #define TURBOC #ifdef TURBOC #define INT_SIZE 16 #else #define INT_SIZE 32 #endif

Example program Program find whether a number is negative or positive #include <stdio.h> void main () int num; printf ("Enter the number"); scanf ("%d", &num); if (num < 0) printf ("The number is negative"); else printf ("The number is positive");

Nested if Statement The if statement may itself contain another if statement is known as nested if statement Syntax: if (condition1) if (condition2) statement-1; else statement-2; else statement-3;

The if statement may be nested as deeply as you need to nest it. One block of code will only be executed if two conditions are true. Condition 1 is tested first and then condition 2 is tested. The second if condition is nested in the first. The second if condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement

Nested if Statement Example #include <stdio.h> main () int a,b,c,big; printf ("Enter three numbers"); scanf ("%d %d %d", &a, &b, &c); if (a > b) if (a > c) big = a ; else big = c ; else if (b > c) big = b; else big = c; printf ("Largest of %d, %d & %d = %d", a,b,c,big); er

The ELSE If Ladder When a series of many conditions have to be checked we may use the ladder else if statement which takes the following general form. Syntax if (condition1) statement 1; else if (condition2) statement2; else if (condition3) statement3; else if (condition) statement n; else default statement; statement-x;

This construct is known as if else construct or ladder. The conditions are evaluated from the top of the ladder to downwards. As soon on the true condition is found, the statement associated with it is executed and the control is transferred to the statement x (skipping the rest of the ladder). When all the condition becomes false, the final else containing the default statement will be executed.

Example program using If else ladder to grade the student according to the following rules.

#include <stdio.h> void main () int marks; printf ("Enter marks\n"); scanf ("%d", &marks); if (marks <= 100 && marks >= 70) printf("\n Distinction"); else if (marks >= 60) printf("\n First class"); else if (marks >= 50) printf ("\n second class"); else if (marks >= 35) printf ("\n pass class"); else printf ("Fail");

The Switch Statement Switch statement allows a program to select one statement for execution out of a set of alternatives. During the execution of the switch statement only one of the possible statements will be executed the remaining statements will be skipped. The switch statement removes these disadvantages by using a simple and straight forward approach. Syntax Switch (expression) Case case-label-1; Case case-label-2; Case case-label-n; Case default

When the switch statement is executed the control expression is evaluated first and the value is compared with the case label values in the given order. If the label matches with the value of the expression then the control is transferred directly to the group of statements which follow the label. If none of the statements matches then the statement against the default is executed. The default statement is optional in switch statement in case if any default statement is not given and if none of the condition matches then no action takes place in this case the control transfers to the next statement of the if else statement.

A program to stimulate the four arithmetic operations using switch #include <stdio.h> void main () int num1, num2, result; char operator; printf ("Enter two numbers"); scanf ("%d %d", &num1, &num2); printf ("Enter an operator"); scanf ("%c", &operator); ; switch (operator) case '+': result = num1 + num2; break;

case '-': result = num1 - num2; break case '*': result = num1 * num2; break; case '/': if (num2!= 0) result = num1 / num2; else printf ("warning : division by zero \n"); result = 0; break; default: printf ("\n unknown operator"); result = 0; break; printf ("%d", result);

The GOTO statement The goto statement is simple statement used to transfer the program control unconditionally from one statement to another statement. Although it might not be essential to use the goto statement in a highly structured language like C, there may be occasions when the use of goto is desirable. Syntax a> goto label; Label; Statement; b> label; goto label;

The goto requires a label in order to identify the place where the branch is to be made. A label is a valid variable name followed by a colon. The label is placed immediately before the statement where the control is to be transformed. A program may contain several goto statements that transferred to the same place when a program. The label must be unique.

A program to find the sum of n natural numbers using goto statement #include <stdio.h> main () int n, sum = 0, i = 0; printf ("Enter a number"); scanf ("%d", &n); loop: i++; sum += i; if (i < n) goto loop; printf ("\n sum of %d natural numbers = %d", n, sum);