150 likes | 255 Views
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.
E N D
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 • The if selection structure can be used in three different ways: • A simple ifstructure • The if / elsestructure • Nested if / elsestructures
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!
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!
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 ;
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 ;
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 ; }
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 ; }
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") ; }
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) ;
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) ; }
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.
The switch Statement • The syntax of switch statement is as follows: switch(<expression>) { case <constant expression> : <statements> break; … … … default: <statements> }
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: