Lecture - 3 Slides - Done
Lecture - 3 Slides - Done
void main() {
{
int i; int i;
for(i=1;i<=3;i++) {
{ printf("%d\n", i);
printf("%d\n",i); }
getch();
}
}
getch();
}
//program to print even numbers from 2 to 10 //Program to print even numbers range should entered by user
#include<stdio.h> #include<stdio.h>
void main() void main()
{
{
int i;
int i,n;
for(i=2;i<=10;i+=2)
printf("Enter the range");
{
printf("%d\n",i); scanf("%d",&n);
} for(i=2;i<=n;i+=2)
getch(); {
} printf("%d\n",i);
}
getch();
}
1
week_3 slides
While Loop
//program to print values from 1 to 4
The while loop is a looping construct available in c language. The while loop continues until the evaluating
#include<stdio.h> condition/expression becomes false. The evaluating condition has to be a logical expression and must
void main() either a true or false value. The variable that is checked in the boolean expression is called control variable
Example program for while loop // program to print values from 5 to 1 // program to print even numbers up to 10
// program to print values from 1 to 3 #include<stdio.h> #include<stdio.h>
{ i; i =i+2;
printf("%d\n",i); } }
// program to print odd values and get the range value from the user what is the output of the program?
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int i,n; int i = 0;
printf("Enter the values for range"); while(i!=0) The condition is wrong so that the control will not get inside of the while loop and no statement
will get executed
scanf("%d", &n);
{
i=1;
printf("%d\n", i);
while(i<=n)
i++;
{
}
printf("%d\n",i);
getch();
i= i+2;
}
}
getch();
}
2
week_3 slides
do }
{
printf("%d", i);
i++;
}while (i<=3);
}
At first codes inside body of do is executed. Then, the condition is checked. If it is true, code/s inside body of do are executed again and the
process continues until test expression becomes false(which i values become more than 3).
Difference between while and do...while loop conditional operator (?:) or ternary operator
The conditional operator is also known as ternary operator. It is called ternary operator
because it takes three arguments. The conditional operator evaluates an expression returning
while loop do... while loop a value if that expression is true and different one if the expression is evaluated as false.
Syntax
EntryControlled Loop ExitControlled Loop
#include<stdio.h>
void main ()
{
int y=10;
(y <= 10)?printf("true"):printf("false");
getch();
}
The output for the above program is true