Exercise 3
Exercise 3
b) int i=0,x=0;
2. State whether the following are true or false. If the for(i=1;i<10;++i){
answer is false, explain why. if (i%2==1){
a) The default case is required in the x+=i;
else
switch selection statement
x--;
b) The break statement is required in the printf(“%d”,x);
default case of a switch selection }
statement printf(“\nx=%d”,x);
c) The expression (x>y && a < b) is true
if either x > y is true or a < b is true. c) int i=0,x=0;
d) An expression containing the || operator is for(i=1;i<10;++i){
true if either or both of its operands is true. if (i%2==1){
x+=i;
3. Write a statement or a set of statements to else
accomplish each of the following task x--;
printf(“%d”,x);
continue;
a) Sum the odd integers between 1 and 99
}
using for statement. Assume the integer
printf(“\nx=%d”,x);
variables sum and count have been
defined. d) int i=0,x=0;
b) Print the integers from 1 to 20 using a while for(i=1;i<10;++i){
loop and the counter variable x. assume if (i%2==1){
that the variable x has been defined, but not x+=i;
initialized. Print only five integers per line. else
[hint: use the calculation x%5. When the x--;
value of this is 0, print a newline character, printf(“%d”,x);
otherwise print a tab character.] break;
}
printf(“\nx=%d”,x);
4. Write a switch statement that will examine the
e) int i=0,x=0;
value of char-type variable called color and print do{
one of the following message, depending on the if (i%3==0){
character assigned to color. x++;
printf(“%d”,x);
a) RED, if either r or R is assigned to color, }
b) GREEN, if either g or G is assigned to color ++i;
c) BLUE, if either b or B is assigned to color }while(i<10);
d) BLACK, if color is assigned any other character. printf(“\nx=%d”,x);
b. for(y=.1;y!=1.0;y+=.1)
printf(“%f\n”,y);
c. switch(n){
case 1:
printf(“the number is 1\n”);
case 2:
printf(“the number is 2\n”);