Chap 4 Control Structures
Chap 4 Control Structures
Objectives
Understand the sequence structure
Understand selection structures; if, if..else statements
Select paths with the switch..case statement
Understand loop structures; for loop, do..while loop
and do..while loop
Use break to exit a loop
Know when to use the continue statement
Understand the goto statement
Nest if statements
Create nested loops
(i) if (x<y)
printf(“x is less that y”);
(ii) if (salary >500)
tax-amount = salary * 1.5;
(iii) if(balance<1000 || status =’R’)
print (“Balance = %f”, balance);
if(expression)
{
block of statements;
}
You may write a series of statements enclosed by braces. (You do not
need to, and should not, put a semicolon after the closing brace, because
the series of statements enclosed by braces is not itself a simple expression
statement.)
1. if(n > 0)
2. average = sum / n;
3. else
4. {
5. printf("can't compute average\n");
6. average = 0;
7. }
1. if(sale_amount>=10000)
2. Disc=sal_amt*0.10; /*ten percent/
3. else if (sal_amt>=5000&&sal_amt<1000 )
4. printf(“The discount is %f”,sal_amt*0.07 ); /*seven percent */
5. else if(sal_amt=3000&&sal_amt<5000)
6. {
7. Disc = sal_amt * 0.05; /* five percent */
8. printf ( “ The discount is %f “ , Disc ) ;
9. }
10. else
11. printf(“The discount is 0”) ;
The switch..case statements can be used in place of the if - else statements when
there are several choices to be made.
1. #include<stdio.h>
2. main()
3. {
4. int choice;
5. printf(“Enter a number of your choice ”);
6. scanf(“ %d”, &choice);
7. if (choice >=1 && choice <=9) /* Range of choice numbers */
8. switch (choice)
9. { /* Begin of switch */
10. case 1: /* label 1 */
11. printf(“\n You typed 1”);
12. break;
13. case 2: /* label 2 */
14. printf(“\n You typed 2”);
1. break;
2. case 3: /* label 3 */
3. printf(“\n You typed 3”);
4. break;
5. case 4: /* label 4 */
6. printf( “ \n You typed 4”);
7. break;
8. default:
9. printf(“There is no match in your choice”);
10. } /* End of switch */
11. else
12. printf(“Your choice is out of range”);
13. return (0);
14. } /* End of main */
The break statement causes the program to break out of the switch and
skip to the next statement after the switch. Without the break statement,
every statement from the matched label to the end of the switch will be
processed.
Remove all the break statements from the program and then
run the program using the number 3. What is the result?
Note
1. /* counter.c */
2. /* Displays the digits 1 through 9 */
3. main()
4. {
5. int digit=0; /* Initialisation */
6. while (digit<=9)
7. {
8. printf(“%d \n”, digit);
9. digit++;
10. }
11. return 0;
12. }
Notice that since the test comes at the end, the loop body
(statement) must be executed at least once.
Rewriting the program that counts from 0 to 9, using the do while loop:
1. /* counter1.c */
2. /* Displays the digits 1 through 9 */
3. main()
4. {
5. int digit=0; /* Initialisation */
6. do
7. {
8. printf(“%d \n”, digit);
9. digit++;
10. } while (digit<=9);
11. return 0;
12. }
Example
for(int k=0;k<=5; k++)
printf(k = %d \n”, k); The code should print integers 0 through to 5, each on a
different line.
Example: Counting 0 to 9 using a ‘for’ loop
/* Displays the digits 1 through 9 */
#include<stdio.h>
main()
{
int digit;
for(digit=0;digit<=9; digit++)
printf(“%d \n” , digit);
return 0;
}
1. #include <stdio.h>
2.
3. main()
4. {
5. int t ;
6.
7. for ( ; ; )
8. {
9. printf(“Value of t: “);
10. scanf("%d" , &t) ;
11. if ( t==10 )
12. break ;
13. }
14. printf("End of an infinite loop...\n");
15.
16. }
Alternative 1 Alternative 2
if (a>14) if (a>14)
goto a; sheds=3;
sheds=2; else
goto b; sheds=2;
a: sheds=3; k=2*sheds;
b: k=2 * sheds;
Using a nested if statement, write a program that prompts the user for a
number and then reports if the number is positive, zero or negative.
Write a while loop that will calculate the sum of every fourth integer,
beginning with the integer 3 (that is calculate the sum 3 + 7 +11 + 15 + ...)
for all integers that are less than 30.
Write a program that prints only the odd numbers between 1 and 100. Use
a for loop that looks this:
for(i=1; i<101; i++)....
Use a continue statement to avoid printing even numbers.