Lecture 3
Lecture 3
CONTROL STRUCTURES IN C
Note:
We can see that instead of num, &num is passed as 2nd
argument to scanf() function.The unary & here passes
the address of num to the scanf() function. This is called
pass by reference
Exercise 1.
Take 2 numbers as input from user, add
them and print the result back to user.
What to cover Today
Review control structures
Sequence
Selection
Repetition
Learning Objectives
Explain the three basic types of control
structures
Explain the selection structure and it types
Explain how the three kinds of repetition
structures work
Apply the concept of repetition control
structures to practical problems
CONTROL STATEMENTS
In the program codes we have studied so far, you would
observe most of the instructions are executed once in the order
in which they appear in the program.
SEQUENCE STRUCTURE
SELECTION STRUCTURE (3 TYPES: if, if-else, switch)
REPTITION STRUCTURE (3 TYPES: while, do-while, for)
1. #include <stdio.h>
2. int main (void){
3. int score;
4. printf("Enter students score\n");
5. scanf("%d", &score);
6. if(score>=41) {
7. printf("pass score: congrats\n");
8. }
9. return 0;
10. }
CONTROL STATEMENTS: SELECTION STRUCTURE (IF :Contd)
1. #include <stdio.h>
2. int main (void){
3. int first,second;
4. printf("Enter number now\n");
5. scanf("%d %d", &first, &second);
6. if((first>second)||(second>third)){
7. printf(“first is smallest of all\n");
8. }
9. return 0;
10. }
CONTROL STATEMENTS: SELECTION STRUCTURE (IF :Contd)
1. #include <stdio.h>
2. int main (void){
3. int first,second;
4. printf("Enter number now\n");
5. scanf("%d %d", &first, &second);
6. if((first<second)&&(second<third)){
7. printf(“third is greatest of all\n");
8. }
9. return 0;
10. }
Flowchart for IF Statement
connector
flow line
decision symbol action symbol
TRUE
Speed > 65 Print “You’re
speeding!”
FALSE
CONTROL STATEMENTS: SELECTION STRUCTURE
(IF –ELSE)
1. #include <stdio.h>
2. int main (void){
3. int score;
4. printf("Enter students score\n");
5. scanf("%d", &score);
6. if(score>=41) {
7. printf("pass score: congrats\n");
8. }
9. else{
10. printf(“fail score: see you next year\n");
11. }
12. return 0;
13. }
CONTROL STATEMENTS: SELECTION STRUCTURE
(IF –ELSE)
1. #include <stdio.h>
2. int main (void){
3. int num1,num2;
4. scanf("%d%d", &num1,&num2);
5. if(num1<num2) {
6. printf(“num1 is smaller\n");
7. }
8. else {
9. printf(“num1 is bigger\n");
10. }
11. return 0;
12. }
IF-ELSE FLOWCHART (Double-Selection)
FALSE TRUE
Print “Within Speed > 65 Print “Over speed
limit” limit!”
CONTROL STATEMENTS: SELECTION STRUCTURE
(IF –ELSE)
1. //sum of two numbers using if else
2. #include <stdio.h>
3. int main (void){
4. int num1,num2,total;
5. scanf("%d%d", &num1,&num2);
6. if(num1==num2) {
7. total=num1*2;
8. printf(“total is equal to\n”,total);
9. }
10. else {
11. total=num1+num2;
12. printf(“total is equal to\n“,total);
13. }
14. return 0;
15. }
/* Compute Area and Perimeter of a circle */
#include <stdio.h>
float pi = 3.14159; /* Global */
main() {
float rad; /* Local */
•case 1
•If (expression1) if (expression2) statement1
else statement2
else if (expression 3) statement3
else statement4
• case 2
• if (expression2) statement1
else if (expression2) statement2
•Case 3
if (expression 1) statement1
else if (expression2) statement4
else statement5
}
}
CONTROL STATEMENTS: SELECTION STRUCTURE