Unit 2
Unit 2
∙ nested if statements
∙ if-else statements
Page 1 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
∙ if statements
Page 2 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
}
There will be an execution of the statements available inside the body of the if block
whenever the given condition returns to be true. In case this condition returns to be false, then
the program will skip the statements that we have inside the if block.
Use:
This statement is one of the very simplest forms of decision control statements. We often use
it in the cases of simple decision-making cases in the C language.
Example:
Let us look at an example of a program that will ultimately print the highest
number. #include <stdio.h>
void main()
{
int x;
printf(―We enter any number:‖);
scanf(―%d‖, &x);
printf (―The number entered here is %d‖, x);
if (x>100)
printf (―Hi. You have entered a comparatively higher number.‖);
}
Here, in this program, the compiler will accept a number provided by the user as an input and
will generate an output accordingly. Here, we have also given the expression n>100. It means
that if the number given by the user is higher than the value of 100, then only we will get the
message as the output. Else, this statement will be skipped once and for all.
Thus,
Case #1:
If x = 300,
The output generated here will be:
Hi. You have entered a comparatively higher number.
Case #2:
If x = 50,
The compiler will skip the statement, ―Hi. You have entered a comparatively higher number. ‖
Page 3 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
Page 4 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
Case #1:
year = 1893
The output generated here will be:
We enter a year: 1893
No. The provided year is not a leap year….!!!
Case #2:
year = 1564
The output generated here will be:
We enter a year: 1564
Yes. The provided year is a leap year. Great job…!!!!
Page 5 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
default:
Page 6 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
Case #2:
#include <stdio.h>
int main()
{
int a=20,b=40;
if (a>b) {
printf(―The number a is greater than the number b‖);
}
else if(a<n) {
printf(―The number a is less than the number b‖);
}
else {
printf(―The number a is equal to the number
b‖); }
}
The output generated here would be:
The number a is less than the number b
Case #3:
#include <stdio.h>
int main()
{
int a=30,b=30;
if (a>b) {
printf(―The number a is greater than the number b‖);
}
else if(a<n) {
printf(―The number a is less than the number b‖);
}
else {
printf(―The number a is equal to the number b‖);
Page 7 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
}}
The output generated here would be:
The number a is equal to the number b
Page 8 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
}
else {
printf(―The number p is equal to the number q‖);
}
}
Page 9 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
Look at the pair of inputs and outputs. Which of these are true?
1. Please enter your age here: 12
Sorry. You are still not eligible for voting today.
2. Please enter your age here: 22
Okay. You are not eligible for voting today.
3. Please enter your age here: 35
Okay. You are not eligible for voting today.
4. Please enter your age here: 09
Sorry. You are still not eligible for voting today.
A. 1, 2, and 3
B. 3, 4
C. 1, 2, and 4
D. 1, 2, 3, and 4
Answer – D. 1, 2, 3, and 4
What is the difference between the if statement and the if-else statement?
The if statement is one of the very simplest forms of decision control statements. We often
use it in the cases of simple decision-making cases in the C language.
Syntax:
if (condition x)
{ Statements; }
The Description is:
In the case of such a statement, when the available condition is true, there occurs an
execution of a certain block of code.
if…else
Various times when a user writes that if a condition/ expression is true, then the compiler
must take an action. Else, if that statement is false, then the compiler will take a different
action. Thus, we can include an action for either of the conditions or include just a single
statement for the true condition and no action for any other condition. We use the if-else
statement here.
Syntax:
if (condition x)
{ Statement x; Statement y; }
Page 10 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
else
{ Statement a; Statement b; }
The Description is:
In the case of such a statement, when the available condition is true, then there occurs an
execution of a certain group of statements. In case this available condition turns out to be
false, there occurs an execution of the statement specified in the else part.
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
#include <stdio.h>
int main()
Page 11 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
{
int m=40,n=20;
if (m>n) {
printf("m is greater than n");
}
else if(m<n) {
printf("m is less than n");
}
else {
printf("m is equal to n");
}
}
*********************************************************
* Statement - Prefect Leap Year
**********************************************************/
#include <stdio.h>
#include <conio.h>
void main()
{
int year;
clrscr();
printf("Enter the year : ");
scanf("%d",&year);
int temp=year/100;
if(year%100==0)
{
if(temp%4==0)
{
printf("%d is leap year.",year);
}
else
Page 12 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
{
printf("%d is ordinary year.",year);
}
}
else
{
if(year%4==0)
{
printf("%d is leap year.",year);
}
else
{
printf("%d is ordinary year.",year);
}
}
getch();
}
/********************************************************************
Statement - ATM money dispatch count while currencies are 1000,500 and 100
*********************************************************************
#include<stdio.h>
#include<conio.h>
int totalThousand =1000;
int totalFiveFundred =1000;
int totalOneHundred =1000;
void main(){
unsigned long withdrawAmount;
unsigned long totalMoney;
int thousand=0,fiveHundred=0,oneHundred=0;
clrscr();
printf("Enter the amount in multiple of 100: ");
Page 13 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
scanf("%lu",&withdrawAmount);
if(withdrawAmount %100 != 0){
printf("Invalid amount;");
getch();
return;
}
totalMoney = totalThousand * 1000 + totalFiveFundred* 500 +
totalOneHundred*100;
if(withdrawAmount > totalMoney){
printf("Sorry,Insufficient money");
getch();
return;
}
thousand = withdrawAmount / 1000;
if(thousand > totalThousand)
thousand = totalThousand;
withdrawAmount = withdrawAmount - thousand * 1000;
if (withdrawAmount > 0){
fiveHundred = withdrawAmount / 500;
if(fiveHundred > totalFiveFundred)
fiveHundred = totalFiveFundred;
withdrawAmount = withdrawAmount - fiveHundred * 500; }
if (withdrawAmount > 0)
oneHundred = withdrawAmount / 100;
printf("Total 1000 note: %d\n",thousand);
printf("Total 500 note: %d\n",fiveHundred);
printf("Total 100 note: %d\n",oneHundred);
getch();
}
Page 14 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
/*******************************************************
Statement - Display Day of the month.
*******************************************************/
#include<stdio.h>
#include<conio.h>
#include<math.h>
int fm(int date, int month, int year) {
int fmonth, leap;
//leap function 1 for leap & 0 for non-leap
if ((year % 100 == 0) && (year % 400 != 0))
leap = 0;
else if (year % 4 == 0)
leap = 1;
else
leap = 0;
fmonth = 3 + (2 - leap) * ((month + 2) / (2 * month))
+ (5 * month + month / 9) / 2;
//bring it in range of 0 to 6
fmonth = fmonth % 7;
return fmonth;
}
Page 15 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
case 0:
printf("weekday = Saturday");
break;
case 1:
printf("weekday = Sunday");
break;
case 2:
printf("weekday = Monday");
break;
case 3:
printf("weekday = Tuesday");
break;
case 4:
printf("weekday = Wednesday");
break;
case 5:
printf("weekday = Thursday");
break;
case 6:
printf("weekday = Friday");
break;
default:
printf("Incorrect data"); }
return 0;
}
//-----------------------------------------
- void main() {
int date, month, year;
clrscr();
printf("\nEnter the year ");
scanf("%d", &year);
Page 16 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
Page 17 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
Page 18 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
char ch;
printf("Enter any character:");
scanf("%c", &ch);
switch(ch)
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
printf("%c is a vowel",ch);
break;
default:
printf("%c is not a vowel", ch);
}
Page 19 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
big=x;
printf(―biggest of given two number is %d‖, big);
}
else
{
big=y;
printf(―biggest of given two number is %d‖, big);
}
Iterative Statements
∙ A loop is a statement whose job is to repeatedly execute some other statement (the loop
body).
∙ In C, every loop has a controlling expression.
∙ Each time the loop body is executed (an iteration of the loop), the controlling expression
is evaluated.
– If the expression is true (has a value that’s not zero) the loop continues to execute.
∙ The while statement is used for loops whose controlling expression is tested before the
loop body is executed.
∙ The do statement is used if the expression is tested after the loop body is executed. ∙ The
for statement is convenient for loops that increment or decrement a counting variable.
Page 20 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
∙ The process continues until the controlling expression eventually has the value zero.
Page 21 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
Infinite Loops
A while statement won’t terminate if the controlling expression always has a nonzero value.
C programmers sometimes deliberately create an infinite loop by using a nonzero constant as
the controlling expression:
while (1) …
A while statement of this form will execute forever unless its body contains a statement that
transfers control out of the loop (break, goto, return) or calls a function that causes the
program to terminate.
Page 22 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
int i, n;
return 0;
}
Page 23 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
The do Statement
Page 24 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
do {
n /= 10; digits++;
} while (n > 0);
printf("The number has %d digit(s).\n", digits); return 0;
}
Page 25 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
while (i > 0) {
printf("T minus %d and counting\n", i); i--; }
for Statement Idioms
The for statement is usually the best choice for loops that ―count up‖ (increment a
variable) or ―count down‖ (decrement a variable).
A for statement that counts up or down a total of n
times will usually have one of the following forms:
∙ On occasion, a for statement may need to have two (or more) initialization expressions
or one that increments several variables each time through the loop.
∙ This effect can be accomplished by using a comma expression as the first or third
expression in the for statement.
A comma expression has the form
expr1 , expr2
where expr1 and expr2 are any two expressions.
Page 26 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
printf("This program prints a table of squares.\n"); printf("Enter number of entries in table: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) printf("%10d%10d\n", i, i * i);
return 0;
}
∙ The break statement can transfer control out of a switch statement, but it can also be used
to jump out of a while, do, or for loop.
∙ A loop that checks whether a number n is prime can use a break statement to terminate the
loop as soon as a divisor is found:
for (d = 2; d < n; d++) if (n % d == 0)
break;
Page 27 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
∙ The break statement is particularly useful for writing loops in which the exit point is in
the middle of the body rather than at the beginning or end.
Loops that read user input, terminating when a particular value is entered, often fall
into this category:
for (;;) {
printf("Enter a number (enter 0 to stop): "); scanf("%d", &n);
if (n == 0) break;
printf("%d cubed is %d\n", n, n * n * n);
}
∙ A break statement transfers control out of the innermost enclosing while, do, for, or
switch.
∙ When these statements are nested, the break statement can escape only one level of
nesting.
Example:
while (…) { switch (…) {
…
break;
…
}
}
∙ break transfers control out of the switch statement, but not out of the while loop.
Page 28 of 29
Introduction to C Programming - 22CPR13/23 UNIT - 2
control leaves the loop; with continue, control remains inside the loop. ∙ There’s another
difference between break and continue: break can be used in switch statements and loops
(while, do, and for), whereas continue is limited to loops.
sum = 0;
while (n < 10) { scanf("%d", &i); if (i == 0)
continue; sum += i; n++;
/* continue jumps to here */
}
Page 29 of 29