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

Size: px
Start display at page:

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

Transcription

1 Control Statements Introduction: - Any C program if you consider, the set of statements are normally executed sequentially in the order in which they are written, and such programs have sequential structure Sometimes, the order will be changed because of certain conditions Those programs where in you have decision making, is said to have selection structure C supports some statements, which helps to alter the flow of control, so that control can be transferred from one part of the program to another The control/decision structure available in C are: - o If statement o Switch statement o Conditional operator statement o Goto statement Decision making with if Statement : - It is one of the most powerful statements of any programming language It is used to carry out a logical test of the condition and take one of the two possible ie, TRUE/FALSE Syntax: - Eduoncloudcom Page 1

2 if(testexpression) statement1; statement2; Flowchart: - Entry Test Expression? False True Eg: - Two-way branching Eg: - 1 if(balance is zero) No more shopping 2 if(code is M) Person is Male 3 if(rain==1) Wear a raincoat The if statement may be implemented in different forms depending on the complexity of conditions to be tested Eduoncloudcom Page 2

3 The different forms are: - o Simple if statement o If statement o Nested if statement o Else-if ladder Simple if statement: - Flowchart: - General form: - If(test expression) Statement-block; Statement-x; Entry Test Expression? True False St_block St_x Next Statement Eduoncloudcom Page 3

4 The syntax and flowchart of simple-if is as shown above In the syntax, the test expression is the condition to be checked Statement-block may be a single statement or a group of statements If the test expression is true, the statement-block will be executed, otherwise the statement block will be skipped and the execution will jump to the statement-x Note: - when the condition is true, both the statement-block and the statement-x are executed in sequence Examples: 1 if(category == professor) salary = salary + increment; printf( %f,salary); if(a>0 && b<10) Sum = a + b; Result = Sum; printf ( %d,result); 3 if(syllabus == central percentage = percentage + addbonus; printf( percentage = %f,percentage); if statement: - Eduoncloudcom Page 4

5 General Syntax: - if(test expression) true_block statement(s) false_block statement(s) Statements-x; Flowchart: - True Test expression? Entry True-block False-block Statement-X Eduoncloudcom Page 5

6 If the test expression is true, the true-block statements following the if statements are executed, if the test expression is false, the false-block statements are executed In either case, either true-block or false-block will be executed, and not both Note: in both the cases, the control is transferred to the statement-x Examples: - if(code == 1) boy = boy + 1; girl = girl + 1; printf( Count = %d,count); if(code == 1) boy = boy + 1; girl = girl + 1; printf( count=%d,count); OR Nested if- Statement: - When a series of decision are involved, we may have to use more than one if statement in nested form, as shown below: - General Form: - Eduoncloudcom Page 6

7 if (testcondition-1) if (testcondition-2) Statement-1; Statement-2; Statement-3; Statement-x; Flowchart: - entry False Test condition1? True Statement-3 False Test condition2? True Statement-2 Statement-1 Eduoncloudcom Page 7

8 Statement-x Next statement Explanation: - If the condition1 is false, the statement-3 will be executed, if the condition-1 is true, it will continue checking for the condition-2, if it is true, the statement-1 will be evaluated the statement-2 will be evaluated and then the control is transferred to statement-x Example: - /* Program to find the largest of 3 numbers */ #include<stdioh> void main() Int a, b, c, large; printf( enter 3 numbers : ); scanf( %d %d %d,&a, &b, &c); if(a>b) if(a>c) large = a; large = c; if(b>c) large = b; large = c; printf( Largest no is %d,large); Eduoncloudcom Page 8

9 Else-if ladder: - Another way of describing the nested if- is the -if ladder, where, every is associated with an if-statement General Form: if(condition-1) statement-1; if(condition-2) statement-2; if(condition-3) statement-3; if(condition-n) statement-n; default-statement; statement-x; Flowchart: - Entry True Cond n -1? False Statement-1 True Cond n -2? False Eduoncloudcom Page 9

10 Statement-2 True Cond n -3? False Statement-3 True Cond n -4? False Statement-n Default statement Statement-X Next Statement Explanation: - Condition-1, condition-2, condition-3 condition-n are the expressions/conditions and statement-1, statement-2 statement-n are statements In this construct, the conditions are checked, starting from the top of the -if ladder, moving downwards Firstly, condition-1 is checked, if true, statement-1 is executed and control is transferred to statement-x Eduoncloudcom Page 10

11 If condition-1 is false, condition-2 is checked and if true, statement-2 is executed and control is transferred to statement-x skipping the rest of the statements When all the conditions are false, ie, condition-1, condition- 2 condition-n, the default-statement is executed followed by the execution of statement-x Examples: - 1 /* program to find the sign of a number */ #include<stdioh> void main() int num, sign; printf( enter input number : ); scanf( %d,&num); if(num<0) //check number is negative sign = -1; if(num = = 0) sign = 0; sign = 1; printf( Sign = %d,sign); 2 /* Program to print the grade of the student using -if ladder*/ void main() int t1, t2, final, marks; char grade; printf( Enter marks in t1, t2, final ); scanf( %d%d%d,&t1,&t2,&final); if(t1>t2) Eduoncloudcom Page 11

12 marks = final + t1; marks = final + t2; if( marks > = 0 && marks < 40) grade = E ; if(marks < 50) grade = D ; if(marks < 60) grade = C ; if(marks < 75) grade = B ; if(marks < = 100) grade = A ; grade = X ; if(grade!= X ) printf( Grade is %c, grade); printf( Invalid Input\n ); 3 /* Program using if- statement to calculate absolute value of an integer*/ #include<stdioh> void main() int num; printf( Type a number: ); scanf( %d,&num); if(num < 0) num = -num; // converting ve number to +ve number printf( The absolute value is %d,num); Eduoncloudcom Page 12

13 printf( The absolute value is %d,num); 4 /* Program to show usage of char in if() statement*/ void main() char wish; printf( Want to go for shopping Y/N? ); wish=getchar(); if(wish == Y wish = = y ) printf( Get ready ); printf( Start Studying ); Decision making and branching Switch statement 1 C supports a multi-way decision statement known as switch 2 It tests the value of a given variable / expression against a list of case values and when the match is found, a block of statements associated with that case is executed 3 When we need to select one block of statements out of two alternatives we use if- structure But, whenever the flow of control must be directed to one of the several possible statements, we use switch 4 A chain of if- can be used in this case, but it becomes cumbersome when the no of if- levels exceeds more that 4 or 5 At that time, the switch-statement is more appropriate 5 C provides switch structure, an alternative to -if ladder which simplifies the code and improves read ability When we need to Eduoncloudcom Page 13

14 implement problems which involves selection of one out of many alternatives Syntax/General Form Of Switch 6 Switch statement is a multi-way decision statement and the general form is as follows: - switch(expression) case value-1: block-1; case value-2: case value-3: default : Statement-X; block-2 block-3; default-block; 7 Switch: Switch is a keyword which checks the value of the variable or expression given within parenthesis, with a list of case values value-1, value-2 value-n, known as case labels When a match is found, a block of statements associated with that case is executed 8 Variable or expression: - Eduoncloudcom Page 14

15 Variable / expression is an integer/ character expression which evaluates to either an integer or a character 9 case value-1: are known as case labels case value-2: Each case value should be unique within a switch statement for execution Case labels should not be float values or Boolean expression Each case label should have colon(:) All case labels should be listed in curly braces Break should be there after each case 10 Default: - default is optional If default is absent, the control goes to the statement-x, if no match is found If none of the case values is matched, then the default statement present at the end of the switch statement will be executed 11 Break: - The last statement of every case including the default is necessary Break statement is used to transfer the control to the first statement outside the switch ie, statement-x If break is absent, control flows to the next case One advantage of not using break is: - When multiple values of variables say value-1, value-2, and value-3 must execute the same set of statements ie, for eg: - switch(choice) Eduoncloudcom Page 15

16 case 1: case 2: case 3: printf( values 1,2 and 3 ); Note: Float values are not allowed as case labels 12 Valid switch statements: - Switch(i+j) where I and j are integral values Case 4: Case a : Case a + b : case value is = 195 Case 1: Case 3: Case 5: printf( odd values 1,3 and 5 ); Invalid statements:- Switch(i+24) case 74: case 7: space should be given after the keyword case Case 5: uppercase not allowed 13 Nested switch statement can also be written where, a switch statement would be part of a block of statements for a particular case value On the lines of if-statement, and its variations, even switch statements can also be nested if required For example: (nested switch) Suppose there are 2 departments namely, sales and production If the numeric code of sales is 1 and production is 2 Eduoncloudcom Page 16

17 And if both the departments have employees with two designations: manager and Assistant Manager and again if the numeric code for manager is 1 and that of assistant manager is 2, ie, Department Code Designation code Sales 1 Manager 1 Production 2 Assistant manager 2 The program is to accept the department code and designation as inputs and display the corresponding department name and designation name #include<stdioh> void main() int dept,desig; printf( Enter department code: \n ); scanf( %d,&dept); printf( Enter designation : \n ); scanf( %d,&design); switch(dept) case 1: switch(desig) case 1: printf( manager in sales department ); case 2: printf( Assistant Manager in sales dept ); Break; case 2: switch(desig) case 1: printf( Manager in production Department ); Eduoncloudcom Page 17

18 Output: - Enter department code: 1 Enter designation code: 2 Assistant Manager in Sales Department Examples for Switch: - 1 Program to accept a digit and display it in word void main() int digit; printf( Enter the digit : ); scanf( %d,&digit); switch(digit) case 0: printf( Zero ); case 1: printf( ONE ); case 2: printf( Two ); case 2: printf( Assistant manager in production department ); Eduoncloudcom Page 18

19 case 9 :printf( Nine ); default: printf( not a digit ); Output : - Enter a digit : 4 Four 2 Program to accept a month number and display the maximum number of days in the month void main() int month, days; first: printf( Enter month numbers : ); scanf( %d,&month); switch(month) case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; case 4: case 6: case 9: Eduoncloudcom Page 19

20 case 11: days = 30; case 2: days = 28; default : printf( invalid entry ); goto first; printf( Maximum number of days in month %d = %d,month, days); Conditional Operator: - It is used for two-way decision making It s a combination of? and : and it needs 3 operands General form: - Conditional expression? expression-1: expression-2; Its equivalent to if- statement: - ie, if(conditional expression) expression-1; expression-2; Nested conditional operator is also possible, where expression-1 may be another conditional operator Flowchart: - Test expression? False True Eduoncloudcom Page 20

21 Expression-1 Expression-2 Program to read the value of X and evaluate the following functions: - y=x + 15 for x > 0 y = 192x + 15 for x = 0 y = 2x + 10 for x < 0 /* Program to show usage of?: operator */ #include<stdioh> void main() float x, y; printf( Enter the value of x:\n ); scanf( %f,&x); y = (x>0)? x + 15: ((x< 0)?2*x+10: 192 * x + 5); printf( When x = %72f, y=%72f,x,y); Output: - Enter the value of x: 3 When x = 300, y = 1800 Enter the value of x: -5 When x = -50, y= 000 Enter the value of x: Exit Eduoncloudcom Page 21

22 0 When x = 0, y = 500 Goto Statement: - Goto statement transfers control unconditionally to another part of the program which is marked by a label The general goto form: - label; Where, label identifies the statement to which the control must be transferred A label is a valid variable name It follows the same rule as identifiers ie, they are built from alphabets, digits and underscore, where the first character must be an alphabet A label name should have a colon (:) The general form of goto and label are : - Goto label; label: statement - 2; Label: statement 1; goto label; Forward Jump Backward Jump Each label in the program should be unique No two statements can have the same labels Eduoncloudcom Page 22

23 Examples: - Sometimes goto statements result in endless loops known as infinite loops, due to the unconditional transfer of control Goto can be used to transfer control out of a loop or nested loops depending on some condition 1 Program to show usage of goto function//backward Jump #include<stdioh> void main() float r, circum; first: printf( enter radius: ); scanf( %f,&r); if(r! = 0) circum = 2 * 314 * r; printf( Circumference = %73f,circum); goto first; 2 Program to show usage of goto function //FORWARD JUMP #include<stdioh> void main() int a,b,big; printf( Enter a and b : \n ); scanf( %d%d,&a, &b); if(a > b) big = a; Eduoncloudcom Page 23

24 goto finish; if(a < = b) big = b; goto finish; finish : printf( Biggest of %d and %d is %d,a, b, big); Output: - Enter a and b: Biggest of 14 and 38 is 38 1 Following are the numeric codes assigned to different colors Write a program to accept a color code and display the appropriate color in word Color code 1 Red 2 Blue 3 Green 4 Yellow 5 Purple Color #include<stdioh> void main() int code; printf( Enter the code: ); scanf( %d,&code); Eduoncloudcom Page 24

25 switch(code) case 1: printf( Entered code is for Red\n ); case 2: printf( Entered code is for blue\n ); case 3: printf( Entered code is for Green\n ); case 4: printf( Entered code is for Yellow\n ); case 5: printf( Entered code is for Purple\n ); default : printf( NO color assigned \n ); Output: - Enter the code : 3 Entered code is for Green 2 Find the sum of first n natural numbers using if and goto statements: - #include<stdioh> void main() int n, i, sum; printf( enter the number : ); scanf( %d,&n); sum=0; i=1; abc : sum = sum + i; i++; if(i <= n) Eduoncloudcom Page 25

26 goto abc; printf( Sum = %d,sum); Eduoncloudcom Page 26

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha 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

More information

SELECTION STATEMENTS:

SELECTION STATEMENTS: UNIT-2 STATEMENTS A statement is a part of your program that can be executed. That is, a statement specifies an action. Statements generally contain expressions and end with a semicolon. Statements that

More information

MODULE 2: Branching and Looping

MODULE 2: Branching and Looping MODULE 2: Branching and Looping I. Statements in C are of following types: 1. Simple statements: Statements that ends with semicolon 2. Compound statements: are also called as block. Statements written

More information

DECISION CONTROL AND LOOPING STATEMENTS

DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL AND LOOPING STATEMENTS DECISION CONTROL STATEMENTS Decision control statements are used to alter the flow of a sequence of instructions. These statements help to jump from one part of

More information

Module 4: Decision-making and forming loops

Module 4: Decision-making and forming loops 1 Module 4: Decision-making and forming loops 1. Introduction 2. Decision making 2.1. Simple if statement 2.2. The if else Statement 2.3. Nested if Statement 3. The switch case 4. Forming loops 4.1. The

More information

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

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions. Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate

More information

Decision Making and Branching

Decision Making and Branching INTRODUCTION Decision Making and Branching Unit 4 In the previous lessons we have learned about the programming structure, data types, declaration of variables, tokens, constants, keywords and operators

More information

Introduction. C provides two styles of flow control:

Introduction. C provides two styles of flow control: Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if

More information

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful

More information

& 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.

& 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. statement- Computer Programming and Utilization (CPU) 110003 D) Decision Control Structure (if, if, if e if, switch, ) 1 Explain if with example and draw flowchart. if is used to control the flow of execution

More information

Unit 3 Decision making, Looping and Arrays

Unit 3 Decision making, Looping and Arrays Unit 3 Decision making, Looping and Arrays Decision Making During programming, we have a number of situations where we may have to change the order of execution of statements based on certain conditions.

More information

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

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action. 3.0 Control Statements in C Statements The statements of a C program control the flow of program execution. A statement is a command given to the computer that instructs the computer to take a specific

More information

Computer Programming. Decision Making (2) Loops

Computer Programming. Decision Making (2) Loops Computer Programming Decision Making (2) Loops Topics The Conditional Execution of C Statements (review) Making a Decision (review) If Statement (review) Switch-case Repeating Statements while loop Examples

More information

C Programming Decision Making

C Programming Decision Making 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

More information

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs PDS Lab Section 16 Autumn-2017 Tutorial 3 C Programming Constructs This flowchart shows how to find the roots of a Quadratic equation Ax 2 +Bx+C = 0 Start Input A,B,C x B 2 4AC False x If 0 True B x 2A

More information

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities INTERNAL ASSESSMENT TEST 1 SOLUTION PART 1 1 a Define algorithm. Write an algorithm to find sum and average of three numbers. 4 An Algorithm is a step by step procedure to solve a given problem in finite

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

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

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION INTRODUCTION Decision Making and Looping Unit 5 In the previous lessons we have learned about the programming structure, decision making procedure, how to write statements, as well as different types of

More information

Computers Programming Course 7. Iulian Năstac

Computers Programming Course 7. Iulian Năstac Computers Programming Course 7 Iulian Năstac Recap from previous course Operators in C Programming languages typically support a set of operators, which differ in the calling of syntax and/or the argument

More information

4.1. Structured program development Overview of C language

4.1. Structured program development Overview of C language 4.1. Structured program development 4.2. Data types 4.3. Operators 4.4. Expressions 4.5. Control flow 4.6. Arrays and Pointers 4.7. Functions 4.8. Input output statements 4.9. storage classes. UNIT IV

More information

Subject: Fundamental of Computer Programming 2068

Subject: Fundamental of Computer Programming 2068 Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

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

CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart Q-2) Explain basic structure of c language Documentation section : CP FAQS Q-1) Define flowchart and explain Various symbols of flowchart ANS. Flowchart:- A diagrametic reperesentation of program is known as flowchart Symbols Q-2) Explain basic structure of c language

More information

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

9/9/2017. If ( condition ) { statements ; ; C has three major decision making instruction, the if statement, the if- statement, & the switch statement. A fourth somewhat less important structure is the one which uses conditional operators In the

More information

BRANCHING if-else statements

BRANCHING if-else statements BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional

More information

C - Basic Introduction

C - Basic Introduction C - Basic Introduction C is a general-purpose high level language that was originally developed by Dennis Ritchie for the UNIX operating system. It was first implemented on the Digital Equipment Corporation

More information

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Flow Chart. The diagrammatic representation shows a solution to a given problem. low Charts low Chart A flowchart is a type of diagram that represents an algorithm or process, showing the steps as various symbols, and their order by connecting them with arrows. he diagrammatic representation

More information

C Programming Class I

C Programming Class I C Programming Class I Generation of C Language Introduction to C 1. In 1967, Martin Richards developed a language called BCPL (Basic Combined Programming Language) 2. In 1970, Ken Thompson created a language

More information

Introduction to C Programming

Introduction to C Programming 1 2 Introduction to C Programming 2.6 Decision Making: Equality and Relational Operators 2 Executable statements Perform actions (calculations, input/output of data) Perform decisions - May want to print

More information

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

Assignment: 1. (Unit-1 Flowchart and Algorithm) Assignment: 1 (Unit-1 Flowchart and Algorithm) 1. Explain: Flowchart with its symbols. 2. Explain: Types of flowchart with example. 3. Explain: Algorithm with example. 4. Draw a flowchart to find the area

More information

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

Chapter 2. Introduction to C language. Together Towards A Green Environment Chapter 2 Introduction to C language Aim Aim of this chapter is to provide the fundamentals for basic program construction using C. Objectives To understand the basic information such as keywords, character

More information

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Programming and Data Structure Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Shortcuts in Assignment Statements A+=C A=A+C A-=B A=A-B A*=D A=A*D A/=E

More information

UIC. C Programming Primer. Bharathidasan University

UIC. C Programming Primer. Bharathidasan University C Programming Primer UIC C Programming Primer Bharathidasan University Contents Getting Started 02 Basic Concepts. 02 Variables, Data types and Constants...03 Control Statements and Loops 05 Expressions

More information

Subject: PIC Chapter 2.

Subject: PIC Chapter 2. 02 Decision making 2.1 Decision making and branching if statement (if, if-, -if ladder, nested if-) Switch case statement, break statement. (14M) 2.2 Decision making and looping while, do, do-while statements

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

More information

IV Unit Second Part STRUCTURES

IV Unit Second Part STRUCTURES STRUCTURES IV Unit Second Part Structure is a very useful derived data type supported in c that allows grouping one or more variables of different data types with a single name. The general syntax of structure

More information

Decision Making and Loops

Decision Making and Loops Decision Making and Loops Goals of this section Continue looking at decision structures - switch control structures -if-else-if control structures Introduce looping -while loop -do-while loop -simple for

More information

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

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities

More information

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

COP 2000 Introduction to Computer Programming Mid-Term Exam Review he exam format will be different from the online quizzes. It will be written on the test paper with questions similar to those shown on the following pages. he exam will be closed book, but students can

More information

Fundamentals of Computer Programming Using C

Fundamentals of Computer Programming Using C CHARUTAR VIDYA MANDAL S SEMCOM Vallabh Vidyanagar Faculty Name: Ami D. Trivedi Class: FYBCA Subject: US01CBCA01 (Fundamentals of Computer Programming Using C) *UNIT 3 (Structured Programming, Library Functions

More information

5. Selection: If and Switch Controls

5. Selection: If and Switch Controls Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation

More information

Chapter 5: Control Structures

Chapter 5: Control Structures Chapter 5: Control Structures In this chapter you will learn about: Sequential structure Selection structure if if else switch Repetition Structure while do while for Continue and break statements S1 2017/18

More information

Chapter 5: Control Structures

Chapter 5: Control Structures Chapter 5: Control Structures What we will learn: Selection structures Loops What you need to know before: Data types Functions For loop While loop If selection If else structures Control structures are

More information

DECISION MAKING STATEMENTS

DECISION MAKING STATEMENTS DECISION MAKING STATEMENTS If, else if, switch case These statements allow the execution of selective statements based on certain decision criteria. C language provides the following statements: if statement

More information

Decision Making. if Statement. Decision Making

Decision Making. if Statement. Decision Making Decision Making Decision is a word which is normally taken in a moment where one is in a position to select one option from the available options which are obviously more than one. While writing programs

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

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

Following is the general form of a typical decision making structure found in most of the programming languages: Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined

More information

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

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps 1 True and False in C False is represented by any zero value The int expression having the

More information

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

BCA-105 C Language What is C? History of C C Language What is C? C is a programming language developed at AT & T s Bell Laboratories of USA in 1972. It was designed and written by a man named Dennis Ritchie. C seems so popular is because it is

More information

Control Structure: Loop

Control Structure: Loop Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop control structure 1 Loop Structure Condition is tested first

More information

Control Statements. If Statement if statement tests a particular condition

Control Statements. If Statement if statement tests a particular condition Control Statements Control Statements Define the way of flow in which the program statements should take place. Implement decisions and repetitions. There are four types of controls in C: Bi-directional

More information

1.4 Control Structures: Selection. Department of CSE

1.4 Control Structures: Selection. Department of CSE 1.4 Control Structures: Selection 1 Department of CSE Objectives To understand how decisions are made in a computer To understand the relational operators To understand the logical operators and,or and

More information

Lecture 6. Statements

Lecture 6. Statements Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions

More information

C library = Header files + Reserved words + main method

C library = Header files + Reserved words + main method DAY 1: What are Libraries and Header files in C. Suppose you need to see an Atlas of a country in your college. What do you need to do? You will first go to the Library of your college and then to the

More information

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE Introduction to C programming By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE Classification of Software Computer Software System Software Application Software Growth of Programming Languages History

More information

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities SOLUTION OF CONTINUOUS INTERNAL EVALUATION TEST -1 Date : 27-02 2018 Marks:60 Subject & Code : Programming in C and Data Structures- 17PCD23 Name of faculty : Dr. J Surya Prasad/Mr. Naushad Basha Saudagar

More information

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

M1-R4: Programing and Problem Solving using C (JAN 2019) M1-R4: Programing and Problem Solving using C (JAN 2019) Max Marks: 100 M1-R4-07-18 DURATION: 03 Hrs 1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter

More information

CHAPTER 9 FLOW OF CONTROL

CHAPTER 9 FLOW OF CONTROL CHAPTER 9 FLOW OF CONTROL FLOW CONTROL In a program statement may be executed sequentially, selectively or iteratively. Every program language provides constructs to support sequence, selection or iteration.

More information

Problem Solving and 'C' Programming

Problem Solving and 'C' Programming Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 05: Selection and Control Structures 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained herein

More information

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

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS 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:

More information

Control Structure: Selection

Control Structure: Selection Control Structure: Selection Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure Selection Structure Single

More information

Conditional Statement

Conditional Statement Conditional Statement 1 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition Also called Branching How do we specify conditions?

More information

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

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva All copyrights reserved - KV NAD, Aluva Dinesh Kumar Ram PGT(CS) KV NAD Aluva Overview Looping Introduction While loops Syntax Examples Points to Observe Infinite Loops Examples using while loops do..

More information

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

n Group of statements that are executed repeatedly while some condition remains true Looping 1 Loops n Group of statements that are executed repeatedly while some condition remains true n Each execution of the group of statements is called an iteration of the loop 2 Example counter 1,

More information

CHAPTER 5 FLOW OF CONTROL

CHAPTER 5 FLOW OF CONTROL CHAPTER 5 FLOW OF CONTROL PROGRAMMING CONSTRUCTS - In a program, statements may be executed sequentially, selectively or iteratively. - Every programming language provides constructs to support sequence,

More information

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester Programming Language Control Structures: Selection (switch) Eng. Anis Nazer First Semester 2018-2019 Multiple selection choose one of two things if/else choose one from many things multiple selection using

More information

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

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4 Instructors: Aykut Erdem, Erkut Erdem, Fuat Akal TAs: Ahmet Selman Bozkir, Gultekin Isik, Yasin Sahin 1 Today Condi/onal Branching Logical Expressions

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

Score score < score < score < 65 Score < 50

Score score < score < score < 65 Score < 50 What if we need to write a code segment to assign letter grades based on exam scores according to the following rules. Write this using if-only. How to use if-else correctly in this example? score Score

More information

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

Technical Questions. Q 1) What are the key features in C programming language? Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility

More information

C++ PROGRAMMING SKILLS Part 2 Programming Structures

C++ PROGRAMMING SKILLS Part 2 Programming Structures C++ PROGRAMMING SKILLS Part 2 Programming Structures If structure While structure Do While structure Comments, Increment & Decrement operators For statement Break & Continue statements Switch structure

More information

Dr. R. Z. Khan, Associate Professor, Department of Computer Science

Dr. R. Z. Khan, Associate Professor, Department of Computer Science ALIGARH MUSLIM UNIVERSITY Department of Computer Science Course: CSM-102: Programming & Problem Solving Using C Academic Session 2015-2016 UNIT-2: Handout-3 Topic: Control Structures (Selection & Repetition)

More information

Dept. of CSE, IIT KGP

Dept. of CSE, IIT KGP Control Flow: Looping CS10001: Programming & Data Structures Pallab Dasgupta Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur Types of Repeated Execution Loop: Group of

More information

CS1100 Introduction to Programming

CS1100 Introduction to Programming Decisions with Variables CS1100 Introduction to Programming Selection Statements Madhu Mutyam Department of Computer Science and Engineering Indian Institute of Technology Madras Course Material SD, SB,

More information

COMPONENTS OF A COMPUTER

COMPONENTS OF A COMPUTER LECTURE 1 COMPONENTS OF A COMPUTER Computer: Definition A computer is a machine that can be programmed to manipulate symbols. Its principal characteristics are: It responds to a specific set of instructions

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

Control Structures in Java if-else and switch

Control Structures in Java if-else and switch Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification

More information

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

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 18 Switch Statement (Contd.) And Introduction to

More information

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003 Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an

More information

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value. 3. EXPRESSIONS It is a sequence of operands and operators that reduce to a single value. Operator : It is a symbolic token that represents an action to be taken. Ex: * is an multiplication operator. Operand:

More information

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

Questions Bank. 14) State any four advantages of using flow-chart Questions Bank Sub:PIC(22228) Course Code:-EJ-2I ----------------------------------------------------------------------------------------------- Chapter:-1 (Overview of C Programming)(10 Marks) 1) State

More information

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition) Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.

More information

CHAPTER 4 CONTROL STRUCTURES

CHAPTER 4 CONTROL STRUCTURES CHAPTER 4 CONTROL STRUCTURES 1 Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may deviate, repeat code or take decisions. For that purpose,

More information

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

These are reserved words of the C language. For example int, float, if, else, for, while etc. Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.

More information

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

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++ CHAPTER 9 C++ 1. WRITE ABOUT THE BINARY OPERATORS USED IN C++? ARITHMETIC OPERATORS: Arithmetic operators perform simple arithmetic operations like addition, subtraction, multiplication, division etc.,

More information

REPETITION CONTROL STRUCTURE LOGO

REPETITION CONTROL STRUCTURE LOGO CSC 128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING REPETITION CONTROL STRUCTURE 1 Contents 1 Introduction 2 for loop 3 while loop 4 do while loop 2 Introduction It is used when a statement or a block of

More information

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

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be

More information

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

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from INTRODUCTION TO C++ PROGRAM CONTROL Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Repetition Statement for while do.. while break and continue

More information

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS) FACULTY: Ms. Saritha P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS) SUBJECT / CODE: Programming in C and Data Structures- 15PCD13 What is token?

More information

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

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 Name :. Roll No. :..... Invigilator s Signature :.. 2011 INTRODUCTION TO PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give

More information

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University ITC213: STRUCTURED PROGRAMMING Bhaskar Shrestha National College of Computer Studies Tribhuvan University Lecture 08: Control Statements Readings: Chapter 6 Control Statements and Their Types A control

More information

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

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style

More information

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

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan 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

More information

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C Sample Test Paper-I Marks : 25 Time:1 Hrs. Q1. Attempt any THREE 09 Marks a) State four relational operators with meaning. b) State the use of break statement. c) What is constant? Give any two examples.

More information

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade; Control Statements Control Statements All programs could be written in terms of only one of three control structures: Sequence Structure Selection Structure Repetition Structure Sequence structure The

More information

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

Comments. Comments: /* This is a comment */ Flow Control Comments Comments: /* This is a comment */ Use them! Comments should explain: special cases the use of functions (parameters, return values, purpose) special tricks or things that are not

More information

Loops / Repetition Statements

Loops / Repetition Statements Loops / Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops C has three kinds of repetition statements: the while loop the for

More information

Tribhuvan University Institute of Science and Technology 2065

Tribhuvan University Institute of Science and Technology 2065 1CSc.102-2065 2065 Candidates are required to give their answers in their own words as for as practicable. 1. Draw the flow chart for finding largest of three numbers and write an algorithm and explain

More information