Upload
catherine-mason
View
214
Download
0
Embed Size (px)
Citation preview
Chapter 6: Control Structures
Computer Programming Skills
4800153-3Second Term 1435-
1436
Department of Computer ScienceFoundation Year ProgramUmm Alqura University, Makkah
Place photo here
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:
Conditional Constructs
There are different types of conditional constructs.
• If statement (if statement, if-else statement and Nested if-else structure)
• Switch statement.
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.
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 */
}
The Basic If Structure:
Example
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;
}
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;
}
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;}
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 */
}
The If-Else Structure:
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;
}
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; }
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); }
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");
}
Selection:
There are two major ways of accomplishing this choice:
• Nested if structure
if-else statements "glued" together
• Switch statement
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 ;
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; }
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;
}