IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT 350142 - Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
INTRODUCTION TO C PROGRAMMING LANGUAGE Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Conditional Statement
Fundamental Programming Fundamental Programming More on Selection.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Flow of Control Part 1: Selection
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Previously Repetition Structures While, Do-While, For.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
Week 4 Program Control Structure
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CISC105 – General Computer Science Class 4 – 06/14/2006.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Decision making If.. else statement.
Decisions Chapter 4.
Chapter 4: Making Decisions.
Chapter 4 (Conditional Statements)
Chapter 4: Making Decisions.
Iteration statement while do-while
Control Statement Examples
SELECTION STATEMENTS (2)
Structured Program
The switch Statement Topics Multiple Selection switch Statement
Decision making If statement.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 3: Selection Structures: Making Decisions
Iteration Statement for
Presentation transcript:

IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

Review: If and If-Else Statement if (condition) statement; OR if (condition) { statement-1; … statement-n; } Do statement(s) only condition is TRUE, otherwise skip them if (condition) { statement-1; … statement-n; } else { statement-1; … statement-n; } Do statements in if block when condition is TRUE, otherwise do statements in else block

Grading Program  Let’s say we want to implement a program that take a score as input and the output of program is grade  For score  0 – 50  Grade = ‘F’   Grade = ‘D’   Grade = ‘C’   Grade = ‘B’   Grade = ‘A’  Just ignore numbers that are not in range of for now.

Step-by-Step (1) #include int main(int argc, char **argv) { int score; printf(“Enter your score : ”); scanf(“%d”, &score); } Declare a variable score as an integer. Display some message to let user understand what to input in (printf) Take input from user and store it in the score variable. (scanf)

Step-by-Step (2) #include int main(int argc, char **argv) { int score; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 50) printf(“Grade = F”); } First Condition: IF score is greater than or equals to 0 AND score is less than or equals to 50 THEN GRADE is F

Step-by-Step (3) #include int main(int argc, char **argv) { int score; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 50) printf(“Grade = F”); if(score >= 51 && score <= 60) printf(“Grade = D”); if(score >= 61 && score <= 70) printf(“Grade = C”); if(score >= 71 && score <= 80) printf(“Grade = B”); if(score >= 81 && score <= 100) printf(“Grade = A”); } Add all the conditions to our program Our program runs fine. However, we can do it better. Problems of this program: 1.If we want to change output display from “Grade = …” to “Your grade is …” we need to make change in our code 5 places. 2.This program need to evaluate 5 conditions all the time.

Be a good programmer (1)  Let’s solve the first problem. We need to write a program that can easily to change. int main(int argc, char **argv) { int score; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 50) printf(“Grade = F”); if(score >= 51 && score <= 60) printf(“Grade = D”); if(score >= 61 && score <= 70) printf(“Grade = C”); if(score >= 71 && score <= 80) printf(“Grade = B”); if(score >= 81 && score <= 100) printf(“Grade = A”); } int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 50) grade = ‘F’; if(score >= 51 && score <= 60) grade = ‘D’; if(score >= 61 && score <= 70) grade = ‘C’; if(score >= 71 && score <= 80) grade = ‘B’; if(score >= 81 && score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); } Define a character variable (grade) that will hold the grade output value

Be a good programmer (2)  This code still need to evaluate all 5 conditions for any inputs.  Let’s see if we input 0  We know that grade is F from the first condition  However, with the flow of our program, program still test all of the rest conditions int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 50) grade = ‘F’; if(score >= 51 && score <= 60) grade = ‘D’; if(score >= 61 && score <= 70) grade = ‘C’; if(score >= 71 && score <= 80) grade = ‘B’; if(score >= 81 && score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); } IF – ELSE IF – ELSE statement can help

Condition: IF- ELSE IF - ELSE statement Condition 1 True action Condition 2 True action Condition N True actionFalse action truefalse truefalse truefalse if (condition-1) statement; else if (condition-2) statement;. else if (condition-n) statement; else statement;

QUIZ 1 int A, B; if ( (A + 5) <= 6 ) { B = A + 10; } else if ( A == 2 || A == 5 ) { B = A – 10; } else if (A == 3 || A > 7) { B = A * 2; } else if (A == 4) { B = A / 4; } else { B = (A + 2) * 3; } B = B + 1; What is the value stored in the variable B at the end of program If A =

Be a good programmer (3)  Make change of our code by using if-else if instead of all if statements. int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 50) grade = ‘F’; if(score >= 51 && score <= 60) grade = ‘D’; if(score >= 61 && score <= 70) grade = ‘C’; if(score >= 71 && score <= 80) grade = ‘B’; if(score >= 81 && score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); } int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 50) grade = ‘F’; else if(score >= 51 && score <= 60) grade = ‘D’; else if(score >= 61 && score <= 70) grade = ‘C’; else if(score >= 71 && score <= 80) grade = ‘B’; else if(score >= 81 && score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); }

Be a good programmer (4)  Now with if-else if statements, we can compact more on our condition. int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score <= 50) grade = ‘F’; else if(score <= 60) grade = ‘D’; else if(score <= 70) grade = ‘C’; else if(score <= 80) grade = ‘B’; else if(score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); } int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 50) grade = ‘F’; else if(score >= 51 && score <= 60) grade = ‘D’; else if(score >= 61 && score <= 70) grade = ‘C’; else if(score >= 71 && score <= 80) grade = ‘B’; else if(score >= 81 && score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); }

Be a good programmer (5)  Good program needs to take care of invalid input  Now, make change of our program that if user input a number of score out of range 0 – 100, we will display “Score is only 0 – 100” int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score <= 50) grade = ‘F’; else if(score <= 60) grade = ‘D’; else if(score <= 70) grade = ‘C’; else if(score <= 80) grade = ‘B’; else if(score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); } int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 100) { if(score <= 50) grade = ‘F’; else if(score <= 60) grade = ‘D’; else if(score <= 70) grade = ‘C’; else if(score <= 80) grade = ‘B’; else if(score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); } else { printf(“Score is only 0-100”); }

Be a good programmer (6)  If you look carefully in if-else if condition, the last else if condition doesn’t really need to be evaluated.  We can just remove that condition int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 100) { if(score <= 50) grade = ‘F’; else if(score <= 60) grade = ‘D’; else if(score <= 70) grade = ‘C’; else if(score <= 80) grade = ‘B’; else if(score <= 100) grade = ‘A’; printf(“Grade = %c”, grade); } else { printf(“Score is only 0-100”); } int main(int argc, char **argv) { int score; char grade; printf(“Enter your score : ”); scanf(“%d”, &score); if(score >= 0 && score <= 100) { if(score <= 50) grade = ‘F’; else if(score <= 60) grade = ‘D’; else if(score <= 70) grade = ‘C’; else if(score <= 80) grade = ‘B’; else grade = ‘A’; printf(“Grade = %c”, grade); } else { printf(“Score is only 0-100”); }

The switch-case statement  The switch statement may also be used in C to select one of several alternatives.  The switch statement is especially useful when the selection is based on the value of a single variable or of an integer expression.  In C, switch-case statement operation is faster than if-else if-else statement operation.

The switch-case Syntax

Example: switch - case

Using break in switch-case

Example: Month Display Program using if

Example: Month Display Program using switch-case Is it correct ?

Last example  Easy program that take a character from user and  Display “YES” if user input ‘Y’ or ‘y’  Display “NO” if user input ‘N’ or ‘n’  Otherwise display “???” int main(int argc, char **argv) { char ch; printf(“[Y]es or [N]o:”); scanf(“%c”, &ch); if(ch == ‘Y’ || ch == ‘y’) printf(“YES”); else if(ch == ‘N’ || ch == ‘n’) printf(“NO”); else printf(“???”); } int main(int argc, char **argv) { char ch; printf(“[Y]es or [N]o:”); scanf(“%c”, &ch); switch( ch ) { case ‘Y’ : case ‘y’ : printf(“YES”); break; case ‘N’ : case ‘n’ : printf(“NO”); break; default : printf(“???”); }