C-Control Structures - Examples I
C-Control Structures - Examples I
Control Sttructures
◼ All programs written in terms of 3 control structures:
Sequence structure
◼ Programs executed sequentially by default.
Selection structures
◼ C has three types - if, if/else, and switch
Repetition structures
◼ C has three types - while, do/while, and for
Sequence Structure Example
Sequence Structure Example
◼ Write a C program to calculate the average of three
integers entered by user.
# include <stdio.h>
int main()
{ int x, y, z;
int sum=0;
double avg;
printf("Enter three integers \n");
scanf("%d %d %d",&x,&y, &z);
sum=x+y+z;
avg= sum/3.0;
printf(”The average value is %.2f",avg);
return 0;
}
Selection Structure
◼ Three kinds of selections structures
if (also called, ‘single-selection’)
◼ if condition is true
Perform action
◼ if condition is false, action is skipped, program continues
if/else (also called, ‘double-selection’)
◼ if condition is true
Perform action
◼ else (if condition is false)
Perform a different action (this will be skipped if condition is true)
switch (also called ‘multiple-selection’)
◼ Allows selection among many actions depending on the
integral value of a variable or expression
IF statement example
IF statement example
◼ Write a C program to check whether a
number entered by user is positive
# include <stdio.h>
int main()
{ int num;
printf("Enter a number you want to check.\n");
scanf("%d",&num);
if(num > 0) //checking whether number is positive
printf("%d is positive.",num);
return 0;
}
IF-ELSE statement - Double Selection
◼ Syntax
if(expression) /* if expression is TRUE (i.e., NOT EQUAL to zero) */
statement1; /* execute this statement */
else /* else execute the following statement */
statement2;
Notes:
If expression is non-zero, statement1 is executed, then the
program continues with the statement after statement2,
i.e., statement2 is skipped
If expression is equal to zero, statement1 is skipped and
statement2 is executed, then the program continues with
the statement after statement2
IF-ELSE statement example
IF-ELSE statement example
◼ Write a C program to check whether a number
entered by the user is negative or positive
# include <stdio.h>
int main()
{ int num;
printf("Enter a number you want to check.\n");
scanf("%d",&num);
if(num < 0) //checking whether number is negative
printf("%d is negative.",num);
else
printf("%d is negative.",num);
return 0;
}
IF-ELSE statement example
IF-ELSE statement example
◼ Write a C program to check whether a number
entered by user 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;
IF-ELSE statement example
IF-ELSE statement example
◼ Write a C program to determine the maximum between
two integers entered by user.
# include <stdio.h>
int main()
{ int numb1, numb2;
printf("Enter two integers to check\n");
scanf("%d %d",&numb1,&numb2);
if(numb1>numb2)
printf(”The maximum value is %d",numb1);
else
printf(" The maximum value is %d",numb2);
return0;
}
SWITCH - Flowchart
SWITCH
◼ Good when faced with testing multiple alternatives
that depend on a single variable
The test is done once
◼ Must be an integral expression
int or char
NOT float, double
case items must be constant integral expressions
◼ No variables
The structure is very organized and readable
SWITCH Example
SWITCH Example
# include <stdio.h>
int main() {
char operator;
float num1,num2;
printf("Select an operator either + or - or * or / \n");
scanf("%c",&o); printf("Enter two operands: "); scanf("%f%f",&num1,&num2);
switch(operator) {
case'+’:
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case'-’:
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case'*’:
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case'/’:
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);
break;
case’%’:
printf("%.1f % %.1f = %d ",num1, num2, num1%num2);
break;
default:
printf(”invalid entry");
break; }
return 0;
}
Repetition Structure
◼ Often need to repeat an action or calculation
◼ Repetition structures are often called ‘loops’.
while tests a condition at the beginning of the loop
◼ condition must first be true for the loop to run even once
do/while tests a condition at the end of the loop
◼ loop will run at least once
for facilitates initializing and incrementing the
variable that controls the loop
◼ Especially helpful for:
Looping for a known number of times
Loops that count or that need to increment a variable
Loop Example
For Loop Example
◼ Write a C program to find the Factorial of a Number
using For Loop.
#include <stdio.h>
int main()
{ int i, n;
int fac = 1;
printf("Please Enter any number to Find Factorial = ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fac = fac * i;
printf("\nFactorial of %d = %d\n", n, fac);
return 0;
While Loop Example
◼ Write a C program to find the Factorial of a Number
using while Loop.
#include <stdio.h>
int main()
{ int Num, i = 1;
int Fc = 1;
printf("\n Please Enter any value \n");
scanf("%d", &Num);
while (i <= Num)
{ Fc = Fc * i;
i++; }
printf("Factorial of %d = %d\n", Num, Fc);
Do-While Loop Example
◼ Write a C program to find the Factorial of a Number
using do-while Loop.
#include<stdio.h>
int main()
{ int n,i=1,f=1;
printf("\n Enter The Number:");
scanf("%d",&n);
do
{
f=f*i;
i++;
}while(i<=n);
printf("\n The Factorial of %d is %d",n,f);
return 0;