Lecture 3
Lecture 3
Lecture 3
(CS116)
Lecture 3
if-statement
#include <stdio.h>
int main (void) Lets test what we learned
{ 123.125000 assigned to an int produces 123
float f1 = 123.125, f2; -150 assigned to a float produces -150.000000
-150 divided by 100 produces -1.000000
int i1, i2 = -150; -150 divided by 100.0 produces -1.500000
i1 = f1; // floating to integer conversion (float) -150 divided by 100 produces -1.500000
printf ("%f assigned to an int produces %i\n", f1, i1);
f1 = i2; // integer to floating conversion
printf ("%i assigned to a float produces %f\n", i2, f1);
f1 = i2 / 100; // integer divided by integer
printf ("%i divided by 100 produces %f\n", i2, f1);
f2 = i2 / 100.0; // integer divided by a float
printf ("%i divided by 100.0 produces %f\n", i2, f2);
f2 = (float) i2 / 100; // type cast operator
printf ("(float) %i divided by 100 produces %f\n", i2, f2);
return 0;
}
If-statement
• Provides a general decision-making
if ( expression )
program statement(s);
• A simple condition
int main(void)
{
int num = 3;
if((num % 2) == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
The scanf() (Scan Formatted) Function
• Scan keyboard for user input
• Assigns input to specific variables
• Ability to input multiple values at once
• Syntax
scanf(“Format Specifier(s) like %d %f %c %i", &Variable name(s));
%d Reads an integer 10
Reads a floating-point
%f number in fixed 10.500000
decimal format
Reads a floating-point
%.1f number with 1 digit 10.5
after the decimal
Let’s Modify the example of absolute value
#define _CRT_SECURE_NO_WARNINGS Type in your number: -100
#include<stdio.h> The absolute value is 100
int main (void)
{
int number; Type in your number: 2000
printf ("Type in your number: "); The absolute value is 2000
scanf ("%i", &number);
if ( number < 0 )
number = -number;
printf ("The absolute value is %i\n", number);
return 0;
}
Odd/Even Test with scanf()
#include <stdio.h>
int main (void) Enter your number to be tested.: 11
The number is odd.
{
int number_to_test, remainder;
printf ("Enter your number to be tested.: ");
scanf ("%i", &number_to_test);
remainder = number_to_test % 2; If-statements can be independent
if ( remainder == 0 )
printf ("The number is even.\n");
if ( remainder != 0 )
printf ("The number is odd.\n");
return 0;
}
The switch statement
• Handles complex conditional and branching operations
• Case Label must be unique
• Case Labels must end with Colon
• Case labels must have constants / constant expression (Not
variables)
• Case label must be of integral Type ( Integer, Character)
• Case label should not be ‘floating point number ‘
• Switch case can optionally have at most one default label
• Two or more cases may share one break statement
• Nesting ( switch within switch ) is allowed.
The work diagram
Example with Numerical Cases
int main()
{
int num = 2; Case1: Value is: 2
switch( (num + 2) % 3 )
{
case 1:
printf("Case1: Value is: %d", num);
break;
case 2:
printf("Case2: Value is: %d", num);
break;
case 3:
printf("Case3: Value is: %d", num);
break;
default:
printf("Default: Value is: %d", num);
}
return 0;
}