Decision Making.

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.
CS161 Introduction to Computer Science Character Data.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
UniMAP Sem II-09/10EKT120: Computer Programming1 Week 3 – Selection Structures.
Previously Repetition Structures While, Do-While, For.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Introduction to C Programming CE Lecture 3 Control Structures in C.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
CTYPE.H Introduction  The ctype header is used for testing and converting characters.  A control character refers to a character that.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Making Decisions in c. 1.if statement Imagine that you could translate a statement such as “If it is not raining, then I will go swimming” into the C.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 4 – C Program Control
The if…else Selection Statement
Decision Making in C.
C Characters and Strings
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 3 Control Statements
C Characters and Strings
Chapter 4 C Program Control Part I
Character Processing How characters can be treated as small integers?
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Week 3 C Program Structures (Selection Structures)
Chapter 4 - Program Control
Week 3 – Selection Structures
Condition Statements.
DKT121: Fundamental of Computer Programming
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Enum ,Char Functions& Math Library Functions I.Mona Alshehri
Chapter 4: Making Decisions.
Chapter 8 - Characters and Strings
Control Structures.
Control Structures – Selection
Introduction to Programming
- Additional C Statements
Chapter 8 JavaScript: Control Statements, Part 2
Additional Control Structures
Chapter 5 Decision Making and Branching
1) C program development 2) Selection structure
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Program Control Topics While loop For loop Switch statement
UMBC CMSC 104 – Section 01, Fall 2016
Chapter 4 Selection.
Week 2 Variables, flow control and the Debugger
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
2.6 The if/else Selection Structure
Character Processing How characters can be treated as small integers?
Week 3 – Program Control Structure
Control Structures Lecture 6.
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
Variables in C Topics Naming Variables Declaring Variables
C Characters and Strings
CSCE 206 Lab Structured Programming in C
ICS103: Programming in C 4: Selection Structures
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Switch Case Structures
Presentation transcript:

Decision Making

Decision Making: Equality and Relational Operators Executable C statements either perform actions (such as calculations or input or output of data) or make decisions (we’ll soon see several examples of these). We might make a decision in a program, for example, to determine if a person’s grade on an exam is greater than or equal to 60 and if it is to print the message “Congratulations! You passed.” This section introduces a simple version of C’s if statement that allows a program to make a decision based on the truth or falsity of a statement of fact called a condition.

Decision Making (Cont.) If the condition is met (i.e., the condition is true) the statement in the body of the if statement is executed. If the condition is not met (i.e., the condition is false) the body statement is not executed. Whether the body statement is executed or not, after the if statement completes, execution proceeds with the next statement after the if statement. Conditions in if statements are formed by using the equality operators and relational operators summarized in Fig. 2.12.

Equality operators and relational operators

if structure-Method1 if (condition) { statement 1; statement 2; … statement n; } else

if structure-Method2 if (condition) statement ; else

In C, a condition may actually be any expression that generates a zero (false) or nonzero (true) value! #include <stdio.h> int main() { int num1,num2; printf("Enter the 1st number >> "); scanf("%d",&num1); printf("Enter the 2nd number >> "); scanf("%d",&num2); //incorrect use of operation!, check the program with zero value if(num1=num2) { printf("num1=%d is equal to num2=%d",num1,num2); } else { printf("num1=%d is NOT equal to num2=%d",num1,num2); return 0;

Tip ; is a null statement! #include <stdio.h> int main() { int num; printf("enter a number>>"); scanf("%d",&num); if (num>100); printf("This statement always is displayed!"); return 0; }

Nested if structure if (condition #1) { some statements; } else if(condition #2) … else if(condition #N) else

Example: Get grade and display the score 17≤ grade ≤ 20 A 15≤grade<17 B 12≤grade<15 C grade<12 D

Example: Get grade and display the score (cont.) #include <stdio.h> int main() { float grade; printf("Enter the student\'s grade >> "); scanf("%f",&grade); if(grade>=17 && grade<=20) printf("grade =%5.2f, score=%c",grade,'A'); else if(grade>=15 && grade<17) printf("grade =%5.2f, score=%c",grade,'B'); else if(grade>=12 && grade<15) printf("grade =%5.2f, score=%c",grade,'C'); else printf("grade =%5.2f, score=%c",grade,'D'); return 0; }

switch Multiple-Selection Statement Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may assume, and different actions are taken. This is called multiple selection. C provides the switch multiple-selection statement to handle such decision making. The switch statement consists of a series of case labels, an optional default case and statements to execute for each case.

switch-case decision switch (expression){ case value#1: some statements; break; case value#2: … case value#n: default: }

switch-case decision(cont.) The expression’s Types: any expression of integral or enumeration type The default section is optional, if the expression is equal to none of the values, none of the statements is done! When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Nested switch-case We can use nested switch-case! switch (expression){ case value#1: some statements; break; … default: }

switch-case vs. if if considers the logical and relational statements but switch-case considers the equality!

Example: A simple calculator! Get the operation (contains +,-,*,/,\) as a character (variable name: op)! Get two input numbers (variable names: num1, num2 ). Compute and display the expression of: num1 (op) num2 + - * /or\

A simple calculator! (cont.) float num1,num2;char op; printf("Enter the operation>>");scanf("%c",&op); printf("Enter the 1st number>>");scanf("%f",&num1); printf("Enter the 2nd number>>");scanf("%f",&num2); switch(op) { case'+': printf("%f %c %f = %f",num1,op,num2,num1+num2); break; case'*': printf("%f %c %f = %f",num1,op,num2,num1*num2); case '-': printf("%f %c %f = %f",num1,op,num2,num1-num2); case '/': case '\\': printf("%f %c %f = %f",num1,op,num2,num1/num2); default: printf("The operation is not defined!"); }

Example: color detector using nested switch Get a character Check if the char is alphabetic, using isalpha() Detect the color, colors are: r, g, b, y The result should be case-insensitive!

Function isalpha() Header file: <ctype.h> Function Work Of Function isalnum Tests whether a character is alphanumeric or not isalpha Tests whether a character is aplhabetic or not iscntrl Tests whether a character is control or not isdigit Tests whether a character is digit or not isgraph Tests whether a character is grahic or not islower Tests whether a character is lowercase or not isprint Tests whether a character is printable or not ispunct Tests whether a character is punctuation or not isspace Tests whether a character is white space or not isupper Tests whether a character is uppercase or not isxdigit Tests whether a character is hexadecimal or not tolower Converts to lowercase if the character is in uppercase toupper Converts to uppercase if the character is in lowercase

char color; printf("Enter the color char>>"); scanf("%c",&color); switch(isalpha(color)){ case 0: printf("Not alphabetic!"); break; default: if(color<=‘Z’) color+= 'a'-'A'; switch (color) { case 'r': printf("The color is red"); case 'b': printf("The color is blue"); case 'y': printf("The color is yellow"); case 'g': printf("The color is green"); printf("No color is defined!"); }