Unit 2
Unit 2
1. Wake up;
2. Get ready;
3. If you have enough time, then
eat breakfast;
©LPU CSE101 C Programming
4. Go to school.
Control Statements
• The C language programs until now follows a
sequential form of execution of statements.
• C language provides statements that can alter the
flow of a sequence of instructions. These statements
are called control statements.
• These statements help to jump from one part of the
program to another. The control transfer may be
conditional or unconditional.
Go!!!
Where
Class To Movie
Go?
Stop Stop
©LPU CSE101 C Programming
if statement
if (expression)
statement;
or
if (expression)
{
block of statements;
}
yes no
Clouds?
No rain
Raining
Grab
something to
eat along
MESSAG
E
DISPLAY
}
A. Nothing will be printed
B. Compile time error
C. Hello
D. Logical error
66.70
Grade D
or
78.00
Grade C C Programming
©LPU CSE101
Q1
#include <stdio.h>
int main() A. inside if
{ B. inside elseif
int x = 1; C. inside if
if (x > 0) inside elseif
printf("inside if\n");
D. Compile time error
else if (x > 0)
printf("inside elseif\n");
}
// Nested - if statement
// Will only be executed if statement above is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
Day= No Day=
Monday Sunday
Yes
case 2.5:
printf(“A line”); Floating point number
break; cannot be used
case 3 + 7.7:
printf(“A triangle”);
case 3 + 7.7: Floating point number
printf(“A triangle”); cannot be used and
break; same expression cannot
case count+5: be used
printf(“A pentagon”);
break;
} constant expression
should be used
©LPU CSE101 C Programming
#include<stdio.h>
int main()
{
int pt;
printf("Enter the number of nodes:");
Program to
scanf("%d", &pt);
switch(pt){ show switch
case 0:
printf("\nNo Geometry");
break;
statement in
case 1:
printf("\nA point");
break;
geometry
case 2:
printf("\nA line");
break;
case 3:
printf("\nA triangle");
break;
case 4:
printf("\nA rectangle");
break;
case 5:
printf("\nA pentagon");
break;
default:
printf("Invalid input");
break;
}
return 0;
}
890
890
890
-890
32000
200000000
890
3246435674
1.234568e+006
-1.234568e+006
1.234568E+006
1234567.890000
1.234568e+006
1.234568E+006
A
This is string
This is string
int main()
{
printf( "%4d\n", 123 );
printf( "%4d\n", 1234 );
printf( "%4d\n\n", 12345 );
123
1234
12345
Example:
char c;
c = getchar();
©LPU CSE101 C Programming
#include<stdio.h>
int main()
{
char c;
printf(“enter a character”);
c=getchar();
printf(“c = %c ”,c);
}
Enter a character k
c = k
enter a character: r
r
getche();
Syntax
char str[length of string in number];
gets(str);
puts(str);
A. 12.678
B. 12.6
C. 12.679
D. 12.0
A. 3.460000
B. 3.000000
C. 4.000000
D. 3.500000
A. 3.010000
B. 3.000000
C. 4.000000
D. 3.500000
A. 1.000000
B. 3.000000
C. 1.428571
D. Error
A. ABC
B. ABC 1
C. 3 ABC
D. ABC 3
return 0;
}
Output:
sum=2
10 9 8
10 8 6 4 2
n=10;
A:
printf(“%d “, n);
n = n -1;
if (n>0)
goto A;
Output:
10 9 8 7 6 5 4 3 2 1
enter a number: 18
18 is even
A. int
B. char
C. long
D. All of the above
A. int
B. char
C. long
D. All of the above
• Body – a statement or a
block of statements that
will be repeated.
• Condition – is used to
control the iteration –
either to continue or stop
iterating.
10 9 8 7 6 5 4 3 2 1
Do TEN push ups = for
count=1; count<=10;
count++
©LPU CSE101 C Programming
©LPU CSE101 C Programming
Program to find sum of first n numbers
#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("enter the value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("the sum=%d",sum);
return 0;
}
}
printf("the sum of digits=%d",sum);
}
…
}
}
• The above loop will run for 100*50 iterations.
Enter a number
4
The tables from 1 to 4
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12CSE101
©LPU 16 20 24 28 32 36 40
C Programming
#include<stdio.h>
int main() Program to
{
int i,j;
display a
printf(“Displaying right angled triangle for 5 pattern.
rows”);
for(i=1 ; i<=5 ; i++) {
for(j=1 ; j<=i ; j++)
printf(“* ”);
printf(“\n”);
}
return 0;
}
5-28
©LPU CSE101 C Programming
do…while statement
do
{
Repeated_Actions;
} while (Condition);
10 9 8 7 6 5 4 3 2 1
B. In while loop
In while loop