2. if-else Statement
if-else Statement
◻ In simple ‘if’ statement, when the condition is true, a set of statements are
executed. But when it is false, there is no alternate set of statements
◻ The statement ‘else’ provides the same
Syntax:
if (testExpression)
{
// codes inside the body of if
}
else
{
// codes inside the body of else
}
2
3. If-else Statement (Cont’d)
else Statement
Example:
if (Duration >= 3) {
RateOfInterest = 6.0;
}
else {
RateOfInterest = 5.5;
}
else if Statement
◻ The ‘else if’ statement is to check for a sequence of conditions
◻ When one condition is false, it checks for the next condition and so on
◻ When all the conditions are false the ‘else’ block is executed
◻ The statements in that conditional block are executed and the other ‘if’ statements are
skipped
3
4. Nested if Statement
4
Syntax:
if (condition-1)
{
Statement 1;
if (condition-2)
{
Statement 2;
}
else
{
Statement n;
}
}
else {
Statement x;
}
Next Statement;
Nested if Statement
• An ‘if’ statement embedded within another ‘if’
statement is called as nested ‘if’
• Example:
if (iDuration > 6 )
{
if (dPrincipalAmount > 25000)
{
printf(“Your percentage of incentive is 4%”);
}
else
{
printf(“Your percentage of incentive is 2%”);
}}
else {
printf(“No incentive”);
}
5. Example
/* Program to check whether an integer entered by the user is odd
or even */
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
/* True if remainder is 0*/
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
5
6. switch case Statement
◻ The ‘switch’ statement is a selection control structure that selects a choice
from the set of available choices.
◻ It is very similar to ‘if’ statement.
◻ But ‘switch’ statement cannot replace ‘if’ statement in all situations.
Syntax:
int n;
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
6
8. switch case Example
/*Following is a simple program to demonstrate syntax of switch.
#include <stdio.h>
void main()
{
int x =2;
switch (x)
{
case 1: printf("Choice is 1");
break;
case 2: printf("Choice is 2");
break;
case 3: printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
}
}
Output: Choice is 2
8
9. Iterational Control Structures
◻ Iterational (repetitive) control structures are used to repeat certain
statements for a specified number of times
◻ The statements are executed as long as the condition is true
◻ These kind of control structures are also called as loop control structures
◻ Three kinds of loop control structures are:
⬜ while
⬜ do while
⬜ for
9
10. while Loop Control Structure
◻ A ‘while’ loop is used to repeat certain statements as long as the condition is true
◻ When the condition becomes false, the ‘while’ loop is quitted
◻ This loop control structure is called as an entry-controlled loop because, only when the
condition is true, are the statements executed
Syntax:
while (condition)
{
Set of statements;
}
Next Statement;
Example:
int Count = 1;
while (Count <= 3)
{
printf(“%dn”,Count);
}
10
11. What is the output of the following code?
Example 1:
#include<stdio.h>
int main(){
int count=3;
while (count<=5) {
printf("%dn",count);
count++;
}
return 0;
}
11
Example 2:
#include<stdio.h>
int main(){
int count = 0;
while(count<=10)
{
printf("n PCU");
count++;
}
return 0;
}
12. do while Loop Control Structure
◻ The ‘do while’ loop is very similar to ‘while’ loop. In ‘do while’ loop, the
condition is tested at the end of the loop.
◻ Because of this, even when the condition is false, the body of the loop
is executed at least once.
◻ This is an exit-controlled loop.
Syntax:
do
{
Set of statement(s);
} while (condition);
Next Statement;
12
13. do while Loop Control Structure Example
#include<stdio.h>
int main()
{
int number, sum = 0;
do {
printf(“Enter a number or 0(zero) to end the input
”);
scanf("%d",&number);
sum = sum + number;
} while (number != 0);
return 0;
13
15. for Loop Control Structure
◻ The ‘for’ loops are similar to the other loop control structures
◻ The ‘for’ loops are generally used when certain statements have to be
executed a specific number of times
◻ Advantage of for loops:
⬜ All the three parts of a loop (initialization, condition , increment) can be given
in a single statement
⬜ Because of this, there is no chance of user missing out initialization or increment
steps which is the common programming error in ‘while’ and ‘do while’ loops
Syntax:
for (Initialization; Termination-Condition; Increment-Step)
{
Set of statement(s);
}
Next Statement;
15
16. for Loop Control Structure (cont’d)
Example:
#include<stdio.h>
int main(){
int count;
for (int count = 1; count <= 5; count+
+)
{
printf(“%dn”,count);
}
return 0;
}
16
Output:
1
2
3
4
5
17. Unconditional Statement
goto statement :
• The statement is a control transfer statement that allows the control jump to a
labelled statement within the same function. The basic syntax of the follows:
18. goto statement example:
#include <stdio.h>
int main() {
int start = 1, end = 10;
int curr = start;
print_line:
printf("%d ", curr);
if (curr < end) {
curr++;
goto print_line;
}
return 0;
}
1 2 3 4 5 6 7 8 9 10
Output :
19. Unconditional Statement
Break Statement:
• The break in C is a loop control statement that breaks out of the loop when
encountered. It can be used inside loops or switch statements to bring the control
out of the block. The break statement can only break out of a single loop at a time.
• The break statement has the following two usages:
• When the break statement is encountered inside a loop, the loop is immediately
terminated and program control resumes at the next statement following the loop.
• It can be used to terminate a case in the switch statement.
21. #include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
for (i = 1; i <= 4; ++i) {
printf("Enter n%d: ", i);
scanf("%lf", &number);
// if the user enters a negative number, break the loop
if (number < 0.0) {
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %.2lf", sum);
return 0;
}
Enter n1: 1
Enter n2: 2
Enter n3: 3
Enter n4: 4
Sum = 10.00
Output :
22. Continue Statement in C
• The continue statement is used to skip the current
iteration of any loop and bring the control to the
beginning of the iteration.
• The statements following the continue statement are
skipped and the iteration starts again.
• It does not terminate the loop but only affects the
current iteration.
23. Continue Statement in C
#include<stdio.h>
int main () {
int i = 0;
while (i <= 5) {
if (i == 3) {
i++;
continue;
}
printf("%dn", i);
i++;
}
return 0;
}
• This C program in the C Compiler iterates 05
times using a for loop.
• The continue statement is encountered when
i=3. So it skips the remainder of the loop's
body for that iteration.
• As a result, it only prints the numbers 0-2 and
4-5, skipping out 3.
Output :
0
1
2
4
5
24. Continue Statement in C
Output:
2
4
6
8
10
#include <stdio.h>
int main() {
int i;
// Print even numbers between 1 and 10
for (i = 1; i <= 10; i++) {
if (i % 2 != 0) {
// Skip odd numbers
continue;}
printf("%dn", i);
}
return 0;
}