DECISION MAKING STATEMENTS

Similar documents
Conditional Statement

Introduction to C Programming

Decision Making -Branching. Class Incharge: S. Sasirekha

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

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

Decision Making and Loops

Lecture 6. Statements

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

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

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Conditional Statement

Lecture 3. More About C

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

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

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

Use of scanf. scanf("%d", &number);

Chapter 4. Flow of Control

Lecture 5 Tao Wang 1

Programming for Engineers Iteration

Simple if Simple if-else Nested if - else Notes for if-else statement Logical operators

Module 4: Decision-making and forming loops

Flow Control. CSC215 Lecture

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

3 The L oop Control Structure

Chapter 4: Making Decisions

Information Science 1

Chapter 4: Making Decisions

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

Computer Science & Engineering 150A Problem Solving Using Computers

The New C Standard (Excerpted material)

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

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

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

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

Computers Programming Course 7. Iulian Năstac

5. Selection: If and Switch Controls

YOLOP Language Reference Manual

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 5: Control Structures

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

DECISION CONTROL AND LOOPING STATEMENTS

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

C: How to Program. Week /Mar/05

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

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

Lecture 8. Daily Puzzle

LECTURE 04 MAKING DECISIONS

CS1100 Introduction to Programming

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

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

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

Chapter 2 - Introduction to C Programming

Information Science 1

Lexical Considerations

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

Introduction. C provides two styles of flow control:

Prepared by: Shraddha Modi

Conditionals. CSE / ENGR 142 Programming I. Chapter 4. Conditional Execution. Conditional ("if ") Statement. Conditional Expressions

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

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

A control structure refers to the way in which the Programmer specifies the order of executing the statements

COMP 208 Computers in Engineering

Chapter 4 The If Then Statement

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

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

Computer Programming. Decision Making (2) Loops

Lexical Considerations

Unit 3 Decision making, Looping and Arrays

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

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

CSI33 Data Structures

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Review of the C Programming Language for Principles of Operating Systems

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

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

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

1.4 Control Structures: Selection. Department of CSE

Iteration. Side effects

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

1 Lexical Considerations

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

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

CS240: Programming in C

APSC 160 Review. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi Borrowing many questions from Ed Knorr

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

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

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

CHAD Language Reference Manual

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

2.8. Decision Making: Equality and Relational Operators

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

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

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

Introduction to C Language

Chapter 2 Control in Programming. lecture 2 1

C-LANGUAGE CURRICULAM

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

Chapter 3 Structured Program Development

Transcription:

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 if - else statement Nesting (i.e., embedding) if else statements switch statement

if statement Till now, we have written programs in which instructions were executed in order of their appearance. There will be many situations where such execution sequence may not be sufficient. For example, we want to find the bigger out of two numbers. This problem requires comparison of two numbers and based on comparison result, the bigger number will be found. This can be implemented using if instruction.

if statement if instruction carries out a logical test and then takes one of the two possible actions depending on the outcome of the test (i.e., whether the outcome is true orfalse). This instruction can be written in simplest form as: if (expr) statement» The expr must be placed in parenthesis, as shown. In this form, the statement will be executed only if the expr has a nonzero value (i.e., if expr is true).» If the expr has a value of zero (i.e., if the expr is false), then the statement will be ignored.

Code of if statement and Logic flow control : : If ( expression ) { statement } : : expression true false statement

if statement - Example Let us try to use if instruction to find out bigger out of two numbers. In the program, if the #include<stdio.h> void main(void) { int first, second; scanf( %d %d, &first, &second); if ( first > second ) printf ( First number is bigger\n ); } input value of first number is higher than second number, only then the printf statement will be executed.

if statement - Example Let us try to use if instruction to find out bigger out of two numbers. #include<stdio.h> void main(void) { int first, second; scanf( %d %d, &first, &second); if ( first > second ) printf ( First number is bigger\n ); } So if the inputs are given as: 20 10 We will see the output First number is bigger

if statement - Example Let us try to use if instruction to find out bigger out of two numbers. #include<stdio.h> void main(void) { int first, second; scanf( %d %d, &first, &second); if ( first > second ) printf ( First number is bigger\n ); } On the other hand, if we give input values as 10 20 No output message will appear on the screen.

if statement - Example Let us try to use if instruction to find out bigger out of two numbers. So in order to get proper output message for all combinations of inputs, the program can be extended as: #include<stdio.h> void main(void) { int first, second; scanf( %d %d, &first, &second); if ( first > second ) printf ( First number is bigger\n ); if (second > first) printf ( Second number is bigger\n ); if (first == second) } printf ( Both numbers are equal\n );

if else statement Many times the program requires to take a specific action if a particular condition is true and another action if the condition is not true (i.e., false). Such a situation can be effectively implemented using if else instruction.

if else statement The general form of an if instruction which includes the else clause is: if (expr) statement1 else statement2» If the expr has a nonzero value (i.e., if expr is true), then statement1 will be executed otherwise (i.e., if the expr is false), then the statement2 will be executed.

Code of if else statement and Logic flow control : If ( expression ) { statement -1 } else { statement -2 } : false expression Statement-2 true Statement-1

if else statement - Example Let us write a simple program using if-else instruction to test whether given two numbers are equal ornot. #include<stdio.h> void main(void) { int first, second; scanf( %d %d, &first, &second); if ( first == second ) printf ( Both numbers are equal\n ); else printf ( Numbers are not equal\n ); } In the program, if the input value of first and second number is equal, then the statement Both numbers are equal will be displayed.

Nesting if else statements It is possible to nest (i.e., embed) if - else statements, one within another. There are several different forms that nested if - else statements can take. The most general form of two-layer nesting is: if e1 else if e2 s1 else s2 if e3 s3 else e4 where e1, e2 and e3 represent logical expressions and s1, s2, s3 and s4 represent statements.

Nesting if else statements Now, one complete if - else statement will be executed if e1 is nonzero (true), and another complete if - else statement will be executed if e1 is zero (false). It is, of course, possible that s1, s2, s3 and s4 will contain other if - else statements. We would then have multilayer nesting. if e1 else if e2 s1 else s2 if e3 s3 else e4

Nesting if else statements - SEVERAL FORMS if e1 s1 else if e2 s2 if e1 s1 else if e2 s2 else e3 if e1 if e2 s1 else s2 else s3 if e1 if e2 s1 else s2 In the first three cases the association between the else clauses and their corresponding expressions is straightforward. In the last case, however it is not clear which expression (e1 or e2) is associated with the else clause. The answer is e2. The rule is that the else clause is always associated with the closest preceding unmatched ( i.e., else -less) if. This is suggested by the indentation, though indentation itself is not the deciding factor.

switch statement The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based upon the current value of an expression which is included within the switch statement. The general form of the switch statement is switch ( expression ) statement

switch statement The general form of the switch statement is switch ( expression ) statement where expression results in an integer value. Note that expression may also be of type char, since individual characters have equivalent integer values. the embedded statement is generally a compound statement that specifies alternate courses of action. Each alternative is expressed as a group ofone or more individual statements within the overall embedded statement.

switch statement The general form of the switch statement is switch ( expression ) statement for each alternative, the first statement within the group must be preceded by one or more case labels ( also called case prefixes). the case labels identify the different groups of statements (i.e., the different alternatives) and distinguish then from one another. the case labels must therefore be unique within a given switch statement.

switch statement In general terms, each group of statements is written as: case expression : statement 1 statement 2........... statement n or, when multiple case labels are required, where expression 1, expression 2,., expression m represent constant, integer-valued expressions. case expression 1 : case expression 2 :........... case expression m : statement 1 statement 2.......... statement n

Code of switch case statement : switch ( expression ) { case val - 1: case val - 2: case val - 3: statement -1; break; statement -2; break; statement -3; break; : : : : case val - n: statement -n; break; default: statement -d; }

switch statement - Code Explanation If expression takes any value from val-1, val-2, val-3,...., val-n, the control is transferred to that appropriate case. In each case, the statements are executed and then break statement transfers the control out of switch statement. If no break statement is used following a case, except the last one in the absence of default keyword, the control will fall through to the next case.

switch statement - Code Explanation If the value of the expression does not match any of the case values, control goes to the default keyword, which is usually at the end of the switch statement. Theuse of the default keyword can be of a great convenience. If there is no default keyword, the whole switch statement simply terminates when there is no match.

Logic flow control of switch case statement- SIMPLIFIED VIEW = val-1 expression default = val-2 = val-3 = val-n... Statement-1 Statement-2 Statement-3 Statement-n Statement-d...

switch case statement - Example A simple switch statement is illustrated below. In this example, choice is assumed to be a char-type variable. switch ( choice ) { } case R : case W : default: printf( Red ); break; printf( White ); break; printf( Error ); Thus, Red will be displayed if choice represents R, White will be displayed if choice represents W. Default group will generate an Error message if none of the case labels matches the original expression.

switch case statement - Example A simple switch statement is illustrated below. In this example, choice is assumed to be a char-type variable. switch ( choice ) { } case R : case W : default: printf( Red ); break; printf( White ); break; printf( Error ); Note that each of first two groups ends with the break statement. The break statement causes control to be transferred out of the switch statement, thus preventing more than one group of statements from being executed.

switch case statement - Example A simple switch statement is illustrated below. In this example, choice is assumed to be a char-type variable. switch ( choice ) { } case R : case W : default: printf( Red ); break; printf( White ); break; printf( Error ); One of the labeled groups of statements within the switch statement may be labeled default. This group will be selected if none of the case labels matches the value of the expression.

switch case statement - Example A simple switch statement is illustrated below. In this example, choice is assumed to be a char-type variable. switch ( choice ) { } case R : case W : default: printf( Red ); break; printf( White ); break; printf( Error ); The default group may appear anywhere within the switch statement - it need not necessarily be placed at the end. If none of the case labels matches the value of the expression and the default group is not present, then no action will be taken by the switch statement.

Practice Questions Determine the final value of x in the following cases: