Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.

Similar presentations


Presentation on theme: "Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4."— Presentation transcript:

1 Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4

2 Lecture 4: C/C++ Control Structures Control Structures Sequential execution –Statements executed in order (from first to last sequence). Transfer of control –Next statement executed not next one in sequence. 3 control structures –Sequence structure Programs executed sequentially by default –Selection structures (conditional instructions) if, if/else, switch –Repetition structures (loop/jump instructions) while, do/while, for

3 Lecture 4: C/C++ Control Structures if Selection Structure Selection structure –Choose among alternative courses of action –Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed” –If the condition is true Print “Passed” statement executed, program continues to next statement. –If the condition is false Next statement will be executed.

4 Lecture 4: C/C++ Control Structures if Selection Structure Translation into C Language If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) printf ("Passed“); pseudo code C Language code

5 Lecture 4: C/C++ Control Structures if Selection Structure Flowchart of pseudocode statement

6 Lecture 4: C/C++ Control Structures if/else Selection Structure if –Performs action if condition true if/else –Different actions if conditions true or false Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C Language code if ( grade >= 60 ) printf("Passed“); else printf("Failed“);

7 Lecture 4: C/C++ Control Structures if/else Selection Structure Ternary conditional operator (?:) –Three arguments (condition, value if true, value if false) Code could be written: printf( grade >= 60 ? “Passed” : “Failed” ); ConditionValue if trueValue if false

8 Lecture 4: C/C++ Control Structures if/else Selection Structure

9 Lecture 4: C/C++ Control Structures if/else Selection Structure Nested if/else structures –One inside another, test for multiple cases –Once condition met, other statements skipped if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”

10 Lecture 4: C/C++ Control Structures if/else Selection Structure Example if ( grade >= 90 ) // 90 and above printf("A“); else if ( grade >= 80 ) // 80-89 printf("B“); else if ( grade >= 70 ) // 70-79 printf("C“); else if ( grade >= 60 ) // 60-69 printf("D“); else // less than 60 printf("F“);

11 Lecture 4: C/C++ Control Structures if/else Selection Structure Compound statement –Set of statements within a pair of braces if ( grade >= 60 ) printf("Passed“); else { printf("Failed“); printf("You must take this course again“); } –Without braces, printf("You must take this course again”); always executed Block –Set of statements within braces

12 Lecture 4: C/C++ Control Structures while Repetition Structure Repetition structure –Action repeated while some condition remains true –Pseudocode while there are more items on my shopping list Purchase next item and cross it off my list –while loop repeats until condition becomes false C Language Example int product = 2; while ( product <= 1000 ) product = 2 * product;

13 Lecture 4: C/C++ Control Structures The while Repetition Structure

14 Lecture 4: C/C++ Control Structures Formulating Algorithms (Counter-Controlled Repetition) Example A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. Counter-controlled repetition –Loop repeated until counter reaches certain value –Same as we have studied in “Program Counter” register of control unit.

15 Lecture 4: C/C++ Control Structures Formulating Algorithms (Counter- Controlled Repetition) Pseudocode for example: Set total to zero Set grade counter to one while grade counter is greater than ten Input the next grade Add the grade into the total Add one to the grade counter Set the class average to the total divided by ten Print the class average Next: C code for this example

16 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } Total = 0 gradeCounter = 0 inputGrade = 0

17 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } Total = 0 gradeCounter = 0 inputGrade = 0 average = 0

18 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } Total = 0 gradeCounter = 0 inputGrade = 0 average = 0

19 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } Enter the grade 67 gradeCounter = 0 inputGrade = 67 average = 0 Total = 0

20 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } gradeCounter = 0 inputGrade = 67 average = 0 Total = 67

21 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } gradeCounter = 1 inputGrade = 67 average = 0 Total = 67

22 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } gradeCounter = 1 inputGrade = 67 average = 0 Total = 67

23 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } gradeCounter = 1 inputGrade = 67 average = 0 Total = 67

24 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } gradeCounter = 1 inputGrade = 78 average = 0 Total = 67 Enter the grade 78

25 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } gradeCounter = 1 inputGrade = 78 average = 0 Total = 145

26 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } gradeCounter = 2 inputGrade = 78 average = 0 Total = 145

27 Lecture 4: C/C++ Control Structures #include void main (void) { clrscr(); int total = 0, gradeCounter = 0, inputGrade = 0; float average = 0; while (gradeCounter <= 10) { printf (“Enter the grade = ”); scanf(“%d”,&inputGrade); total = total + inputGrade; gradeCounter = gradeCounter + 1; } average = total/10; printf (“The average is %f”,average); getch (); } gradeCounter = 2 inputGrade = 78 average = 0 Total = 145


Download ppt "Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4."

Similar presentations


Ads by Google