Programming Construct 1 ) We divide the basic programming construct into three types: Sequential Structure 、 Selection Structure and Loop Structure.

Slides:



Advertisements
Similar presentations
2.1 Program Construction In Java
Advertisements

Decisions If statements in C.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Chapter 3 - Structured Program Development
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
© 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:::::::::
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
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;
Structured Program Development in C
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
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 Operators and Conditionals. Variables(again???) Type: Representation of “bits” in memory Variables: Name for a memory object. Starts with letters,
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
C Lecture Notes 1 Structured Program Development.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
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.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Python Conditionals chapter 5
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
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.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS115 FALL Senem KUMOVA-METİN1 CHAPTER 4 FLOW OF CONTROL-1 Operators, If and switch statements.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
Arithmetic Instructions. Integer and Float Conversions.
15.1 More About High-Level Programming Languages Syntax and Semantics Each programming language contains its own rules for both syntax and semantics. Syntax.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Operators And Expressions
Unit VI- Selection – Making Decisions, Repetition
Decisions Chapter 4.
Chapter 2.1 Control Structures (Selection)
If statement.
Introduction to Programming
Compound Assignment Operators in C++
Chapter 5 Decision Making and Branching
Structured Program
Chapter 3 - Structured Program Development
CSC215 Lecture Control Flow.
Chapter 3 - Structured Program Development
Summary Two basic concepts: variables and assignments Basic types:
Relational, Logical, and Equality Operators
Chapter 3 Operators and Expressions
EPSII 59:006 Spring 2004.
CSC215 Lecture Control Flow.
Presentation transcript:

Programming Construct 1 ) We divide the basic programming construct into three types: Sequential Structure 、 Selection Structure and Loop Structure. 2 ) Flow Chart( 流程图 ) : is a graphical representation in which specific symbols and necessary letters are used to describe the steps. 3 ) The flow chart about the three basic programming construct is as follows : A B conditio n A B A Sequential structure Selection Structure

1 、 relation operator and relation expression 2 、 logical operators and logical expression 3 、 if statements 4 、 switch statements 5 、 examples Chapter Outline

Three forms : ( 1 ) if ( expression ) statements ( 2 ) if ( expression ) statements 1 else statements 2 ( 3 ) if ( expression 1 ) statements 1 else if (expression 2) statements 2 else if (expression 3) statements 3 else if ( expression m ) statements m else statements n ex 第 五 章 选 择 结 构 If 语 句 的 形 式 Brief Summary

Forme1 : Forme1 : if ( expression ) statements expression statements F(0) T(not 0) ex : if(x>y) printf(“%d”,x); Firstly,solve the expression. If the value of the expression is not zero , then the corresponding ststement sequence is excuted. If none of the expression is false,then excute the statements after if derectly. 第 五 章 选 择 结 构 If 语 句 的 形 式 example

expression statements1statements2 T F ex : if(x>y) printf(“%d”,x); else printf(“%d”,y); Form2 : Form2 : if ( expression ) statements1 else statements2 第 五 章 选 择 结 构 If 语 句 的 形 式 example

expression1 expression2 expression3 expression4 statements1 statements2statements3 statements4statements5 F F F F T T T T ex : if (number>500) cost=0.15; else if (number>300) cost=0.10; else if (number>100) cost=0.075; else if (number>50) cost=0.05; else cost=0; Form3 : Form3 : nested- if statements 第 五 章 选 择 结 构 If 语 句 的 形 式 example

( 1 ) The expression of three forms behind if- statements always is logical or relation expression. : DCL of three if- statements forms : ( 2 ) In form2 and forme3 , there is a semicolon in front of else, also in the end of all statements. ( 3 ) Behind if or else,it may include only one operate statements,also include many statements,but should be drawed together with brace.({ }) 第 五 章 选 择 结 构 If 语 句 的 形 式

Ex: #include main() { int x,y; scanf(“%d,%d”,&x,&y); if(x>y) x=y; y=x; else x++; y++; printf(“%d,%d\n”,x,y); } Compile Error!

ex : if(a+b a&&c+a>b) { s=0.5*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(“area=%6.sf”,area); } else printf(“it is not a trilateral”); Notice : The brace( “}”)’s outer face need not semicolon 。 Because in the brace({}) there is a full compound statement. 第 五 章 选 择 结 构 If 语 句 的 形 式

original program : main() { float a,b,t; scanf(“%f,%f”,&a,&b); if(a>b) { t=a;a=b;b=t; } printf(“%5.2f,%5.2f”,a,b); } ex1 : Input twe real numbers , please output there numbers according to the order of little to big. Need three float variables: a,b,t 第 五 章 选 择 结 构 If 语 句 举 例

ex2 : Input three real numbers , please output there numbers according to the order of little to big. original program : main() { float a,b,c,t; scanf(“%f,%f,%f”,&a,&b,&c); if (a>b) {t=a;a=b;b=t;} if (a>c) {t=a;a=c;c=t;} if (b>c) {t=b;b=c;c=t;} printf(“%5.2f,%5.2f,%5.2f”,a,b,c); } 第 五 章 选 择 结 构 If 语 句 举 例 Return

#include main() { int a,b; printf("Enter integer a:"); scanf("%d",&a); printf("Enter integer b:"); scanf("%d",&b); if(a==b) printf("a==b\n"); else printf("a!=b\n"); } ex3 : Iput two numbers,decide them to be equally or not. Result : Enter integer a:12  Enter integer b:12  a==b Result : Enter integer a:12  Enter integer b:9  a!=b 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例 Return

General form : expression1 ? expression2 : expression3 Condition operator commands three operate objects,it is a operator alonely in C language. ex : max=(a>b) ? a : b; Equal to : if(a>b) max= a; else max=b; 第五章选择结构条件运算符第五章选择结构条件运算符 Condition operator

DCL : ( 1 ) Execution order : Firstly,solve the expression 1 , If the value is ture,then solve the expression 2, this moment here is the value of whole expressions. If the value is false, then solve the expression 3, this moment here is the value of whole expressions. ex : max=(a>b) ? a : b; If a=3,b=4; max = 4 ; If a=6 , b=4; max = 6 ; 第五章选择结构条件运算符第五章选择结构条件运算符

( 2 ) Condition operator overmatch assigned operator,but under relation and arithmetic operator. ( 3 ) Condition operator’s combined direcion is from right to left. ex1 : max=(a>b) ? a : b; max= a>b ? a : b; ex2 : max=a>b? a : b+1; max= a>b ? a : (b+1); ex3 : max= a>b ? a : c > d ? c:d ; max= a>b ? a : ( c > d ? c:d ) ; Suppose a=1,b=2,c=3,d=4 第五章选择结构条件运算符第五章选择结构条件运算符

( 4 ) Condition operator cann’t replce ordinary if-statements,only when the embed statements in it is assigned statements(and two branches assign to the same variable). ex : if (a>b) printf(“%d”,a); else printf(“%d”,b); printf(“%d”,a>b?a:b); ( 5 ) The type of expression 1 and expression 2 can be same,also be different. ex : int x; x ? ‘a’ : ‘b’; ex : x>y ? 1 :1.5 ; 第五章选择结构条件运算符第五章选择结构条件运算符

Ex5: Input a letter , iudge it is a capital letter or not , if is , then change it to lower-case letter ; if not , then nothing changed 。 Finally,output this letter 。 main() { char ch; scanf( “ %c “, &ch); ch = ( ch > = 'A‘ && ch < = 'Z‘ ) ? ( ch + 32 ) : ch ; printf( “ %c “, ch ) ; } 第五章选择结构条件运算符第五章选择结构条件运算符 Return

ex6 : Function -1 ( x<0) 0 (x=0) 1 (x>0) y= Write a programme , input the value of x. Then ouput y. main( ) { float x ; int y ; scanf(“ %f ”, &x ) ; if ( x < 0 ) y = - 1 ; if ( x = =0 ) y = 0 ; if ( x > 0 ) y = 1 ; printf(“ \n %f, %d ”, x, y ) ; } 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例 Return

Algorithm 2 : input x ifx<0 y= -1 else : ifx=0 y=0 ifx>0 y=1 output y Algorithm 3 : input x ifx<0 y=-1 else : ifx=0 y=0 else : y=1 ouput y 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例

p1 : main() {float x; int y; scanf(“%f”,&x); if (x<0) y=-1; else if (x==0) y=0; else y=1; printf(“x=%f,y=%d\n”,x,y); } p2 : modify to : if(x>=0) if(x>0) y=1; else y=0; else y=-1; p3 : modify to : y=-1; if(x!=0) if(x>0) y=1; else y=0; p4 : modify to : y=0; if(x>=0) if(x>0) y=1; else y=-1; 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例 Return

#include main() { int x,y; printf("Enter an integer:"); scanf("%d",&x); y=x; if(y<0) y= -y; printf("\ninteger:%d--->absolute value:%d\n",x,y); } ex 运行: Enter an integer:-12  integer:-12--->absolute value :12 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例

ex #include main() { int x,y; printf("Enter integer x,y:"); scanf("%d,%d",&x,&y); if(x!=y) if(x>y) printf("X>Y\n"); else printf("X<Y\n"); else printf("X==Y\n"); } 运行: Enter integer x,y:12,23  X<Y Enter integer x,y:12,6  X>Y Enter integer x,y:12,12  X==Y 第 五 章 选 择 结 构 If 语 句 举 例