Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.

Similar presentations


Presentation on theme: "Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura."— Presentation transcript:

1 Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Place photo here

2 Objectives To understand the principles of control structures. To be able to use the if selection statement and if…else selection statement to select actions. The Outcomes: Students should be able to understand the control structures. Student should be able to Use control structures correctly. Control Structures:

3 Conditional Constructs There are different types of conditional constructs. If statement (if statement, if-else statement and Nested if- else structure) Switch statement.

4 Types of Conditional Constructs: StatementDescription if statement An if statement consists of a Boolean expression followed by one or more statements. if...else statement An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. nested if-else statements You can use one if or else if statement inside another if or else if statement(s). switch statement A switch statement allows a variable to be tested for equality against a list of values.

5 The Basic If Structure: The syntax of an if statement in C programming language is: if(expression) { /* statement(s) will execute if the expression is true */ }

6 The Basic If Structure: Example

7 The Basic If Structure: Example 1: Write a C program to print the number entered by user only if the number entered is negative. #include int main(){ int num; printf("Enter a number to check.\n"); scanf("%d",&num); if(num<0) { /* checking whether number is less than 0 or not. */ printf("Number = %d\n",num); } /*If test condition is true, statement above will be executed, otherwise it will not be executed */ printf("The if statement in C programming is easy."); return 0; }

8 The Basic If Structure: Example 2: Write a C program to check if the value a is less than 20 or not. #include int main () { /* local variable definition */ int a = 10; /* check the boolean condition using if statement */ if( a < 20 ) { /* if condition is true then print the following */ printf("a is less than 20\n" ); } printf("value of a is : %d\n", a); return 0; }

9 The Basic If Structure: Example 3: Write a C program to sort two numbers entered by user. #include int main(void) int num1,num2; int remember_num1; printf("Enter two numbers: "); scanf{"%d %d", &num1,&num2); if(num1 > num2) { Remember_num1 = num1; num1 = num2; num2 = remember_num1; } printf("The input in sorted order: %d and %d\n.", num1,num2); return 0;}

10 The If-Else Structure: The syntax of an if...else statement in C programming language is: if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } else { /* statement(s) will execute if the boolean expression is false */ }

11 The If-Else Structure:

12 Example 1: Check if a given number is Zero or Not. #include int main(){ int value; printf("Enter a number you want to check.\n"); scanf("%d",& value); if(value==0) //checking the value is 0 or not. printf("value is 0"); else printf("value is not 0"); return 0; }

13 The If-Else Structure: Example 2: Check if a given number is Even or Odd. #include int main(){ int num; printf("Enter a number you want to check.\n"); scanf("%d",&num); if((num%2)==0) //checking whether remainder is 0 or not. printf("%d is even.",num); else printf("%d is odd.",num); return 0; }

14 The If-Else Structure: Example 3: Finding the Max between two integers #include int main() { int a, b; printf("\nEnter value of a & b : "); scanf("%d %d", &a, &b); if (a > b) printf("\n a is Max"); else printf("\n b is Max "); return(0); }

15 The If-Else Structure: Example 4: C Program to Accept two Integers and Check if they are Equal. #include void main() { int m, n; printf("Enter the values for M and N\n"); scanf("%d %d", &m, &n); if (m == n) printf("M and N are equal\n"); else printf("M and N are not equal\n"); }

16 Selection : There are two major ways of accomplishing this choice: Nested if structure if-else statements "glued" together Switch statement

17 The syntax for Nested if structure: The nested if...else statement is used when program requires more than one test expression. if ( condition1 ) statement1 ; else if ( condition2 ) statement2 ;... else if ( condition-n ) statement-n ; else statement-e ;

18 The syntax for Nested if structure: Example 1:Write a C program to relate two integers entered by user using = or > or < sign. # include int main(){ int numb1, numb2; printf("Enter two integers to check\n"); scanf("%d %d",&numb1,&numb2); if(numb1==numb2) //checking whether two integers are equal. printf("Result: %d = %d",numb1,numb2); else if(numb1>numb2) //checking whether numb1 is greater than numb2. printf("Result: %d > %d",numb1,numb2); else printf("Result: %d > %d",numb2,numb1); return 0; }

19 The syntax for Nested if structure: Example 2: Write a C program to find largest number among three numbers entered by user using nested if...else statement. #include int main(){ float a, b, c; printf("Enter three numbers: "); scanf("%f %f %f", &a, &b, &c); if(a>=b && a>=c) printf("Largest number = %.2f", a); else if(b>=a && b>=c) printf("Largest number = %.2f", b); else printf("Largest number = %.2f", c); return 0; }


Download ppt "Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura."

Similar presentations


Ads by Google