1. Conditional Statements
In our daily life we do many things which depends on some
kind of conditions for e.g. If I study I will pass exams. If he
is hungry he will eat etc.
So in programming languages also we can perform different
sets of actions depending on the circumstances(.)حاالت
There are three major decision making instructions:
If statement.
If else statement.
If else If statement.
2. The If statement
If statement is the simplest conditional statement.
If enables us to check condition is true or false.
When the condition is true it will execute the statement and
when false it
will not execute the statement.
If Statement syntax:
if(condition is true)
{
execute statements;
}
4. The If statement
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of if is executed.
4. If condition is false then body will not be executed and
program will end.
5. The program will end.
5. The If statement Example
Code:
main( )
{
int num ;
printf ( “Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( “Nice Choice !" ) ;
}
6. The If else Statement
If else is another useful statement.
It executes statement 1 if the condition is true, and another if its false.
The simplest form of if else statement:
If Else Statement syntax:
if (expression)
{
statement 1;
}
else
{
statement 2;
}
8. The If else
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of if is executed.
4. If condition is false then body of else is executed.
5. The program will end.
9. The If Else If Statement
When you have multiple expressions to evaluate(لگانہ ,)اندازہ
you can use the if. Else if is form of the if statement.
11. The If Else If statement
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of if is executed.
4. If condition is false then next condition is checked and body of
if-else is
executed.
5. The condition is checked again and again until the condition
come true.
6. If none of else-if condition is true the body of else is executed.
7. The program will end.
12. If Else If Example
main( )
{
int m1, m2, m3, m4, m5, per ;
per = ( m1+ m2 + m3 + m4+ m5 ) / per ;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}
13. Loops in C
Loops are used to repeat a block of code.
Being able to have your program repeatedly execute a
block of code is one of the most basic but useful tasks in
programming.
If you want to do the same thing many times.
For instance you want to print the same words ten times.
14. Loops in C
There are four basic types of loops which are:
For Loop
While Loop
Do While Loop
Nested Loop
15. Whenever we need to execute certain action
multiple times, we need to wrap programming
statement in the loop body.
The For Loop
17. The For Loop
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of for loop is executed.
4. Increment or decrement takes place.
5. the condition is checked again
6. Step 2 and 3 are checked again until the condition become
false.
7. The program will end.
21. The While Loop
1. Initialization takes place.
2. Condition is checked.
3. If condition is true the body of while loop is executed.
4. Increment or decrement takes place.
5. the condition is checked again.
6. Step 2 and 3 are checked again until the condition become
false.
7. The program will end.
23. The Do While Loop
Unlike for and while loops, which test the loop
condition at the top of the loop, the do while loop in C
programming checks its condition at the bottom( آخرمیں )
of the loop.
A do while loop is similar to a while loop, except the
fact that it is guaranteed to execute at least one time.
26. The Do While Loop
1. Initialization takes place.
2. The body of do-while loop is executed.
3. Increment or decrement takes place.
4. Condition is checked.
5. If condition is true then body of loop is executed else it
will terminate( ختمکرنا ).
6. If condition is true then Step 2 and 3 are executed again
until the condition become false.
7. The program will end