1 / 14

Control statements in C language Lecture-6

King Fahd University of Petroleum and Minerals Affiliated Colleges at Hafr Al-Batin (ACHB / HBCC )  Computer Science & Engineering Technology Unit (CSET). CSET101: Computer programming. Control statements in C language Lecture-6. Selection Structures.

omar-sexton
Download Presentation

Control statements in C language Lecture-6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. King Fahd University of Petroleum and Minerals Affiliated Colleges at Hafr Al-Batin (ACHB / HBCC )  Computer Science & Engineering Technology Unit (CSET) CSET101: Computer programming Control statements in C language Lecture-6

  2. Selection Structures • The if selection structure can be used in three different ways: • A simple ifstructure • The if / elsestructure • Nested if / elsestructures

  3. if • A simple if structure is called a single-selection structure because it either selects or ignores a single action. • Syntax for the if selection structure is as follows: if ( this logical expression is true ) statement ; • The if selection structure is often written as: if ( this logical expression is true ) statement ; no semi-colon!

  4. if / else • The if / else structure is called a double-selection structure because it selects between two different actions. • Syntax for the if / else selection structure : if ( this logical expression is true ) statement ; else statement ; • if / else selection structure is often written as: if ( this logical expression is true ) statement ; else statement ; no semi-colon!

  5. if / else if / else • Nested if / else structures test for multiple cases by placing if / else structures inside other if / else structures. • Syntax for the if / else if / else selection structure is as follows: if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else statement ;

  6. if / else if / else • The actual syntax for the multiple if / else if / else selection structure is as follows: if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else if ( this logical expression is true ) statement ; else statement ;

  7. if / else if / else • Curly brackets {} can be used to put one or more statements in a block. if ( this logical expression is true ) { Execute statements in this block ; } else if ( this logical expression is true ) { Execute statements in this block ; } else { Execute statements in this block ; }

  8. Example 1 • One way of writing if/else: #include <stdio.h> main ( ) { int a = 1, b = 2, c ; if (a > b) c = a; else c = b; } • Another way of writing if/else: #include <stdio.h> main ( ) { int a = 1, b = 2, c ; if (a > b) c = a ; else c = b ; }

  9. Example 2 // a program to compare two numbers #include <stdio.h> main ( ) { int a , b ; printf ("Enter values for a and b > ") ; scanf ("%d%d", &a, &b ) ; if ( a < b ) printf ("a is less than b \n") ; else if ( a == b ) printf (" a is equal to b \n") ; else printf ("a is larger than b \n") ; }

  10. Example 3 /* Grading program using if / else if / else.This program associates letter grades with numeric test scores */ #include <stdio.h> int main ( ) { int score ; printf (“Enter your test score >") ; scanf ("%d", &score) ;

  11. Cont… if (score >= 90) printf ("Your score of %d is grade A \n", score) ;   else if (score >= 80) printf ("Your score of %d is grade B \n", score) ;   else if (score >= 70) printf ("Your score of %d is grade C \n", score) ;   else if (score >= 60) printf ("Your score of %d is grade D \n", score) ;   else printf ("Your score of %d is grade F \n", score) ; }

  12. The switch Statement • This is another form of the multi way decision. It is well structured, but can only be used in certain cases where; • Only one variable is tested, all branches must depend on the value of that variable. The variable must be an integral type. (int, long, short or char). • Each possible value of the variable can control a single branch. A final, catch all, default branch may optionally be used to trap all unspecified cases.

  13. The switch Statement • The syntax of switch statement is as follows: switch(<expression>) { case <constant expression> : <statements> break; … … … default: <statements> }

  14. Example of switch #include <stdio.h> void main (void) { float n1, n2; int option; printf("Please enter two real numbers \n"); scanf("%f %f", &n1, &n2); printf("Available options:\n 1. add \n 2. subtract \n 3. multiply \n 4. divide \n "); printf("Enter your choice ... "); scanf("%d", &option); switch (option) { case 1: printf("Answer = %f \n", n1 + n2); break; case 2: printf("Answer = %f \n", n1 - n2); break; case 3: printf("Answer = %f \n", n1 * n2); break; case 4: printf("Answer = %f \n", n1 / n2); break; default: printf("Error: You have input a wrong choice"); } } A sample OUTPUT of the program:

More Related