Lecture 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Computing Fundamentals

(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

if ( count > COUNT_LIMIT )


printf ("Count limit exceeded\n");

Opening and closing braces are needed in case condition is applied to


more than one statement
If statement contd.
Decision control statements Syntax Description
In these type of statements, if
if (condition)
if { Statements; }
condition is true, then respective
block of code is executed.
In these type of statements,
if (condition)
group of statements are executed
{ Statement1; Statement2; }
if…else else
when condition is true. If
condition is false, then else part
{ Statement3; Statement4; }
statements are executed.
If condition 1 is false, then
if (condition1){ Statement1; }
condition 2 is checked and
else_if(condition2)
nested if { Statement2; }
statements are executed if it is
true. If condition 2 also gets
else Statement 3;
failure, then else part is executed.
Calculate The Absolute Value of an Integer
The absolute value is 100
#include <stdio.h>
int main (void)
{
int number = -100;
if ( number < 0 )
number = -number;
printf ("The absolute value is %i\n", number);
return 0;
}
Nested if-statements
#include <stdio.h>
int main ()
{ Value of a is 100 and b is 200
/* local variable definition */ Exact value of a is : 100
int a = 100; Exact value of b is : 200
int b = 200;
/* check the boolean condition */
if( a == 100 )
{
/* if condition is true then check the following */
if( b == 200 )
{
/* if condition is true then print the following */
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}
What is The Output?
#include <stdio.h>
int main(void)
{
char username = ‘a’;
int password = 12345; Username is incorrect, Try again.
if ( username == ‘A’ )
{
if ( password == 1345 )
{
printf( "Login successful“ );
}
else
{
printf("Password is incorrect, Try again.");
}
}
else
{
printf("Username is incorrect, Try again.");
}
}
What is The Output?
#include <stdio.h> 3 is odd.

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));

• Note: The standard library function scanf() is not secure! It is prevented by


default in Visual Studio. To be able to use it you need to add the following
line to the beginning of your program:
#define _CRT_SECURE_NO_WARNINGS
Input Formats

Format Specifier Explanation Example

%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;
}

You might also like