1 ICS 2175 Lecture 7 Control Structures
1 ICS 2175 Lecture 7 Control Structures
Methodology
ICS 2175
11/18/24
Basically, program statements are executed
in the sequence in which they appear in the
program
However, we can control the execution of C
program by using any control mechanism by
which we can compare things and come to a
decision.
This involves using some operations called
Relational Operators, logical operators,
conditional statements called if-else and
loops..
These relational operators compare two
values and return true or false after
comparison.
Control Structures
11/18/24
These may determine which actions to take
(subsequent statements to be executed)
depending on the outcome of the test
We can use the relational operators to
compare values of any basic data type, so all
we need is to modify the behavior of the
program.
A group of statements in a program may also
have to be executed repeatedly until some
condition is satisfied. This is called looping
Control Structures
11/18/24
< Less than
> Greater than
== Equal to
<= Less than or equal to
>= Greater than or equal to
!= Not equal to
Conditional Operators
11/18/24
Selection
11/18/24
The decision control structure in C can
be implemented using;
1. The if statement
2. The if - else statement
3. Multiple choice: else if statement
4. The switch and break statements
5. The continue statement
6. The goto statement
Selection
11/18/24
The if statement by itself provides a junction
at which the program has to select which
path to follow. The general form is;
if(expression)
statement;
If the expression is true (i.e. non zero), the
statement is executed, otherwise it is
skipped.
Normally the expression is a relational
expression that compares the magnitude of
two quantities (e.g. x > y or c==6)
Examples:
The If Statement
11/18/24
(i) If (x < y)
printf (“x is less than y”);
(ii) if (salary > 500)
tax-amount = salary * 1.5;
(iii) if (balance < 1000 || status = ‘R’)
printf(“Balance = %f”, balance);
The statement in the if structure can be a single
statement or a block (compound statement). If it is
a block it must be marked off by braces.
Example:
if(salary>5000)
{
tax_amt = salary * 1.5;
printf(“Tax charged is %f”, tax_amt);
}
The If Statement
11/18/24
Allows the programmer to choose between two
statements as opposed to the simple if
statement that gives the choice of executing a
statement or skipping it.
The general form is;
if (expression)
statement1;
else
statement2;
If the expression is true, statement1 is executed,
if false, the single statement following the else
(statement2) is executed. The statements can
be simple or compound
N/B: the indentation is not compulsory but is a
standard style of programming
Explanation
11/18/24
#include<stdio.h>
main()
{
int m1,m2,m3,m4,m5;
float total, average;
char grade;
printf("Enter marks for Database systems, computer Maths,
chemistry, biology, and physics\n");
printf("Press enter after each mark\n");
scanf("\n%d%d%d%d%d",&m1, &m2,&m3,&m4,&m5);
total =(float)(m1+m2+m3+m4+m5);
average=(float)(m1+m2+m3+m4+m5)/5;
printf("The total marks for the student is %.2f\n",total);
printf("The average mark is %.2f\n",average);
//Computation of grades
if (average>70)
grade='A';
else if (average>60)
grade='B';
else if (average>50)
grade='C';
else if (average>40)
grade='D';
else
grade='F';
printf("The student's grade is %c\n", grade);
}
11/18/24
#include<stdio.h>
main()
{
int m1,m2,m3,m4,m5;
float total, avg;
char grade;
int av;
printf("Enter the marks for Database systems->");
scanf("%d",&m1);
printf("Enter the marks for Computer Mathematics->");
scanf("%d",&m2);
printf("Enter the marks for Networking Essentials->");
scanf("%d",&m3);
printf("Enter the marks for Principles of Computer support and Maintenance->");
scanf("%d",&m4);
printf("Enter the marks for Programming Methodology->");
scanf("%d",&m5);
total =(float)(m1+m2+m3+m4+m5);
avg=(float)(m1+m2+m3+m4+m5)/5;
printf("The total marks for the student is %.2f\n",total);
printf("The average mark is %.2f\n",avg);
av=(avg/10);
switch (av)
case 5:
{
case 0: case 1: case 2: case 3: grade='C';
grade='F'; break;
break; case 6:
case 4: grade='B';
grade='D'; break;
break; default:
} grade='A';
printf("The student's grade is %c\n", grade); break;
return 0;
} 11/18/24
Like the break statement, this statement
is a jump that interrupts the flow of a
program. It is used in loops to cause the
rest of an iteration to be skipped and the
next iteration to be started
If a break is used in a loop it quits the
entire loop
Exercise
11/18/24
The most common statement used for looping in C
The general form is;
for (expression1; expression2; expression3)
statement;
Expression1 is used to initialize a parameter
(called an index). This index controls the loop
action. It is usually an assignment operator
expression2 is a test expression, comparing the
initialized index in expression1 to some maximum
or minimum value
Expression3 is used to alter the value of the
parameter index initially assigned by expression
and is usually a unary expression or assignment
operator
for (i=10;i>=1;i--)
printf("%3d",i);
printf("\n\n");
The ‘for’ Loop – Example 2
return 0;
}
11/18/24