Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Control

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 the expression is true statement1 is


executer, otherwise statement2 is
executed
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 the expression is true statement1 is


executer, otherwise statement2 is
executed
if-then-else Example
int finalGrade;

if(finalGrade >= 45)
printf("Pass \n");
else
printf("Fail \n");

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

const int maxTimes = 20;


int i = 0;

printf(“How do you like C programming?\n”);
while(i < maxTimes) {
printf(“Programming is fun!\n”);
i++;
}
do while Statement
• The do while loop is similar to the while,
but the test occurs after the loop body is
executed
• This ensures that the loop body is run at
least once
• Syntax:

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

char option = ‘’;



printf(“Select an option: \n”);
printf(“(a) Calculate grades \n”);
printf(“(b) Calculate class average \n”);
printf(“(c) Print grades \n”);
printf(“(q) Quit \n”);
do {
option = getchar();

}
while((option == ‘q’) || (option == ‘Q’));
What are the differences between
the while and the do while
statements?

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:

for(initializing list; expression; altering list) {


statement1;
statement2;

}
for Example

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

You might also like