3. #include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Mondayn");
printf("Please enter the number of day. n The number must be
any integer value from 1 to 7n");
scanf("%d",&day);
if (day == 1)
printf("Mondayn");
else if (day == 2)
printf("Tuesdayn");
else if (day == 3)
printf("Wednesdayn");
// You may complete it yourself
Dr. Yousaf, PIEAS
4. The if/else Selection Structure
Nested if/else structures
• In previous example, the nested if/else structure is to
be used.
• Test for multiple cases by placing if/else selection
structures inside if/else selection structures
• Deep indentation usually not used in practice
• How can we solve this example more conveniently?
• Switch statement is a convenient way to code it.
Dr. Yousaf, PIEAS
5. Switch Statement
If you have a large decision tree, and all
the decisions depend on the value of the
same variable, you will probably want to
consider a switch statement instead of a
ladder of if...else or else if constructions.
Dr. Yousaf, PIEAS
6. // Example of switch statement
#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Mondayn");
printf("Please enter the number of day. n The
number must be any integer value from
1 to 7n");
scanf("%d",&day);
// Contd. (next page)
Dr. Yousaf, PIEAS
7. switch(day)
{
case 1: // 1 is one of possible value of day and so on.
printf("Mondayn");
break;
case 2:
printf("Tuesdayn");
break;
// please write cases 3 to 6 yourself
case 7:
printf("Sundayn");
break;
default:
printf("The number must be any integer value from 1 to 7n");
break;
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
8. • switch
– Useful when a variable or expression is tested for all the
values which can happen and different actions are taken
• Format
– Series of case labels and an optional default case
switch ( value )
{
case 1:
actions
case 2:
actions
default:
actions
}
– break; exits from structure
Dr. Yousaf, PIEAS
9. The switch Structure
• Flowchart of the switch structure
true
false
.
.
.
case a case a
action(s)
break
case b case b
action(s)
break
false
false
case z case z
action(s)
break
true
true
default
action(s)
Dr. Yousaf, PIEAS
10. • The expression used in a switch statement must have an integral or
character type.
• You can have any number of case statements within a switch. Each case
is followed by the value to be compared and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is needed in
the default case.
Dr. Muhammad Yousaf Hamza
Switch Statement
12. #include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
double y;
y = sin(PI/2.0); // argument is in radian
printf("%lf", y);
getchar();
return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
13. // If given theta is in degree, then convert it first into radians.
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
double y;
float theta_deg, theta_rad;
theta_deg = 90.0;
theta_rad = (PI/180)*theta_deg;
y = sin(theta_rad); // argument is in radian
printf("%lf", y);
getchar(); return 0; }
Output: 1.000000
Dr. Yousaf, PIEAS
24. Examples
Write a program that prints 10 stars as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("*n*n*n*n*n*n*n*n*n*");
getchar();
return 0;
}
25. Examples
Write a program that prints 100 stars in the format
shown in the previous slides.
Dr. Yousaf, PIEAS
26. Examples
Write a program that prints 100 stars in the format
shown in the previous slides.
Too much labor work?
There MUST be an easier way
Dr. Yousaf, PIEAS
28. Examples
Write a program that prints counting 1 to 10 as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("1n2n3n4n5n6n7n8n9n10");
getchar();
return 0;
}
29. Examples
Write a program that prints counting from 1 to 1000 in
the format shown in the previous slide.
Dr. Yousaf, PIEAS
30. Examples
Write a program that prints counting from 1 to 1000 in
the format shown in the previous slide.
• Too much labor work?
• There MUST be an easier way
• Is there any?
• YES
LOOPS
Dr. Yousaf, PIEAS
34. Printing the counting from 1 to 10
#include<stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
printf("%dn", i);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
35. Printing the counting from 1 to 1000
#include<stdio.h>
int main()
{
int i;
for (i = 1; i <= 1000; i++)
printf("i = %dn", i);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
37. The for Statement
• The most important looping structure in C.
• Generic Form:
for (initial ; condition ; increment )
statement
• initial, condition, and increment are C expressions.
• For loops are executed as follows:
1. initial is evaluated. Usually an assignment statement.
2. condition is evaluated. Usually a relational expression.
3. If condition is false (i.e. 0), fall out of the loop (go to step 6.)
4. If condition is true (i.e. nonzero), execute statement
5. Execute increment and go back to step 2.
6. Next statement
38. The for Statement
//For statement examples
#include <stdio.h>
int main ()
{
int count;
/* 1. simple counted for loop */
printf("Output with increment in loopn");
for (count =1; count <=10; count++)
printf ("%dn", count);
/* 2. counting backwards */
printf("Output with decrement in loopn");
for (count = 56; count >48; count--)
printf("%dn", count);
/* 3. for loop counting by
5's */
printf("Output with increment
of 5n");
for (count=0; count<32;
count += 5)
printf("%dn", count);
getchar();
return 0;
}
39. The for Statement
#include <stdio.h>
int main ()
{
int count;
/* initialization outside of loop */
count = 1;
for ( ; count < 7; count++)
printf("%d ", count);
printf(“n");
/* increment outside of loop */
count = 1;
for ( ; count < 7; )
{
printf("%d ", count);
count++;
}
getchar(); return 0; }
40. #include <stdio.h>
int main ()
{
int count;
int x, y;
/* compound statements increment */
for (x=0, y=100; x<y; x++, y--)
{
printf("%d, %dn", x,y);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The for Statement
41. The for Repetition Structure
• Format when using for loops
for ( initialization; loopContinuationTest; increment )
statement
• Example:
for (i = 1; i <= 10; i++)
printf("i = %dn", i);
–Prints the integers from one to ten
No
semicolon
(;) after last
expression
Dr. Yousaf, PIEAS
42. The for Structure: Observations
• Arithmetic expressions
Initialization, loop-continuation, and increment can
contain arithmetic expressions.
x = 2;
y = 10;
for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to
for ( j = 2; j <= 80; j += 5 )
Dr. Yousaf, PIEAS
43. The for Structure: Observations
• Notes about the for structure:
– "Increment" may be negative (decrement)
– If the loop continuation condition is initially
false
• The body of the for structure is not
performed
• Control proceeds with the next statement
after the for structure
– Control variable // for (i = 1; i <= 10; i++)
• Often printed or used inside for body, but not
necessary
Dr. Yousaf, PIEAS
44. For Loop
#include <stdio.h>
int main()
{
int x;
/* The loop goes if x < 10, and x increases by one in every
loop*/
for ( x = 0; x < 10; x++ )
{
/* Keep in mind that the loop condition checks
the conditional statement before it loops again.
consequently, when x equals 10 the loop breaks.
x is updated before the condition is checked. */
printf( "%dn", x );
}
return 0;
}
Dr. Yousaf, PIEAS