C Programming Language is the most popular computer language and most used programming language till now. It is very simple and elegant language. This lecture series will give you basic concepts of structured programming language with C.
4. Branching: The if-else statement
ď˝ Lets consider a scenario:
We want to find the biggest out of two numbers.This problem
requires comparison of the two numbers and based on the
comparison result, the bigger number will be found.
ď˝ We can solve this problem by using if instruction.
if(expression)
{
statement
}
5. Branching: The if-else statement (cont..)
ď˝ Lets solve this problem by using if instruction.
#include<stdio.h>
main()
{
int first, second;
scanf(â%d %dâ, &first, &second);
if(first > second)
{
printf(âFirst number is biggerâ);
}
}
6. Branching: The if-else statement (cont..)
ď˝ In the written program, if the input is higher than the second number, only
than the printf statement will be executed.
ď˝ If the input is 20 10
ď˝ We will see the output
First number is bigger
ď˝ If the input is 10 20
ď˝ No output message will appear on the screen.
ď˝ We can extend the above program
7. Branching: The if-else statement (cont..)
ď˝ Lets extend the previous program:
#include<stdio.h>
main()
{
int first, second;
scanf(â%d %dâ, &first, &second);
if(first > second)
printf(âFirst number is biggernâ);
if(second >first)
printf(âSecond number is biggernâ);
if(first == second)
printf(âTwo numbers are equalnâ);
}
8. Branching: The if-else statement (cont..)
ď˝ This will give the following results:
ď˝ If the input is 20 10
ď˝ We will see the output
First number is bigger
ď˝ If the input is 10 20
ď˝ We will see the output
Second number is bigger
ď˝ If the input is 20 10
ď˝ We will see the output
Two numbers are equal
9. Branching: The if-else statement (cont..)
ď˝ We can also rewrite the program like this:
#include<stdio.h>
main()
{
int first, second;
scanf(â%d %dâ, &first, &second);
if(first > second)
printf(âFirst number is biggernâ);
else if(second >first)
printf(âSecond number is biggernâ);
else
printf(âTwo numbers are equalnâ);
}
10. Branching: The if-else statement (cont..)
ď˝ What we can see is:
ď˝ The if-else statement can be written as:
if (testExpression1) {
// statement(s)
}
else if(testExpression2){
// statement(s)
}
âŚâŚâŚ
else {
// statement(s)
}
12. The if-else statement (More Examples)
ď˝ Example 2: Suppose, we want to calculate the
biggest among three numbers. So, how can we
solve this problem?
13. The if-else statement (More Examples)
#include<stdio.h>
main(){
int a,b,c, biggest;
scanf(â%d %d %dâ, &a, &b, &c);
if(a > b && a > c)
biggest = a;
else if(b> c)
biggest = b;
else
biggest = c;
printf(âbiggest = %dâ, biggest);
}
14. The nested if-else statement
ď˝ It is possible to nest if-else statement, one within another
ď˝ There are several different forms that nested if-else statements
can take.
ď˝ The most general form of two-later nesting is
if e1 if e2 s1
else s2
else if e3 s3
else s4
15. The nested if-else statement (Example)
ď˝ Recall the previous Example: Suppose, we want
to calculate the biggest among three numbers.
So, how can we solve this problem?
16. The if-else statement (Example)
#include<stdio.h>
main(){
int a,b,c, biggest;
scanf(â%d %d %dâ, &a, &b, &c);
if(a > b)
if(a>c) biggest = a;
else if(b> c)
biggest = b;
else
biggest = c;
printf(âbiggest = %dâ, biggest);
}
17. Looping
ď˝ We may encounter situations, when a block of code needs to
be executed several number of times.
ď˝ In general, statements are executed sequentially:The first
statement in a function is executed first, followed by the
second, and so on.
ď˝ A loop statement allows us to execute a statement or group
of statements multiple times.
18. Looping (cont..)
ď˝ The general form of a loop statement in most of the
programming languages â
19. Looping (cont..)
C programming language provides the following types of loops to
handle looping requirements.
ď˝ while loop: Repeats a statement or group of statements
while a given condition is true. It tests the condition before
executing the loop body.
ď˝ for loop: Executes a sequence of statements multiple times
and abbreviates the code that manages the loop variable.
ď˝ do...while loop: It is more like a while statement, except that
it tests the condition at the end of the loop body.
ď˝ nested loops: You can use one or more loops inside any
other while, for, or do..while loop.
20. Looping: The while Statement
ď˝ Suppose we want to display âhelloâ on output screen five
times in five different lines, we might think of writing either
five printf statements or one printf statement consisting of
constant string âhellonâ five times.
ď˝ Now, if we want to display âhelloâ 500 times, what will
happen?
ď˝ To solve this, we need some programming facility to repeat
certain works.
21. The while Statement
ď˝ The while statement is used to carry out looping operation in
which a group of statements is executed repeatedly, until some
condition has been satisfied.
ď˝ The general form of the while statement is
while (expression) statement
ď˝ The statement will be executed repeatedly, as long as the
expression is true.
23. The while Statement (cont..)
ď˝ Example: Suppose we want to display the consecutive
digits 0,1,2,âŚ..,9, with one digit on each line.This can
be accomplished with the following program.
Solution:
â
main()
â
{
â
int digit = 0;
â
while(digit<=9){
â
printf(â%dnâ,digit);
â
++digit;
â
}
25. The while Statement (More Examples)
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
main()
{
int num, count = 1, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
while (count <= num) {
sum += count;
++count;
}
printf("Sum = %d", sum);
}
26. The while Statement: Infinite Loop
Infinite loop: var will always have value >=5 so the loop would never end.
int main()
{
int var = 6;
while (var >=5)
{
printf("%d", var);
var++;
}
return 0;
}
27. The while Statement: Infinite Loop
Infinite loop: var value will keep decreasing because of â- operator, hence it will always be
<= 10.
int main()
{
int var =5;
while (var <=10)
{
printf("%d", var);
var--;
}
return 0;
}