Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department...
-
Upload
catherine-mason -
Category
Documents
-
view
214 -
download
0
Transcript of Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department...
![Page 1: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/1.jpg)
Chapter 6: Control Structures
Computer Programming Skills
4800153-3Second Term 1435-
1436
Department of Computer ScienceFoundation Year ProgramUmm Alqura University, Makkah
Place photo here
![Page 2: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/2.jpg)
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:
![Page 3: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/3.jpg)
Conditional Constructs
There are different types of conditional constructs.
• If statement (if statement, if-else statement and Nested if-else structure)
• Switch statement.
![Page 4: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/4.jpg)
Types of Conditional Constructs:
Statement Description
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 statementsYou can use one if or else if statement
inside another if or else if statement(s).
switch statementA switch statement allows a variable to be
tested for equality against a list of values.
![Page 5: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/5.jpg)
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 */
}
![Page 6: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/6.jpg)
The Basic If Structure:
Example
![Page 7: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/7.jpg)
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 <stdio.h>
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;
}
![Page 8: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/8.jpg)
The Basic If Structure:
Example 2: Write a C program to check if the value a is less than 20 or not.
#include <stdio.h>
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;
}
![Page 9: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/9.jpg)
The Basic If Structure:
Example 3: Write a C program to sort two numbers entered by user.#include <stdio.h>
#include <stdlib.h>
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;}
![Page 10: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/10.jpg)
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 */
}
![Page 11: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/11.jpg)
The If-Else Structure:
![Page 12: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/12.jpg)
The If-Else Structure:
Example 1: Check if a given number is Zero or Not.
#include <stdio.h>
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;
}
![Page 13: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/13.jpg)
The If-Else Structure:
Example 2: Check if a given number is Even or Odd.
#include <stdio.h>
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; }
![Page 14: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/14.jpg)
The If-Else Structure:
Example 3: Finding the Max between two integers #include<stdio.h>
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); }
![Page 15: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/15.jpg)
The If-Else Structure:
Example 4: C Program to Accept two Integers and Check if they are Equal.
#include <stdio.h>
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");
}
![Page 16: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/16.jpg)
Selection:
There are two major ways of accomplishing this choice:
• Nested if structure
if-else statements "glued" together
• Switch statement
![Page 17: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/17.jpg)
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 ;
![Page 18: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/18.jpg)
The syntax for Nested if structure:
Example 1:Write a C program to relate two integers entered by user using = or > or < sign.
#include <stdio.h>
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; }
![Page 19: Chapter 6: Control Structures Computer Programming Skills 4800153-3 Second Term 1435-1436 Department of Computer Science Foundation Year Program Umm Alqura.](https://reader036.fdocuments.us/reader036/viewer/2022082820/56649efd5503460f94c114c5/html5/thumbnails/19.jpg)
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 <stdio.h>
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;
}