Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Conditional Statements and Loops

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

CONDITIONAL STATEMENTS IN C

•The conditional statements (also known as decision control


structures) such as if, if else, switch, etc. are used for
decision-making purposes in C programs
.
•They are also known as Decision-Making Statements and
are used to evaluate one or more conditions and make the
decision whether to execute a set of statements or not.

•Conditional statements are used to execute a set of


statements on some conditions. It provides a unit block in
which we can execute either one statements or more than
one statements. If the given condition is true then the
set of statements are executed otherwise body of the
statement is skipped.
Control Statement: A statement used to alter the sequential flow of a program. C
provides two types of control statements:
1.Selection 2. Iteration (loop)
Selection: Using selection, statements can be executed conditionally. That is, if a
condition is true, then one sequence of statements will be executed.
Iteration: Using iteration, a set of statements to be performed repeatedly until a
certain condition is fulfilled. The iteration statements are also called as loops or
looping statements.
Different types of conditional control structures available in 'C' are (Conditional
statements)
1) if - statement
2) if else statement
3) Nested if statement
4) if-else-if Ladder
4) switch statement

Jump Statements: ( Unconditional statements)


1. break
2. continue
3. goto
4. return
Iteration Statements:

1. For Loop
2. While Loop
3. Do - while Loop
1. If Condition:
The if in C is the most simple decision-making statement. It
consists of the test condition and if block or body. If the given
condition is true only then the if block will be executed.
The general form of simple if ( ) statement is

if (test expression) Flowchart C


{
Statement block;
}
statement – x;

OR
if(condition)
{
// Statements to execute
// if condition is true
}
// C Program to demonstrate the syntax of if statement

#include <stdio.h>
int main()
{
int num = 9;
// if statement with true condition
if (num < 10) {
printf("%d is less than 10", num);
}
// if statement with false condition
if (num> 20) {
printf("%d is greater than 20", num);
}
return 0;
}

Output:
9 is less than 10
// Program to display a number if it is negative

#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0)
{
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}

Output 1: Output 2:

Enter an integer: -2 Enter an integer: 5


You entered -2. The if statement is easy.
The if statement is easy.
2. if- else Condition
The if statement alone tells us that if a condition is true it will execute a
block of statements and if the condition is false it won’ t. But what if we
want to do something else when the condition is false? We can use
the statement with the statement to execute a block of code when
the condition is false. The if-else statement consists of two blocks, one for
false expression and one for true expression.

Syntax:
if (test expression)
{
true-block statements;
}
else
{
false-block statement;
}
statement-x;
FlowChart
if (condition)
{
// Executes this block
if
// condition is true
}
else
{
// Executes this block
if
// condition is false
}
Example:
// C Program to demonstrate the use of if-else statement
#include <stdio.h>
int main()
{
// if block with condition at the start
if (5 < 10){
// will be executed if the condition is true Output:
printf("5 is less than 10."); 5 is less than
} 10.
// else block after the if block
else {
// will be executed if the condition is false
printf("5 is greater that 10.");
}
return 0;
}
Check whether an integer is odd or even
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Output:
Enter an integer: 4
4 is an even integer.
/* program to check if the given two numbers are equal or not */
#include <stdio.h>
int main()
{
int num1 , num2;
printf(" Enter the values for Numl and Num2\n");
scanf("%d %d", &numl,&num2);
if(numl = = num2) {
printf("Entered numbers are equal\n");
}
else{
printf("Entered numbers are not equal\n");
}
return 0;
Output:
}
Enter the values for Numl and Num2
2 4
Entered numbers are not equal
3. Nested if-else Condition:

•Nested if- else statements are useful when there are


multiple conditions and different statements are to be
executed for each condition,
• Nested if statements means an if statement inside
another if statement.
• C allow us to nested if statements within if statements,
i.e, we can place an if statement inside another if
statement.
if (test expression 1)
{
if (test expression 2)
{
statement block-1;
}
else
{
statement block-2;
}
}
else
{
statement block-3;
}
statement-x;
Example / program to find the largest among three numbers*/
#inc1ude <stdio.h>
int main()
{
int a,b,c; Output:
printf(" Enter the values for A,B,C\n” ');
scanf("%d %d %d", &a,&b,&c); Enter the values for A,B,C
if( a > b ) 10 20 30
{ C is the largest
if ( a > c)
printf(" A is the largest\n"); Enter the values for A,B,C
else 30 10 20
printf("C is the largest\n"); A is the largest
}
else Enter the values for A,B,C
{ 20 30 10
if ( b> c) B is the largest
printf(" B is the largest\n");
else
printf("C is the largest\n");
}
}
#include <stdio.h>
int main()
{
int number1, number2; Output:
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2); Enter two integers: 5 10
if (number1 >= number2) Result: 5 < 10
{
if (number1 == number2) Enter two integers: 10 5
{ Result: 10 > 5
printf("Result: %d = %d",number1,number2);
} Enter two integers: 10 10
else Result: 10 = 10
{
printf("Result: %d > %d", number1, number2);
}
}
else
{
printf("Result: %d < %d",number1, number2);
}
return 0; }
4.if else if ladder
•When multiple decisions are involved, we can make use of if -
elseif - else statements.
•if else if ladder in C programming is used to test a series of
conditions sequentially.
•Furthermore, if a condition is tested only when all previous if
conditions in the if-else ladder are false. If any of the
conditional expressions evaluate to be true, the appropriate code
block will be executed, and the entire if-else ladder will be
terminated.
Its general form is given below:
if( test expression -I)
{ else
statement block -1; {
} statement block;
}
else if (test expression-2)
statement-x;
{
statement block-2;
}
else if(test expression-3)
{
Statement block-3;
}
else if (test expression-n)
{
statement block-n;
}
#include <stdio.h>
int main()
{
int marks; Output:
printf(" Enter Marks \n");
scanf("%d",&marks); Enter Marks 80
if((marks<=100) && (marks >=70)) Distinction
printf("Distinction");
else if(marks >=60)
printf("First Class");
else if(marks >=50)
printf("Second Class");
else if (marks >=40)
printf ("Pass Class \n");
else
printf("Fails");
return 0;
}
// C Program to check whether a number is positive, negative or 0 using if else if
ladder
#include <stdio.h>
int main()
{
int n = 0;
// all Positive numbers will make this
Output
// condition true
if (n > 0) { Zero
printf("Positive");
}
// all Negative numbers will make this
// condition true
else if (n < 0) {
printf("Negative");
}
// if a number is neither Positive nor Negative
else {
printf("Zero");
}
return 0;
}
5. Switch Statement

The switch case statement is an alternative to the if else if


ladder that can be used to execute the conditional code based
on the value of the variable specified in the switch statement.
The switch block consists of cases to be executed based on
the value of the switch variable.

Depending on the expression, the control is transferred to


that particular case label and executed the statements under
it. If none of the cases are matched with the switch
expression, then the default statement is executed.
Syntax:
switch (discrete_variable or expression)
{
case value_1:
Statements;
break;
case value_2:
Statements;
break;
case value_3:
Statements;
break;
… … ..
………
case value_n:
Statements;
break;
default :
Statements;
break;
}
Example: Program to accept day number and display corresponding week day.
#include<stdio.h>
int main()
{ case 6: printf("Friday");
int dayno; break;
printf(" Enter Day number \n"); case 7: printf("Saturday");
scanf("%d",&dayno); break;
switch(dayno) default: printf("Invalid Day
{ number");
case 1: printf("Sunday"); }
break; }
case 2: printf("Monday");
break; Output:
case 3: printf("Tuesday"); Enter Day number:1
break; Sunday
case 4: printf("Wednesday");
break;
case 5: printf("Thursday");
break;
Jump Statements in C

In c, there are control statements that do not


need any condition to control the program
execution flow. These control statements are
called as unconditional control statements.

C provides three types of Jump Statements


• break
• continue
• goto.
break:
In C, the break statement is used to perform the following
two things..
• break statement is used to terminate the switch case
statement
• break statement is also used to terminate looping
statements like while, do-while and for.

• When a break statement is encountered inside the


switch case statement, the execution control moves out
of the switch statement directly.

• the program control jumps to the statement following


the loop or switch.
Example:

#include <stdio.h>
int main() { Output:
int i;
for (i = 0; i < 10; i++) 0
{ 1
if (i == 4) 2
{ 3
break;
}
printf("%d\n", i);
}
return 0;
}
continue statement

• The continue statement is used to move the program execution


control to the beginning of the looping statement.

• continue statement: The continue statement is used to skip the


current iteration of a loop and continue with the next iteration.
When the continue statement is encountered, the control is
transferred to the beginning of the loop.
• The continue statement breaks one iteration (in the loop), if
a specified condition occurs, and continues with the next
iteration in the loop.
Example for continue

#include <stdio.h> Output:


int main() {
0
int i; 1
for (i = 0; i < 10; i++) 2
{ 3
5
if (i == 4) 6
{ 7
continue; 8
9
}
printf("%d\n", i);
}

return 0;
}
goto statement

• The goto statement can be used to jump from anywhere to


anywhere within a function.

• goto statement is used to transfer control to a specified label


within the same function.

• Using goto statement we can jump from top to bottom or


bottom to top. To jump from one line to another line, the goto
statement requires a label.

• Label is a name given to the instruction or line in the program.

• When we use a goto statement in the program, the execution


control directly jumps to the line with the specified label.
Syntax:

Syntax1 |
Syntax2
----------------------------
------------
goto label; |
label:
.
| .
.
| .
// C program to check if a number is even or not using goto statement
#include <stdio.h>
void EvenOrNot(int num)
{
if (num % 2 == 0)
goto even;
else
goto odd;
even:
printf("%d is even", num);
return;
odd:
printf("%d is odd", num);
}

int main()
{
int num = 26;
checkEvenOrNot(num);
return 0;
}
// C program to print numbers from 1 to 10 using goto statement
#include <stdio.h>

// function to print numbers from 1 to 10


void printNumbers()
{
int n = 1;
label:
printf("%d ", n);
n++;
if (n <= 10)
goto label;
}

// Driver program to test above function


int main()
{
printNumbers();
return 0;
}
Loops in C

Loops in programming are used to repeat a block of code until


the specified condition is met.

A loop statement allows programmers to execute a statement or


group of statements multiple times without repetition of code

The looping statements are used to execute a single statement


or block of statements repeatedly until the given condition is
FALSE.
There are mainly two types of loops in C Programming:

Entry Controlled loops: In Entry controlled loops the test


condition is checked before entering the main body of the loop.
For Loop and While Loop is Entry-controlled loops.

Exit Controlled loops: In Exit controlled loops the test condition


is evaluated at the end of the loop body. The loop body will
execute at least once, irrespective of whether the condition is
true or false.
do-while Loop is Exit Controlled loop.
for loop:

•The for statement is used to execute a single statement or a block


of statements repeatedly as long as the given condition is TRUE.

•for loop enables programmers to perform n number of steps


together in a single line.

Syntax:

for(initialize expression; test expression; update expression)


{
// // body of for loop //
}
// C program to illustrate for loop
#include <stdio.h>
int main() Output:
{
int i = 0; Hello World
Hello World
for (i = 1; i <= 10; i++) Hello World
{ Hello World
printf( "Hello World\n"); Hello World
} Hello World
return 0; Hello World
Hello World
}
Hello World
Hello World
The example below will print the numbers 0 to 4:

#include <stdio.h>
int main()
{ Output:
int i;
for (i = 0; i < 5; i++) 0
{ 1
printf("%d\n", i); 2
} 3
return 0; 4
}
While Loop

•The while statement is used to execute a single statement or


block of statements repeatedly as long as the given condition is
TRUE.

•The while statement is also known as Entry control looping


statement.

•While loop does not depend upon the number of iterations.

•In for loop the number of iterations was previously known to us


but in the While loop, the execution is terminated on the basis
of the test condition.
Flow Chart
Syntax:

initialization_expression;
while (test_expression)
{
// body of the while
loop
update_expression;
}
The example below will print the numbers 0 to 4:

#include <stdio.h>
int main()
{
Output:
int i = 0;
while (i < 5)
0
{
1
printf("%d\n", i);
2
i++;
3
}
4
return 0;
}
do-while Loop:

The do-while statement is used to execute a single


statement or block of statements repeatedly as long as given
the condition is TRUE. The do-while statement is also known
as the Exit control looping statement.

This loop will execute the code block once, before checking
if the condition is true, then it will repeat the loop as long
as the condition is true.
Syntax:

initialization_expression;
do
{
// body of do-while loop
update_expression;
}
while (test_expression);
The example below will print the numbers 0 to 4:

#include <stdio.h>
int main() {
int i = 0;
do Output:
{
printf("%d\n", i); 0
i++; 1
} 2
while (i < 5); 3
return 0; 4
}

You might also like