BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.

Slides:



Advertisements
Similar presentations
Decisions If statements in C.
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Chapter 3 - Structured Program Development
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Lecture 4 Introduction to Programming in C Prof. Dr. Arne Kutzner Hanyang University / Seoul Korea.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Structural Program Development: If, If-Else Outline.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Structured Program Development Outline 2.1Introduction 2.2Algorithms 2.3Pseudo code 2.4Control Structures 2.5The If Selection Structure 2.6The If/Else.
Lecture 2: Logical Problems with Choices. Problem Solving Before writing a program Have a thorough understanding of the problem Carefully plan an approach.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C Lecture Notes 1 Structured Program Development.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Structured Program Development Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010.
Program to calculate product of odd numbers b/w 1 and 15 #include main() { int prod = 1, x; for(x = 1; x
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Control statements Mostafa Abdallah
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Control Statements: Part1  if, if…else, switch 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
Chapter 4 – C Program Control
The if…else Selection Statement
- Standard C Statements
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Lecture 2: Logical Problems with Choices
- Additional C Statements
Structured Program
Chapter 4 - Program Control
Chapter 3 - Structured Program Development
3 Control Statements:.
Program Control Topics While loop For loop Switch statement
Chapter 3 - Structured Program Development
2.6 The if/else Selection Structure
Dale Roberts, Lecturer IUPUI
Control Statements Paritosh Srivastava.
Chapter 4 - Program Control
Dale Roberts, Lecturer IUPUI
Dale Roberts, Lecturer IUPUI
Flow of Control.
Structural Program Development: If, If-Else
Presentation transcript:

BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures

BBS514 Structured Programming (Yapısal Programlama)2 Control Structures All programs written in terms of 3 control structures: Sequence structures: Built into C. Programs executed sequentially by default. Selection structures: C has three types: if, if / else, and switch Repetition structures: C has three types: while, do / while and for

BBS514 Structured Programming (Yapısal Programlama)3 Relational Operators The relational operators are, =. They take 2 expressions as operands and yield either the int value 0 (false) or the int value 1 (true). Valid Invalid a < 3 a =< b a > b a < = b Examples: Assume a = 1, b=2. ExpressionValue a <= b 1 a < b-5 0 a + 10 / b <=

BBS514 Structured Programming (Yapısal Programlama)4 Equality Operators The equality operators are == and !=. Yield either the int value 0 or the int value 1. Valid Invalid x != x = = y-1 x != 3.3/z x =! 44 ch == ‘*’ch = ‘*’ Examples: Assume a=1, b=2, ch = ‘A’ ExpressionValue a == b0 a != b1 ch < ‘B’1 a+b == -2 * 30

BBS514 Structured Programming (Yapısal Programlama)5 Equality Operators Note carefully that the two expressions a == b and a = b are visually similar. The expression a == b is a test for equality and a = b is an assignment expression.

BBS514 Structured Programming (Yapısal Programlama)6 Logical Operators The logical operators are &&, ||, and !. Expressions connected by && or || are evaluated left to right. Logical negation: ! Value of expression !expression zero 1 nonzero 0 Examples ExpressionValue !50 !!51 !(6 < 7)0 !6 < 71 !(3-4)0

BBS514 Structured Programming (Yapısal Programlama)7 Logical Operators aba && ba || b zero 00 nonzero01 zero01 nonzero 11

BBS514 Structured Programming (Yapısal Programlama)8 Examples Given declarations: int a =3, b = 3, c =3; double x = 0.0, y =2.5; char ch = ‘g’ ExpressionValue !(a<b) && c 1 ch >= ‘a’ && ch <= ‘z’ 1 x || a && b – 3 0 a < b && x < y 0 a < b || x < y 1 The precedence of && is higher than ||, but both operators are of lower precedence than all unary, arithmetic and relational operators. Their associativity is left to right.

BBS514 Structured Programming (Yapısal Programlama)9 Short-Circuit Evaluation For the expressions that contain the operands of && and ||, the expression process stops as soon as the outcome true or false is known. Suppose expr1 is 0. expr1 && expr2 = 0 (expr2 will not be evaluated.) Suppose expr1 is nonzero. expr1 || expr2 = 1 (expr2 will not be evaluated.)

BBS514 Structured Programming (Yapısal Programlama)10 The if Selection Structure Selection structure: –Used to choose among alternative courses of action –e.g: If student’s grade is greater than or equal to 60 Print “Passed” If condition true –Print statement executed and program goes on to next statement –If false, print statement is ignored and the program goes onto the next statement

BBS514 Structured Programming (Yapısal Programlama)11 if Statement true false grade >= 60 print “Passed” A decision can be made on any expression. zero - false nonzero - true Example: is true if ( grade >= 60 ) printf( "Passed\n" ); printf(“Bye!\n”);

BBS514 Structured Programming (Yapısal Programlama)12 /* Determines if a number is even */ #include int main() { int value; printf(“Enter a number.\n”); scanf(“%d”,&value); if (value % 2 == 0) printf(“\n%d is an even number.\n”,value); return 0; } Enter a number is an even number. Enter a number. 15

BBS514 Structured Programming (Yapısal Programlama)13 /* An example program using a compound statement in an if statement */ #include int main ( ) { int value1, value2, max=0; printf(“Enter two values:\n”); scanf(“%d%d”, &value1, &value2); if (value1 > value2) { max = value1; printf(“Value1 is greater than value2. \n”); } printf(“%d\n”, max); return 0; } Enter two values: 10 5 Value1 is greater than value2. 10 Enter two values:

BBS514 Structured Programming (Yapısal Programlama)14 if-else statement Flow chart of the if / else selection structure if ( grade >= 60 ) printf( "Passed\n"); else printf( "Failed\n"); truefalse print “Failed”print “Passed” grade >= 60

BBS514 Structured Programming (Yapısal Programlama)15 /* Determines the larger of two numbers */ #include int main () { int v1, v2, larger; printf(“Enter two numbers.\n”); scanf(“%d%d”, &v1, &v2); if (v1 > v2) larger = v1; else larger = v2; printf(“%d is the larger number.\n”, larger); return 0; } Enter two numbers is the larger number. Enter two numbers is the larger number.

BBS514 Structured Programming (Yapısal Programlama)16 Examples Compound statement: if ( grade >= 60 ) printf( "Passed.\n" ); else { printf( "Failed.\n" ); printf( "You must take this course again.\n" ); } Dangling else: an else attaches to the nearest if. if (a == 10) if (b==20) printf(“***\n”); else printf(“###\n”);

BBS514 Structured Programming (Yapısal Programlama)17 Problem Solving // Find the minimum of three values. #include <stdio.h> int main() { int a, b, c, min; printf(“Enter three numbers:”); scanf(“%d%d%d”, &a,&b,&c); if (a < b) min = a; else min = b; if (c < min) min = c; printf(“The minimum value is %d\n”, min); return 0; }

BBS514 Structured Programming (Yapısal Programlama)18 Nested if / else structures Test for multiple cases by placing if / else selection structures inside if / else selection structures. Once condition is met, the rest of statements skipped. Its general form is: if (expr1) statement1 else if (expr2) statement2 else if (expr3) statement3 …… else if (exprN) statementN else default statement next statement

BBS514 Structured Programming (Yapısal Programlama)19 Nested if’s if (grade >= 90) printf(“A”); else if (grade >= 80) printf(“B”); else if (grade >= 70) printf(“C”); else if (grade >= 60) printf(“D”); else printf(“F”);

BBS514 Structured Programming (Yapısal Programlama)20 The Ternary Conditional Operator (?:) cond ? expr1 : expr2 –Takes three arguments (condition, value if true, value if false ) –e.g: printf("%s\n", grade >= 60 ? "Passed" : "Failed" ); –Or it could have been written: grade >= 60 ? printf( “Passed\n” ) : printf( “Failed\n” ); –Or it could be used in an assignment statement: letter = (grade >= 60) ? ‘S’ : ‘U’;

BBS514 Structured Programming (Yapısal Programlama)21 The switch Multiple-Selection Structure switch –Useful when a variable or expression is tested for all the values it can assume and different actions are taken Format –Series of case labels and an optional default case switch ( a_variable ){ case value1: actions case value2 : actions... default: actions } –break; exits from structure

BBS514 Structured Programming (Yapısal Programlama)22 The switch Multiple-Selection Structure true false case a case a action(s)break case b case b action(s)break false case z case z action(s)break true default action(s)

BBS514 Structured Programming (Yapısal Programlama)23 /*Counting letter grades */ int grade; int aCount = 0, bCount = 0, cCount = 0, dCount = 0, fCount = 0; printf("Enter the letter grade.\n" ); scanf(“%c”,&grade); switch ( grade ) { case 'A': case 'a': ++aCount; break; case 'B': case 'b': ++bCount; break; case 'C': case 'c': ++cCount; break; case 'D': case 'd': ++dCount; break; case 'F': case 'f': ++fCount; break; default: /* catch all other characters */ printf( "Incorrect letter grade entered." ); printf( " Enter a new grade.\n" ); break; }

BBS514 Structured Programming (Yapısal Programlama)24 int main() { int month, year, days, leapyear; printf(“Enter a month and a year:”); scanf(“%d%d”, &month, &year); if (((year % 4 == 0) && (year % 100 != 0))|| (year % 400 == 0) ) leapyear = 1; else leapyear = 0; switch (month){ case 9 : case 4 : case 6 : case 11: days=30; break; case 2 : days = (leapyear == 1)? 29: 28; break; default : days = 31; } printf(“There are %d days in that month in that year.\n”, days); }

BBS514 Structured Programming (Yapısal Programlama)25 Exercises 1.Describe in English what the following conditional expression means: (x != 4) || (x != 17) 2.Write a conditional expression that is true i.if exactly one of a and b is true. ii.if both a and b are true, or both are false.

BBS514 Structured Programming (Yapısal Programlama)26 Exercises 3.Write a C statement that i.classifies a given character as an uppercase letter, a lowercase letter or a special character. ii.If the values of variables a, b, and c are all the same, print the message “All equal” on the screen and find their sum.

BBS514 Structured Programming (Yapısal Programlama)27 Programming Exercise Description: Write a C program that reads in three numbers, checks whether they can be the lengths of the three sides of a triangle (the length of each side must be less than the sum of the other two); and finally determines whether the triangle is scalene, isosceles, equilateral, or right-angled. –Equilateral triangle: all three sides are of equal length; –Isosceles triangle: only two sides of the triangle are of equal length; –Right-angled triangle: the square of the longest side is equal to the summation of the squares of the other two sides; –Scalene triangle: any triangle which do not meet any of the criteria above.