CSCI-1190: Beginning C
Programming for Engineers
Lecture 2: Logic, repetition,
iteration
Gang Chen
Reviewing Lecture 1
• printf ( %d, %f, \n )
• arithmetic operators (+,-,*,/,%,++,--)
• expressions
• variables ( integer, floating point number)
• scanf ( %d, %f, & )
• assignment (=)
Reviewing Lecture 1 (cont.)
1. #include <stdio.h>
2. int main()
3. {
4. float a,b,c;
5. printf("Please input the first number:\n");
6. scanf("%f",&a);
7. printf("Please input the second number:\n");
8. scanf("%f",&b);
9.
10. c=a*a+b*b;
11. printf("c*c = %f \n", c);
12. return 0;
13. };
Control Structure
• Sequential execution
• Branch
– if, if-else
– switch
• Repetition
– while
– do-while
• Iteration
– for
if Statement
1. #include <stdio.h>
2. int main()
3. {
4. int grade;
5. printf("Please input your grade:");
6. scanf("%d", &grade);
7. if( grade >= 60) /* boolean expression*/
8. printf("You passed the exam!\n");
9. return 0;
10. };
if-else Statement
1. if( grade >= 60)
2. { /* compound statement */
3. printf("You passed ");
4. printf("the exam!\n");
5. }
6. else
7. {
8. printf("You failed!\n");
9. }
Relational Operators
• Relational expressions == Equal
return 1 if the condition is
true, or 0 otherwise. != Not equal
• Relational operators have < Less than
a lower precedence than
arithmetic operators. <= Less than or
equal
• Don’t confuse == with =
– if( a==3 ) > Greater than
– if( a=3 )
>= Greater than or
equal
Nested if-else Statement
1. if( grade >= 90)
2. printf("You got an A!\n");
3. else if ( grade >= 80 )
4. printf("You got a B!\n");
5. else if ( grade >= 70 )
6. printf("You got a C!\n");
7. else if ( grade >= 60 )
8. printf("You got a D!\n");
9. else
10. printf("You failed!\n");
Logical Operators
Negation: ==1 if expr is 0, and 0 otherwise
!expr !0 == 1;
!2.0 == 0;
And: ==1 if both expr1 and exp2 are true
expr1&&expr2 2 && 3 == 1;
0 && 2 == 0;
Or: ==1 if either expr1 or expr2 are true
expr1||expr2 2 || 3 == 1;
0 || 3 == 1;
• Precedence: logical operators <relational
operators < arithmetic operators
Using Logical Operators
1. if( grade >= 90)
2. printf("You got an A\n");
3. if( grade >= 80 && grade < 90)
4. printf("You got a B\n");
5. if( grade >= 70 && grade < 80)
6. printf("You got a C\n");
7. if( grade >= 60 && grade < 70)
8. printf("You got a D\n");
9. if( grade < 60 )
10. printf("You failed!\n");
Logical Operators or Nested if-else
Statement?
if ( expr1 && expr2 ) if ( expr1)
A; { if ( expr2 ) A; else B;}
else else
B; B;
if ( expr1 || expr2 ) if ( expr1)
A; A;
else else
B; { if ( expr2 ) A; else B;}
In-Class Exercise 2-1
• Find the largest and smallest number
among three numbers.
– Nested if-else statement (minimum
comparisons)
– Logical operators (only if statement is
allowed; no else clause)
switch Statement
1. char grade; 18. case 'D':
2. printf("Please enter the letter grade:"); 19. case 'd':
3. scanf("%c",&grade); 20. printf("At least passed!\n");
4. switch (grade) 21. break;
5. { 22. case 'F':
6. case 'A': 23. case 'f':
7. case 'a': 24. printf(“Don’t worry!\n");
8. printf("Excellent!\n"); 25. break;
9. break; 26. default:
10. case 'B': 27. printf("I don't understand what you
11. case 'b': mean\n");
12. printf("Not bad!\n"); 28. break;
13. break; 29. }
14. case 'C':
15. case 'c':
16. printf("It is okay!\n");
17. break;
goto Statement
1. int grade;
2. loop: /* label */
3. printf("Please input your grade:");
4. scanf("%d", &grade);
5. if (grade <0) goto exit;
6. if( grade >= 60)
7. printf("You passed the exam!\n");
8. goto loop; /* label */
9. exit:
10. return 0;
Structured Programming
• Complete sets
– if, goto
– if,while,for
• Never use goto
Grade Translation (while)
1. int grade=0;
2. while(grade>=0)
3. {
4. printf("Please input your grade:");
5. scanf("%d", &grade);
6. if( grade >= 60)
7. printf("You passed the exam!\n");
8. }
Sum from 1 to 100 (goto)
i=1;
1. int i=1, sum=0; sum=0;
2. loop:
3. sum=sum+i; sum=sum+i;
4. i++;
5. if(i<=100) i++;
6. goto loop;
i<=100
Yes
No
Sum from 1 to 100 (while)
i=1;
1. int i=1,sum=0; sum=0;
2. while(i<=100)
3. { No
i<=100
4. sum=sum+i;
5. i++; Yes
6. } sum=sum+i;
i++;
Control Flows
i=1; i=1;
sum=0; i=1; sum=0;
sum=0;
Yes
i<=100
sum=sum+i; No
i<=100
sum=sum+i;
No
Yes
i++;
i++; sum=sum+i;
i<=100
Yes i++;
No
Goto While
Sum from 1 to 100 (do-while)
i=1;
1. int i=1,sum=0; sum=0;
2. do{
3. sum=sum+i; sum=sum+i;
4. i++;
5. } while (i<=100); i++;
i<=100
Yes
No
Sum from 1 to 100 (for)
i=1;
1. int i,sum; sum=0;
2. for(i=1,sum=0;i<=100;i++)
3. sum=sum+i; No
i<=100
Yes
sum=sum+i;
i++;
break and continue
1. int i; 1. int i;
2. for(i=1;i<=10;i++) 2. for(i=1;i<=10;i++)
3. { 3. {
4. if(i==5)break; 4. if(i==5)continue;
5. printf("%d ",i); 5. printf("%d ",i);
6. } 6. }
Nested Structure
1. int i,j;
2. for(i=0;i<10;i++)
3. {
4. for(j=0;j<i;j++)
5. printf("*");
6. printf("\n");
7. }
In-Class Exercise 2-2
• Write a program that prints the following
diamond shape. Minimize the number of
printf statements.
*
***
*****
*******
*********
*******
*****
***
*