chapter2_4
chapter2_4
2
3.1. C control structures
• Selection
if
if . . . else
switch
• Repetition
for loop
while loop
do . . . while loop
3
Selection
• if statement
• if … else statement
• switch statement
4
If statement
if (expression) statement
If the condition is true (value of expression is 1 or nonzero)
the statement is executed
5
If … else statement
6
Condition: expression
6 Relational Operators
< <= > >= == !=
3 Logical Operators
! && ||
7
Example 1
8
Program
1. #include <stdio.h>
2. #include <math.h>
3. main()
4. {
5. float a,b,c,s,A;
6. printf("\nEnter three sizes of the triangle, a,b,c:");
7. scanf("%f %f %f", &a,&b,&c);
8. if (a>0 && b>0 && c>0 && a+b>c && a+c>b && b+c>a)
9. {
10. s=(a+b+c)/2;
11. A= sqrt(s*(s-a)*(s-b)*(s-c));
12. printf ("\nArea of the triangle: %1.2f",A);
13. }
14. else
15. printf("\nNot a triangle!");
9
16. }
Example 2
10
Example 3
11
Program
1. #include <stdio.h>
2. #include<conio.h>
3. #include <math.h>
4. main()
5. { float a, b, c, delta, x1,x2;
6. puts("Enter coefficients a b c : ");
7. scanf("%f%f%f", &a, &b, &c);
8. if( a==0)
9. {printf("Equation is not quadratic.Become linear equation %fx+%f=0",b,c);
10. if(b==0)
11. if (c!=0)puts ("Inconsistent liner equation. No root.");
12. else puts ("Every number is a root of the equation!");
13. else printf ("\nThe unique root of linear equqtion: x= %f",-c/b);
14. }
15. else
16. {delta=b*b-4*a*c;
17. if (delta < 0)
18. puts("");
19. else
20. if (delta == 0)
21. {x1= -b/(2*a);
22. printf("Quadratic equation has unique root x = %f", x1);
23. }
24. else
25. {x1=(-b + sqrt(delta))/(2*a);
26. x2=(-b - sqrt(delta))/(2*a);
27. printf("Quadratic equation has 2 roots\n x1=%f \n x2=%f",x1,x2 );
28. }
29. } 12
30. }
User screen
13
The switch statement
switch (expression)
{
case value1: statements_1
case value2: statements_2
/* more cases */
[default: default_statements]
}
14
The switch statement
Compute value of
expression
F F
exp=Value 1 exp=Value 2 default ? F
T T T
Statements1 Statements2 Statementsn+1
15
Example 1
16
Program and user screen
1. #include <stdio.h>
2. main(){
3. int a;
5. switch(a) {
14. }
15. }
17
Homework
18
4.3.Repetition
• while statement
• do…. while statement
• for statement
• Loop interruption: break and continue
19
while statement
20
Find the largest integer that satisfies 3n5-317n <
5
1. #include <stdio.h>
2. #include <math.h>
3. main()
n = 10
4. { while (3*pow(n,5)-317*n >= 5)
5. int n=0; n--;
6. while (3* pow(n,5) - 317*n < 5) n++;
7. printf("%4d",n-1);
8. }
n= 3
21
Find the output of the program
1. #include <stdio.h>
2. main()
3. {
4. int i=3;
5. while (i > 1){
6. if(i % 2==0) i = i / 2;
7. else i = i * 3 + 1;
8. printf("%4d",i);
9. }
10.}
10 5 16 8 4 2 1
22
do … while statement
23
Example 1
24
Use do…while loop
1. main(){
2. float mark;
3. do
4. {printf("Input a mark between 0 and 10:");
5. scanf("%f",&mark);
6. if (mark < 0 || mark > 10)
7. printf("\nThe entered mark is invalid
!\n");
8. }
9. while (mark < 0 || mark > 10);
10. printf("\nThe entered mark is: %.2f",mark);
11.}
25
Use do while loop
26
Use while loop
1. #include <stdio.h>
2. main(){
3. float mark;
4. printf("Input a mark between 0 and 10:");
scanf("%f",&mark);
5. while (mark < 0 || mark > 10) {
6. printf("\nThe entered mark is invalid
!\n");
7. printf("Enter the mark
again(0<=mark<=10):");
8. scanf("%f",&mark);
9. }
10. printf("\nThe entered mark is: %.2f",mark);
11.} 27
Use while loop Result
28
for statement
29
Use cases of for loop
int i;
for(i = 0; i < 100; i++) statement;
int i;
for(i = 0; i < 100; i+=2) statement;
int i;
for(i = 100; i > 0; i--) statement;
30
Example 1: Print odd numbers less than 100
1. #include <stdio.h>
2. main(){
3. int i;
4. for(i = 1;i<100;i++) {
5. if(i%2 == 1) printf("%5d",i);
6. if((i+1)%20 ==0) printf("\n");
7. }
8. }
31
Example 2: Print odd numbers less than 100 in descending order
1. #include <stdio.h>
2. main(){
3. int i;
4. for(i = 99;i > 0;i-=2) {
5. printf("%5d",i);
6. if( (i-1) % 20 == 0) printf("\n");
7. }
8. }
32
Example 3: Input number n and print its factorial
1. main()
2. { long P = 1;
3. int n, i;
4. printf("Input n : ");scanf("%d",&n);
5. for(i = 1;i<=n;i++)
6. P = P * i;
7. printf("%d!= %ld",n,P);
8. }
33
Example 4: Compute sum of harmonic series
• The harmonic series is: 1 +1/2+…+1/n. n is entered from
keyboard
1. #include <stdio.h>
2. main()
3. {
4. float S = 0.0;
5. int n, i;
6. printf("Input n : ");scanf("%d",&n);
7. for(i = 1;i <= n;i++)
8. S = S + (float)1/i; //S+=1.0/i;
9. printf("Sum of harmonic series: %7.4f ",S);
10.}
34
Example 5: Find Amstrong numbers of 3 digits
36
Loop Interruption: break and continue
• break
The break statement skips any statements between the break
and the bottom of the loop; the break statement ends the loop
and control resumes with the first statement following the
loop.
• continue
The continue statements skips any statements between the
continue and the bottom of the loop; the next iteration of the
loop begins (including the update statement in the case of a for
loop).
37
Continue vs break
38
Example 1
40
Example 3
#include <stdio.h>
main()
{
int i;
for(i = 1;i<=10;i++)
{
if(i == 5) continue;
printf(“%5d”,i);
if(i==7) break;
}
}
41
Example 4
#include <stdio.h>
main()
{ int i,j;
for(i = 0;i<10;i++) {
for (j=0; j < 10; j ++) {
if(j > i){
break;
}//if
}//for _ j
printf("i:%d j:%d\n",i,j);
}//for_i
}
42