Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.

Similar presentations


Presentation on theme: "CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101."— Presentation transcript:

1 CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101

2 Control structure  Normally, statements in a program are executed one after the other in the order in which they’re written.This is called sequential execution.  Various C statements we’ll soon discuss enable you to specify that the next statement to be executed may be other than the next one in sequence.This is called transfer of control. 2 A.AlOsaimi

3 Types of Control structure  could be written in terms of only three control structures:  sequence structure  selection structure  repetition structure. 3 A.AlOsaimi

4 Types of Control Structures A.AlOsaimi 4

5 Selection structure  C provides three types of selection structures in the form of statements:  The if selection statement either performs (selects) an action if a condition is true or skips the action if the condition is false.  The if … else selection statement performs an action if a condition is true and performs a different action if the condition is false.  The switch selection statement performs one of many different actions depending on the value of an expression. 5 A.AlOsaimi

6 selection structure : if  The if statement is called a single-selection statement because it selects or ignores a single action.  Syntax: if (expression) statement Expression referred to as decision maker. Statement referred to as action statement. 6 A.AlOsaimi

7 If – example  Write a program that reads a grade and prints “passed” if the grade is greater than or equal 60. if ( grade >= 60 ) { printf( "Passed\n" ); } /* end if */ 7 A.AlOsaimi

8 selection structure : if.. else  The if…else statement is called a double- selection statement because it selects between two different actions.  Syntax: if (expression) statement else statement 8 A.AlOsaimi

9 If..else- Example  For example, the pseudocode statement  If student’s grade is greater than or equal to 60 Print “Passed” else Print “Failed” if ( grade >= 60 ) printf( "Passed\n" ); else printf( "Failed\n" ); 9 A.AlOsaimi

10 Compound (Block of) Statements A.AlOsaimi 10  Syntax:  {  statement1  statement2 .  statementn  }

11 Compound (Block of) Statements A.AlOsaimi 11 if ( grade >= 60 ) printf( "Passed\n" ); else { printf( "Failed\n" ); printf( “you have to take this course again\n" ); } Java Programming: From Problem Analysis to Program Design, D.S. Malik

12 Multiple Selection: Nested if if (expression1) statement1 else if(expression2) statement2 else statement3 Multiple if statements can be used if there is more than two alternatives else is associated with the most recent if that does not have an else syntax 12 A.AlOsaimi

13 Nested if - example  Many C programmers prefer to write the preceding if statement as if ( grade >= 90 ) printf( "A\n" ); else if ( grade >= 80 ) printf( "B\n" ); else if ( grade >= 70 ) printf( "C\n" ); else if ( grade >= 60 ) printf( "D\n" ); else printf( "F\n" ); 13 A.AlOsaimi

14 Nested if - example if( tempreture >= 50 ) if (tempreture >= 80) printf (“Good swimming day”); else printf (“Good golfing day”); else printf (“Good tennis day”); 14 A.AlOsaimi

15 Multiple Selection: Nested if A.AlOsaimi 15 if( tempreture >= 50 ) if (tempreture >= 80) printf(“Good swimming day”); else printf(“Good golfing day”);

16 Multiple Selection: Nested if A.AlOsaimi 16 //The following code does not work as intended. For example if GPA=3.8 if ( GPA >= 2.0 ) if (GPA >= 3.9) printf(“Deen Honor list\n”); else printf(“GPA below graduation requirement\n”); ---------------------------------------------------------- //This is the correct way of writing the above code if ( GPA >= 2.0 ) { if (GPA >= 3.9) printf(“Deen Honor list”); } else printf(“GPA below graduation requirement\n”);

17 If …else = conditional expression  C provides the conditional operator (?:) which is closely related to the if…else statement.  The conditional operator is C’s only ternary operator—it takes three operands. Syntax:  expression1 ? expression2 : expression3 If expression1 = true, then the result of the condition is expression2.  Otherwise, the result of the condition is expression3. 17 A.AlOsaimi

18 Conditional (? :) Operator- example A.AlOsaimi 18 Example: int x = 5, y =3, min ; if (x< = y) min = x ; else min = y ; The above statement can be written using the conditional operator : min = ( x<= y ) ? x : y ;

19 selection structure : switch  The switch statement is called a multiple- selection statement because it selects among many different actions. 19 A.AlOsaimi

20 Switch Structures A.AlOsaimi 20 Expression is also known as selector. Expression can be an identifier. Value can only be integral. switch (expression) { case value1: statements1 break; case value2: statements2 break;... case valuen: statementsn break; default: statements }

21 Switch With break Statements A.AlOsaimi 21 switch (N) { case 1: x = 10; break; case 2: x = 20; break; case 3: x = 30; break; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true break;

22 A.AlOsaimi 22 Switch With No break Statements switch (N) { case 1: x = 10; case 2: x = 20; case 3: x = 30; } x = 10; false true N == 1 ? x = 20; x = 30; N == 2 ? N == 3 ? false true

23 Switch With break Statements A.AlOsaimi 23 Example switch (grade) { case 'A': printf("The grade is A."); break; case 'B': printf("The grade is B."); break; case 'C': printf("The grade is C."); break; case 'D': printf("The grade is D."); break; case 'F': printf("The grade is F."); break; default: printf("The grade is invalid."); }

24 Previous Example - With Nested If A.AlOsaimi 24  if (grade == 'A') printf("The grade is A."); else if (grade == 'B') printf("The grade is B."); else if (grade == 'C') printf("The grade is C."); else if (grade == 'D') printf("The grade is D."); else if (grade == 'F') printf("The grade is F."); else printf("The grade is invalid.");

25 Exercise  Write a program that reads the price and the production date ( month and year of an item ) then the program prints the price after applying the discount rate which equals to :  50% if it is produced before 2011.  30% if it is produced in 2011 but before the 4 th month.  10% if it is produced after 4,2011 and before 8,2011 25 A.AlOsaimi


Download ppt "CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101."

Similar presentations


Ads by Google