CS1100 - Introduction To Programming
CS1100 - Introduction To Programming
• Programming : From Turtle to C.
• Data Types in C, Representations, Operators.
• Formatting the Input and the Output.
Week 1-4
• Execution of Programs, Compilers.
• Modifying the control flow in Programs
if-then-else, switch.
CS1100 – Introduction to Programming
• Programming : From Turtle to C.
• Data Types in C, Representations, Operators.
• Formatting the Input and the Output.
Week 1-4
• Execution of Programs, Compilers.
• Modifying the control flow in Programs
if-then-else, switch.
• while, for, do while constructs in C.
• Example problems. Week 5-6
• Programming for engineers.
Multiple Selection : switch construct
• Syntax:
switch (expression) {
case const-expr-1: statements-1;
case const-expr-2: statements-2;
..
.
default: statements-def;
}
• Semantics
• Evaluate expression.
• If it matches the const-expr-i, execute statements-i and
following as long as an explicit break is not encountered.
• If none of the const-expr-i match, execute the statements-def.
Even or odd – using switch
int input;
switch (input % 2) {
case 0: printf("Number is even\n");
case 1: printf("Number is odd\n");
default: printf("Neither even nor odd\n");
}
return 0;
}
Even or odd – using switch
int input;
switch (input % 2) {
case 0: printf("Number is even\n"); break;
case 1: printf("Number is odd\n"); break;
default: printf("Neither even nor odd\n");
}
return 0;
}
The while construct
• Syntax
while (expression) {
statements;
}
The while construct
• Syntax
while (expression) {
statements;
}
• Semantics
1. As long as expression is
true, execute statements.
2. If expression is false, exit
the loop.
The while construct
• Syntax
while (expression) {
statements;
}
• Semantics
1. As long as expression is
true, execute statements.
2. If expression is false, exit
the loop.
• Value of expression must be
changed by the body of the loop,
otherwise we have an infinite
loop.
The while construct
• Syntax
while (expression) {
statements;
}
• Semantics
1. As long as expression is
true, execute statements.
2. If expression is false, exit
the loop.
• Value of expression must be
changed by the body of the loop,
otherwise we have an infinite
loop.
• expression can contain relational,
logical or equality operators.
Relational <= < > >=
Equality == !=
Logical && ||
Reversing the digits of a given unsigned integer
#include "stdio.h"
int main () {
int number, revNumber, remainder;
revNumber = 0;
printf ("Input number:");
scanf ("%d", &number);
while (number > 0) {
remainder = number % 10;
revNumber = revNumber*10 + remainder;
number = number/10;
}
printf ("The reversed number is : %d\n", revNumber);
}
Example: Sum even and odd numbers
Accept integers from the standard input as long as the user does
not enter -1. Once the user enters -1, print the sum of all integers
entered so far, sum of even integers and sum of odd integers.
#include<stdio.h>
int main() {
int input;
int sum, eSum, oSum;
printf("Enter an integer: \t");
scanf(" %d", &input);
#include<stdio.h>
• common
int main() {
int input; mistake:
int sum, eSum, oSum; forgotten
printf("Enter an integer: \t"); initialization.
scanf(" %d", &input);
• expr. not
while (input != -1) { modified in body
sum += input; of loop.
switch (input % 2) {
case 0: eSum += input; break;
case 1: oSum += input;
}
}
printf("sum = %d, oddSum = %d, evenSum = %d\n", sum, oSum, eSum);
return 0;
}
Summing up odd and even numbers
#include<stdio.h>
int main() {
int input;
int sum, eSum, oSum;
printf("Enter an integer: \t");
scanf(" %d", &input);
sum = eSum = oSum = 0; // initialization.
• Syntax
for (stmt1; expr; stmt2) {
statements;
}
for construct
• Syntax
for (stmt1; expr; stmt2) {
statements;
}
• Semantics
1. Execute stmt1.
2. If expr is true, execute
statements. execute
stmt2. goto step 2.
3. If expr is false, exit loop.
User enters 100 integers. Sum them and print the sum.
Sum of 100 integers – using for loop
User enters 100 integers. Sum them and print the sum.
#include<stdio.h>
int main() {
int input;
int count;
int sum = 0;
int d, ten_power_d;
for (d=1, ten_power_d = 10; ten_power_d <= n; d++, ten_power_d *= 10)
{
}
printf("%d", d);
return 0;
}
Uncommon ways of using for
Predict the output of this program :
#include<stdio.h>
int main() {
int n;
int d, ten_power_d;
for (d=1, ten_power_d = 10; ten_power_d <= n; d++, ten_power_d *= 10)
{
}
printf("%d", d);
return 0;
}
• Prints the number of digits in the given number.
• The above method is considered to be a bad programming
practice.
do while construct
• Syntax
do
{ statements; }
while (expr);
do while construct
• Syntax
do
{ statements; }
while (expr);
• Semantics
1. Execute statement.
2. If expr is true, goto step
1, else exit loop.
#include<stdio.h>
int main() {
int input;
int count = 1;
int sum = 0;
do {
printf("Enter an integer: \t");
scanf(" %d", &input);
count++;
sum += input;
} while (____);
#include<stdio.h>
int main() {
int input;
int count = 1;
int sum = 0;
do {
printf("Enter an integer: \t");
scanf(" %d", &input);
count++;
sum += input;
} while (count <= 100);
#include<stdio.h>
int main() {
int input;
int count = 1;
int sum = 0;
do {
printf("Enter an integer: \t");
scanf(" %d", &input);
count++;
sum += input;
} while (count <= 100);
#include<stdio.h>
main() {
int int input;
int count = 1;
int sum = 0;
do {
printf("Enter an integer: \t");
scanf(" %d", &input);
count++;
if (input != -1) sum += input;
} while (count <= 100 && input != -1);
#include<stdio.h>
int main() {
int n;
int value = 2;
while (____) {
value *= 2;
______;
}
printf("%d\n", value);
return 0;
}
Computing 2n
Accept n from the user. Print 2n on the standard output.
Computing 2n
Accept n from the user. Print 2n on the standard output.
#include<stdio.h>
int main() {
int n;
int value = 2;
while (n > 1) {
value *= 2;
n--;
}
printf("%d\n", value);
return 0;
}
Stepping back : Stages of Program Design ...
Pseudo-code:
begin program
while n is more than 1
show n
if n is odd then
set n = 3n + 1
else
set n = n / 2
endif
endwhile
show n
end program
An Illustration : Collatz Sequence
Pseudo-code:
begin program C-program segment:
while n is more than 1
while(n > 1) {
show n
printf("%d,",n);
if n is odd then
if (n%2 == 1)
set n = 3n + 1
n = 3*n+1;
else
else
set n = n / 2
n = n/2;
endif
}
endwhile
printf("%d.",n);
show n
end program
Programs : Termination and Correctness
∑n n(n−1)
Someone claims that For every n, k=1 k = 2 .
Programs : Termination and Correctness
∑n n(n−1)
Someone claims that For every n, k=1 k = 2 . You ask for
proof !
Programs : Termination and Correctness