Professional Documents
Culture Documents
statements/functions/symbols:
if-else
SYNTAX :
if (expr)
statement1
else
statement2
Switch
SYNTAX:
switch(expression)
{ case constant expression: statement; break;
case constant expression:
statement:
break:
case constant expression:
statement;
break;
[default
statement;]
}
Continue
SYNTAX:
continue;
2.
A. Give the syntax and explain for loop:
SYNTAX:
for(initialization; condition; increment)
statement;
do {
Statement;
} while(expression);
next statement;
if x>25.0{
y=x
else
y=z; ; }
if (x>25.0)
{ y=x;
printf(“x is equal to %d\n”, y);
}
else
{ y=z;
printf(“z is equal to %d\n”, y);
}
points =0;
switch (mark) {
case ‘A’:
points =4;
exit();
case ‘B’:
points =3;
break;
case ‘C’:
points =2;
continue;
case ‘E’:
case ‘I’:
case ‘W’:
points =0;
}
if (points>0)
printf (“Passed, points earned =%d\n”, points);
else
printf (Failed, no points earned\n”);
ANSWER:
When mark is ‘T’: “Passed, points earned 2”
When mark is ‘A’: “Passed, points earned 4”
When mark is ‘B’: “Passed, points earned 3”
When mark is ‘C’: “Passed, points earned 2”
(b)
x=7;y=8
if (x<=y)
{if(x==y)
x++;}
ANSWER: 1
8. Explain the difference between the statements on the left and
if(ME<2)
if(ME>0)
{YOU=ME; printf(“%d”, YOU}
else if(ME=5)
{YOU=THEY*THEM; printf(“%d”, YOU}}
else if(ME=2)
{YOU++; printf(“%d”, YOU}}
else if(ME=7)
{YOU=YOU-4; printf(“%d”, YOU}}
else
{ME=YOU; printf(“%d”, YOU}
}
b. switch… case statement
switch(number) {
case 1: printf(YOU\n”);
break;
case 5: printf(YOU\n”);
break;
case 2: printf(ME\n”);
break;
case 7: printf(YOU\n”);
break;
default:printf(“Invalid entry!”); }
getch();
}
12. Write a program segment for the following using case. If the
value of the character variable CHAR_VAR is ‘A’ or ‘a’, add 1 to
integer variable JETT. If the value of CHAR_VAR is ‘B’ or ‘b’,
subtract Y from Z giving the value to JETT. If the value of
CHAR_VAR is ‘C’ or ‘c’, subtract Z from Y giving the value to
JETT. Any other value would result to an error message “INVALID
ENTRY”.
scanf(“%c”, &CHAR_VAR);
scanf(“%d”, &JETT) ;
{
if (CHAR_VAR=’a’);
JETT=JETT+1;
else
if (CHAR_VAR=’b’);
JETT=y-z
else
if (CHAR_VAR=’b’);
JETT=z-y
else
printf(“INVALID ENTRY:\n”);
}
else
if (c==2)
{ a = a + a;
goto x;}
else
if (c==2)
{ a = a + a;
goto x;}
printf(“The value of a is %d”,a);
else
printf(“The value of a is %d”,a);
}
else
if (c==2)
{ a = a + a;
goto x;}
printf(“The value of a is %d”,a);
else
printf(“The value of a is %d”,a);
The value of a is 0.
a=5;
c=-1
x:
c++;
if(c==1)
{ a=a*a;
goto x;
printf(“The value of a is %d”,a);
else
if (c==2)
{ a = a + a;
goto x;}
printf(“The value of a is %d”,a);
else
printf(“The value of a is %d”,a);
(c)
x = 10; y = 10;
while (x != y) {
printf(“%5d %5d\n”,x,y);
x--; y ++; }
ANSWER:9 11
Write a program that would input 3 integers and would output them
in descending order.
Days Fine
< = 2 10.00
<= 4 15.00
<= 5 20.00
>= 7 Equal to the amount of rent
(CD = 50.00 / VHS = 35.00 )
CASE STUDY 1
CASE STUDY 2
CASE STUDY 3
Input number: 9
Factors are 1,3
Sum of its factors : 1 + 3 = 4
Half of number : 9 / 2 = 4.5
9 is NOT DWARF