0% found this document useful (0 votes)
6 views

Lecture - 3 Slides - Done

Uploaded by

Kaphenzu World
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lecture - 3 Slides - Done

Uploaded by

Kaphenzu World
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Week_3 slides

There are 3 types of loops in C programming: #include<stdio.h>


1. for loop void main()

Looping Statements 2. while loop {


3. do ­ while loop int i;
Introduction
Syntax for(i=1;i<=5;i++)
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied, i.e., loops are
For loop {
used in performing repetitive work in programming.
for(intialization;condition;increment/decrement) printf("welcome\n");
Suppose you want to execute welcome for 5 times. You can perform it by writing printf statement for 5 times like as
{ }
#include<stdio.h>
statements;
void main() } The output for the above program is it will print the
{ welcome statement for 5 times.
The for loop constructs provides a compact way of specifying the statements that control the repetition of the steps
printf("welcome");
within the loop. In the for construct, the loop control statements are not written within the body of the loop, instead
printf("welcome"); they are written at the top. The for statement consists of the keyword for followed by parentheses containing three
expressions, each separated by a semicolon. These are initialization, condition, and the increment/decrement .
printf("welcome");
Initialization:
printf("welcome");
The initialization is executed only once, when the control is passed to the loop for the first time. It gives the loop
printf("welcome"); variable an initial value.
} condition:
But the same operation can perform by writing printf statement only one time and repeat the execution 5 times using The condition is executed each time the control passes to the beginning of the loop. The body of the loop is
loop. executed only after the condition has been checked. If the condition evaluates to true, the loop is executed,
otherwise the control passes to the statement following the body of the loop.
Increment /Decrement :
The increment/decrement is always executed when the control returns to the beginning of the loop.

//program to print values from 5 to 1

// program to print values from 1 to 3 #include<stdio.h>

#include<stdio.h> void main()

void main() {

{
int i; int i;

printf("The output values are\n"); for(i=5;i>=1;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

{ Comparing while loop with for loop


While loop for loop
int i; syntax syntax
for(i=1;i>5;i­­) ­ The condition is wrong so that the control will not get inside of the for loop and no intialization; for(intialization;condition;increment/decrement)
statement will get executed
while(condition)
{
{ {
printf("%d\n", i);
statements; statements;
}
increment/decrement; }
getch();
}
}

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>

#include<stdio.h> void main() void main()


{ {
void main()
int i; int i;
{
printf("The output values are\n"); printf("The output values are\n");
int i; i=5; i=2;
printf("The output values are\n"); while(i>=1) while(i<=10)
i=3; { {
while(i<=3) printf("%d\n",i); printf("%d\n",i);

{ i­­; i =i+2;

printf("%d\n",i); } }

i++; getch(); getch();


} }
}
getch();
}

// 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...while Loop #include<stdio.h>


In C, do...while loop construct is very similar to while loop construct, as both iterate until the specified loop condition becomes false. Only void main()
difference between these two loops is that, in while loops, test expression is checked at first but, in do...while loop code is executed at first
then the condition is checked. So, the code are executed at least once in do...while loops. {
Note: there is semicolon in the end of while (); in do...while loop. int i = 0;
Syntax // program to print numbers from 1 to 3 {
do #include<stdio.h> printf("%d\n", i);
{ void main() }while(i!=0) ; ­ even though the expression is false, at least one time the loop get executed so that it will
statements; { print the value of i (i.e i=0)

}while(condition); int i=1; getch();

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
Entry­Controlled Loop Exit­Controlled Loop

condition ? result1 : result2


Loop Condition has to be initially
TRUE for body to be executed
Loop body will be executed at­least once If the condition is true, result1 is returned else result2 is returned.
Syntax : Example :
do
while(condition)
{ #include<stdio.h>
{
statement...
statement... void main ()
statement...
statement...
} while(condition); {
}
int x,y=10;
x = (y < 10) ? 30:40;
printf("value of x = %d",x);
getch();
} The output for the above program is value of x=40
It will check the condition 10<10, y value is not less than but it equals to 10 so the condition
returns false. If the condition returns false, 40 value will be printed as x value.

#include<stdio.h>
void main ()
{
int y=10;
(y <= 10)?printf("true"):printf("false");
getch();
}
The output for the above program is true

You might also like