1. Control Structure
• Sequence structures
• Built into C
• 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
2. Sequence Structure
Sequence Structure
• Programs executed sequentially by default
Control flow:
• Statements executed one after the other in the order written
statement 1
statement 2
Sequence Structure
3. The if Selection Structure
False
Print “Passed”
grade >= 60
True
if Selection Structure
if (grade>=60)
printf(“Passed”);
0(zero):false
1(nonzero):true
6. Nested Selection Structure
Nested if-else Structure
if (grade>=90)
printf(“A”);
else
if (grade>=80)
printf(“B”);
else
if (grade>=80)
printf(“C”);
else
if (grade>=60)
printf(“D”);
else
printf(“E”);
if (grade>=90)
printf(“A”);
else if (grade>=80)
printf(“B”);
else if (grade>=80)
printf(“C”);
else if (grade>=60)
printf(“D”);
else
printf(“E”);
7. Compound Statement Nested In if/else
if (grade>=60)
printf(“Passed”);
else {
printf(“Failedn”);
printf(“You must take the course again!”);
}
Two lines
10. Logical Calculation
a !a
1 0
0 1
a b a&&b
1 1 1
1 0 0
0 1 0
0 0 0
a b a||b
1 1 1
1 0 1
0 1 1
0 0 0
!
&&
||
11. Use Of Logical Expression
void main() {
char c;
c=getchar();
if ((c>=‘A’&&c<=‘Z’)||(c>=‘a’&&c<=‘z’))
printf(“%c is a letter.”, c);
else if (c>=‘0’&&c<=‘9’)
printf(“%c is a digit.”, c);
else
printf(“%c is neither a letter nor a digit.”, c);
}