EEE 0115 Chapter 3 Structured Program Development in

  • Slides: 24
Download presentation
EEE 0115 Chapter 3: Structured Program Development in C 1 C How to Program

EEE 0115 Chapter 3: Structured Program Development in C 1 C How to Program Deitel & Deitel

2 Algorithms Pseudocode Control Structures The if Selection Statement The if. . . else

2 Algorithms Pseudocode Control Structures The if Selection Statement The if. . . else Selection Statement The while Repetition Statement

3 Algorithms Algorithm is defined as the actions to be executed in a specific

3 Algorithms Algorithm is defined as the actions to be executed in a specific order to solve a given problem. Program control is an important concept and it defiens the specific order in which statements are to be executed.

4 Pseudocode Artificial or informal language which is used to develop algorithms Pseudocodes are

4 Pseudocode Artificial or informal language which is used to develop algorithms Pseudocodes are not executed on computers. They are similar to everyday English. Pseudocodes can be converted easily to C program.

5 Control Structures All programs can be written in terms of 3 control structures:

5 Control Structures All programs can be written in terms of 3 control structures: Sequence structures: Statements executed sequentially. It is default control structure. Selection structures: if, if-else, switch. Repetition structures: while, do-while, for.

6 The if selection statement Used to choose among alternative actions: Pseudocode of if:

6 The if selection statement Used to choose among alternative actions: Pseudocode of if: If student’s grade is less than 50 print “Failed” If the condition returns true, print statement is executed. If the condition returns false, the body of if is not executed. Program control goes to next statement after if block.

7 The if selection statement Pseudocode statement is transferred to C program as follows:

7 The if selection statement Pseudocode statement is transferred to C program as follows: if(grade<50) printf("Passedn" ); Indentation improves program readibility. C ignores whitespace characters (space, tab, and newline).

8 The if-else selection statement This statement specifies an action for both true condition

8 The if-else selection statement This statement specifies an action for both true condition and false condition. Psuedocode of if-else: If student’ grade is less than 50 Print “Failed” else Print “Passed”

9 The if-else selection statement C code : If (grade<50) printf(“Failedn”); else printf(“Passedn”); Ternary

9 The if-else selection statement C code : If (grade<50) printf(“Failedn”); else printf(“Passedn”); Ternary conditional operator (? : ) It has three arguments (condition, value if true, value if false) printf( "%sn", grade >= 60 ? "Passed" : "Failed" ); grade >= 60 ? printf( “Passedn” ) : printf( “Failedn” );

10 The if-else selection statement Nested if-else statements They are used to test for

10 The if-else selection statement Nested if-else statements They are used to test for multiple cases. if-else selection statements are placed inside other if-else selection statements. Whenever a condition returns true, rest of the statements are skipped.

11 The if-else selection statement Compound statement is defined as a set of statements

11 The if-else selection statement Compound statement is defined as a set of statements within a pair of braces Compound statements with declarations are defined as block. if(a>10) { printf("Your value is greater than 10n"); printf("Enter a new valuen"); }

12 The while repetition statement Repetition structures are used whenever a number of actions

12 The while repetition statement Repetition structures are used whenever a number of actions to be repeated while a condition remains true. Example pseudocode: While varible x is less than 10 Add x to current some Example C code: while(x<10) sum=sum+x;

13 Counter-Controlled Repetition

13 Counter-Controlled Repetition

14 Counter-Controlled Repetition

14 Counter-Controlled Repetition

15 Algorithms (Top-down, stepwise refinement) Programs usually have three phases: Initialization Processing Termination

15 Algorithms (Top-down, stepwise refinement) Programs usually have three phases: Initialization Processing Termination

16 Algorithms (Top-down, stepwise refinement) A class-averaging program (unknown number of students)

16 Algorithms (Top-down, stepwise refinement) A class-averaging program (unknown number of students)

17 Algorithms (Top-down, stepwise refinement) A class-averaging program (unknown number of students)

17 Algorithms (Top-down, stepwise refinement) A class-averaging program (unknown number of students)

18 Algorithms (Top-down, stepwise refinement) A class-averaging program (unknown number of students)

18 Algorithms (Top-down, stepwise refinement) A class-averaging program (unknown number of students)

19 Algorithms (Top-down, stepwise refinement) Nested Control Structures

19 Algorithms (Top-down, stepwise refinement) Nested Control Structures

20 Algorithms (Top-down, stepwise refinement) Nested Control Structures

20 Algorithms (Top-down, stepwise refinement) Nested Control Structures

21 Algorithms (Top-down, stepwise refinement) Nested Control Structures

21 Algorithms (Top-down, stepwise refinement) Nested Control Structures

22 Assignment Operators Assignment operators can be used instead of assignment expressions: a=a+5 can

22 Assignment Operators Assignment operators can be used instead of assignment expressions: a=a+5 can be written as a+=5 a=a-5 can be written as a-=5 a=a*5 can be written as a/=5 a=a/5 can be written as a+=5 a=a%5 can be written as a%=5

23 Increment and Decrement Operators Increment operator (c++) can be used instead of c=c+1

23 Increment and Decrement Operators Increment operator (c++) can be used instead of c=c+1 Decrement operator (c--) can be used instead of c=c-1 c++ and c-- postincrement operators Expression ++c executes before the variable is changed and --c postincrement operators Variable is changed and then expression executes.

24 Increment and Decrement Operators

24 Increment and Decrement Operators