Professional Documents
Culture Documents
5 Control Statements in C
5 Control Statements in C
Statements in C
Start
if-then-else Statement
• Selects a sequence of one or more
instructions based on the result of a
comparison
• Syntax:
if (expression)
statement1;
[else
statement2;]
if (expression)
statement1;
[else
statement2;]
int homeworkIsDone;
int extraIsDone;
int homeworkGrade;
…
if(homeworkIsDone && extraIsDone)
homeworkGrade = homeworkGrade + 3;
if-then-else Example
#define TRUE 1;
#define FALSE 0;
int main() {
int homeworkIsDone;
int extraIsDone;
int homeworkGrade;
…
if((homeworkIsDone == TRUE) &&
(extraIsDone == TURE))
homeworkGrade = homeworkGrade + 3;
}
What happens if I need to execute
more than one statement?
int finalGrade;
…
if(finalGrade >= 45) {
printf("Pass \n");
printf(“Congratulations! \n");
}
else {
block printf("Fail \n");
printf(“Better luck next time \n");
}
Nested if Statements
• A mechanism that allows us to make a
multi-way decision based on several
conditions
• Syntax:
if (expression1)
statement1;
[else if (expression2)
statement2;
else if (expression3)
statement2;
…
else
statementN;]
Nested if Example
int finalGrade;
…
if(finalGrade >= 92)
printf(“Passed: Your grade is A \n”);
else if(finalGrade >= 83)
printf(“Passed: Your grade is A- \n”);
else if(finalGrade >= 75)
printf(“Passed: Your grade is B+ \n”);
else if(finalGrade >= 67)
printf(“Passed: Your grade is B \n”);
else if(finalGrade >= 58)
printf(“Passed: Your grade is B- \n”);
else if(finalGrade >= 50)
printf(“Passed: Your grade is C+ \n”);
else
printf(“Failed \n”);
switch Statement
• Selects a sequence of one or more instructions based
on the result of comparing the value of an integer
expression to a specific value
• Syntax:
switch(expression) {
case value1:
statement1;
…
[break;]
case value2:
statement2;
…
[break;]
default:
statementN;
…
[break;]
}
switch Example
char operator;
int result;
int value;
…
switch(operator) {
case ‘+’:
result += value;
break;
case ‘-’:
result -= value;
break;
case ‘*’:
result *= value;
break;
case ‘/’:
if(value == 0) {
printf(“Error: Divide by zero \n”);
printf(“ Operation ignored \n”);
}
else
result /= value;
default:
printf(“Unknown operator \n”);
break;
}
while Statement
• The while loop keeps repeating an action until an
associated test returns false
• Useful when the programmer does not know in
advance how many times the loop will be
traversed
• Syntax:
while(expression) {
statement1;
statement2;
statement3;
…
}
while Example
do {
statement1;
statement2;
statement3;
…
}
while(expression);
do while Example
const int maxTimes = 20;
int i = 0;
…
printf(“How do you like C programming?\n”);
do {
printf(“Programming is fun!\n”);
i++;
}
while(i < maxTimes);
do while Example
while do while
Entry control structure Exit control structure
Loop may or may not be Loop is executed at least
executed once
for Statement
• More frequently used
• Used when the loop needs to be
traversed a fixed number of times
• Syntax:
int count;
for(count = 1; count < = 10; count = count+1) {
printf("%d ",count);
}
printf("\n");
Printed output:
1 2 3 4 5 6 7 8 9 10