Flow control in C language

advertisement
if () else statement, switch statement,
while () loop, do…while() loop
and
for( ; ; ) loop
1
if() and if()…else statement
In C language the using structure to write any condition by if() is:
 In C language the using
structure to write any
condition by if() is:
 Syntax:
if (condition)
{
statements to execute;
}
Syntax:
if (condition)
{
statements to execute;
}
else
{
statements to execute;
}
2
Continue:
Example: Write a C program to solve this problem:
enter a student number and his grade, then print
classification according the following values:
grade < 60
then print “ fail”
60 <= grade < 70
then print “ pass”
70 <= grade < 80
then print “ good”
80 <= grade < 90
then print “ very good”
90 <= grade <=100
then print “ excellent”
3
#include<stdio.h>
void main()
{
int id;
float grade;
printf(“enter student number: ”);
scanf(“%d”, &id);
printf(“enter student grade: ”);
scanf(“%f”, &grade);
if (grade <= 60)
printf(“%d grade is faile\n”, id);
if (grade >= 60) && (grade < 70)
printf(“%d grade is pass\n”, id);
if (grade >= 70) && (grade < 80)
printf(“%d grade is good\n”,id);
if (grade >= 80) && (grade < 90)
printf(“%d grade is very good\n”, id);
if (grade >= 90) && (grade <=100)
printf(“%d grade is excellent\n”, id);
}
4
#include<stdio.h>
void main()
{
int id;
float grade;
printf(“enter student number: ”);
scanf(“%d”, &id);
printf(“enter student grade: ”);
scanf(“%f”, &grade);
if (grade <= 60)
printf(“%d grade is faile\n”, id);
else
if (grade >= 60) && (grade < 70)
printf(“%d grade is pass\n”, id);
else
if (grade >= 70) && (grade < 80)
printf(“%d grade is good\n”,id);
else
if (grade >= 80) && (grade < 90)
printf(“%d grade is very good\n”, id);
else
if (grade >= 90) && (grade <=100)
printf(“%d grade is excellent\n”, id);
else
printf(“out of the range”);
}
5
Another example: write a program to print the name of a
day’s week (i.e. we have 7 days in a week numbered
from 0 to 6) according the following table:
0
Saturday
1
Sunday
2
Monday
3
Tuesday
4
Wednesday
5
Thursday
6
Friday
6
#include<stdio.h>
void main()
{
int n;
printf(“ Enter the day number: ”);
scanf(“%d”,&n);
if (n == 0)
printf(“\n Saturday”);
if (n == 1)
printf(“\n Sunday”);
if (n == 2)
printf(“\n Monday”);
if (n == 3)
printf(“\n Tuesday”);
if (n == 4)
printf(“\n Wednsday”);
if (n == 5)
printf(“\n Thursday”);
if (n == 6)
printf(“\n Friday”);
}
7
Switch() statement:
You can use switch() to represent if() not if () else.
It is very simple to chose one thing from a set of choices have the same type.
Syntax:
switch( var )
{
case v1: statement;
break;
case v2: statement;
break;
.
.
.
default : statement;
}
8
Example: solve the same last program by using switch.
#include<stdio.h>
void main()
{
int n;
printf(“ Enter the day number: ”);
scanf(“%d”,&n);
switch ( n )
{
case 0: printf(“\n Saturday”); break;
case 1: printf(“\n Sunday”); break;
case 2: printf(“\n Monday”); break;
case 3: printf(“\n Tuesday”); break;
case 4: printf(“\n Wednsday”); break;
case 5: printf(“\n Thursday”); break;
case 6: printf(“\n Friday”); break;
default: printf(“\n out of range ….”);
}
}
9
Ass. Write a program to print a menu on the output
screen. But your choices are character .
Using if() and switch() at each time.
Due next lecture.
10
Loops:
you can use loop when you have a number of iteration,
or you need to continue a such process until enter
special character (like Y or N) or number (like -1) to
exit. There are a different kind of loop in C program
like while(), do…while() and for()
11
While () loop
Syntax:
initialize variable;
while(condition)
{
statements;
increment or decrement variable;
}
12
Example: write a program to enter id_number of students and print what
you read until enter (N or n).
#include <stdio.h>
void main( )
{ char ch;
int id;
printf(“Do you want to enter a student numder?(Y\N)”);
scanf(“%c”,&ch);
while((ch !=‘N’)&&(ch !=‘n’))
{ printf(“\n id number is: ”);
scanf(“%d”, &id);
printf(“id= %d \n”,id);
printf(“Do you want to enter a student numder?(Y\N)”);
scanf(“%c”,&ch);
}
printf(“\n end the program”);
}
13
do … while() loop
Syntax:
initial_value;
do
{
statements;
increment or decrement;
} while(condition);
14
Ass. Write the same last program by using do…
while()
Due next lecture
15
for () loop
Syntax:
for (initial value; condition; increment or decrement variable)
{
statements;
}
16
Example: write a program to print this shape.
*
*
*
*
*
*
*
*
*
*
17
#include <stdio.h>
void main( )
{
int i, j, k, n;
printf(“\n enter number of lines: ”);
scanf(“%d”, &n);
for (i=1; i <= n; i++)
{
for(j=n; j > i; j--)
{
printf(“ ”);
}
for (k=1; k<= i; k++)
{
printf(“*”);
}
printf(“\n”);
}
}
18
Download