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

C lecture 2.1

The document discusses decision-making statements in the C programming language, explaining various constructs such as simple if statements, if-else statements, nested if-else, if-else-if statements, and conditional operators. It also covers switch-case statements, goto statements, and looping constructs like while, do-while, and for loops, along with examples for each. Additionally, it highlights the use of break and continue statements in loops for controlling flow and execution.

Uploaded by

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

C lecture 2.1

The document discusses decision-making statements in the C programming language, explaining various constructs such as simple if statements, if-else statements, nested if-else, if-else-if statements, and conditional operators. It also covers switch-case statements, goto statements, and looping constructs like while, do-while, and for loops, along with examples for each. Additionally, it highlights the use of break and continue statements in loops for controlling flow and execution.

Uploaded by

devlina.karmakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Decision making in C : Decision making statements contain conditions that are evaluated by the program.

If the condition is true, then a set


of statements are executed and if the condition is false then another set of statements is executed.
Decision making is about deciding the order of execution of statements based certain conditions or repeat a group of statements until certain
specified conditions are met. C language handles decision making by supporting the following statements:
 Simple if statement
 if-else ladder
 nested if-else statement
 if –else-if statement
 Conditional operator statement

Simple if statement:
The general form of a simple if statement is,
if( expression ) // if the expression returns true, then the statement 1 will execute otherwise statement 2 will execute.
{
statement 1;
}
statement 2;
Checking greater of 2 numbers using simple if statement.
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y;
x=15;
y=13;
if(x>y) // if the condition satisfies then only the statement will execute, otherwise it won’t give any result
{
printf("x is greater than y");
}
return 0;
getch();
}
if – else statement :
The general form of a simple if-else statement is,

if (expression) // if the expression is true, statement 1 is executed , else it is skipped and statement 2 is executed.
{
statement 1;
}
else
{
statement 2;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int x, y;
x=15;
y=18;
if(x>y) //if this condition satisfies then 1st statement will execute otherwise else statement will execute
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
return 0;
getch();
}
Nested if-else statement:
When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
Syntax of Nested if else statement:
if(condition)
{
//Nested if else inside the body of "if“
if(condition2)
{
//Statements inside the body of nested "if“
}
else
{
//Statements inside the body of nested "else“
}
}
else
{
//Statements inside the body of "else“
}
Example:

#include <stdio.h>

int main()

int var1, var2;

printf("Input the value of var1:");

scanf("%d", &var1);

printf("Input the value of var2:");

scanf("%d",&var2);

if (var1 != var2)

printf("var1 is not equal to var2\n"); //Nested if else

if (var1 > var2)

printf("var1 is greater than var2\n");

else

printf("var2 is greater than var1\n");

else

printf("var1 is equal to var2\n");

return 0;
if –else-if statement:
Here, a user can decide among multiple options. The C if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions are true, then the final else statement will be executed.
Syntax:
if(condition)
{
Statement1;
Statement2;
}
else if(condition)
{
Statement 3;
statement 4;
}
else if(condition)
{
Statement 5;
Statement 6;
}
else
{
Statement 7;
Statement 8;
}
Example:
#include <stdio.h>
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
return 0;
}
Conditional / Ternary Operator:
Conditional operators return one value if condition is true and returns another value is condition is false.
This operator is also called as ternary operator.
Syntax : (Condition? true_value: false_value);
Example : (A > 100 ? 0 : 1);
In above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.

Example:

#include <stdio.h>
int main()
{
int x=1, y ;
y = ( x ==1 ? 2 : 0 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
Programs on decision making:
WAP to check whether a given number is even or odd.
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num); // True if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
}
WAP to check whether a given input alphabet is either vowel or consonant.
#include<stdio.h>
int main()
{
char x;
printf(“enter the alphabet”);
scanf(“%c”,&x);
if ( x==‘a’|| x==‘A’||x==‘e’||x==‘E’||x==‘i‘||x==‘I’||x==‘o’||x==‘O’||x==‘u’||x==‘U’)
{
printf(“the alphabet is vowel”);
}
else
{
printf(“the alphabet is consonant”);
}
rerturn 0;
}
WAP to check a year given as input is whether leap year or not.
#include<stdio.h>
int main()
{
int y;
printf("enter a year");
scanf("%d",&y);
if(y%4==0 && y%100==0 || y%400==0)
{
printf("the year is leap year");
}
else
{
printf("the year is not leap year");
}

return 0;
}
WAP to evaluate y where,

y={

1,x>0

0,x=0

-1,x<0

#include<stdio.h>

int main()

float x;

printf("enter x");

scanf("%f",&x);

if(x>0)

printf("y=1");

else

if(x=0)

printf("y=0");

else

printf("y=-1");

return 0;

}
 WAP to find the larger between two numbers.
 WAP to find out the root of any quadratic equation using Sridhar Acharya’s Formulae.(x=-b±√b2 - 4ac/2a)
 WAP to find the largest among 3 numbers.
 WAP to calculate gross salary,
if basic<10000, hra=20% of basic, da= 10% of basic
else hra=30% of basic, da=20% of basic.
 WAP to input marks of 5 subjects, calculate percentage and grade.
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
 WAP to check whether a number is divisible by 5 and 11 or not.
 WAP to check whether a triangle is equilateral, isosceles or scalene.
 WAP to input any character and check whether it is alphabet ,digit or character.
Switch Case Statement:
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched
on is checked for each switch case.
Syntax:
main()
{
int i=2;
switch(i)
{
case1: printf(“case1”); break;
case2 :printf(“case2”);break;
case3:printf(“case3”);break;
default:printf(“default”);
}
}
When a break statement is reached the switch terminates and the flow of control jumps to the next line following the switch statement. If no
break appears the flow of control will fall through to subsequent cases until a break is reached. No break is needed in the default case.
Example:
#include <stdio.h>
int main()
{
int num=2;
switch(num+2)
{
case 1:
printf("Case1: Value is: %d", num);
case 2:
printf("Case2: Value is: %d", num);
case 3:
printf("Case3: Value is: %d", num);
default:
printf("Default: Value is: %d", num);
}
return 0;
}
#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
case 1: printf("Case1 "); break;
case 2: printf("Case2 "); break;
case 3: printf("Case3 "); break;
case 4: printf("Case4 "); break;
default: printf("Default ");
}
return 0;
}
 WAP to check 7 days in a week.
 WAP find maximum between two numbers using switch case
 WAP to make a simple calculator using switch case.
A lowercase single character is entered from keyboard, find the character is vowel or consonant.
#include<stdio.h>
int main()
{
char ch;
printf("enter any character");
ch=getchar();
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("vowel");
break;
default:printf("consonent");

}
return 0;
}
GO-to Statement:
The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the program control to a predefined label. The goto statment can be
used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can't be done by using a single break statement.
Syntax:
label:
//some part of the code;
goto label;
Example:
#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}
Looping Statement:
In C there are 3 types of looping statements, these are:
 While loop
 Do-while loop
 For loop
Apart from that break and continue statements are also used for branching.
Why to use loop in C for branching?
A loop is used for executing a block of statements repeatedly until a given condition returns false. C language provides statements that can alter
the flow of a sequence of instructions. These statements are called as control statements/branching statements. ... If condition becomes true
than it executes statements written in true block, if condition fails than true block will be skipped. In C, both if statements and while loops
rely on the idea of Boolean expressions.
While Loop:
A while loop in C programming repeatedly executes a target statement as long as a given condition is true.
Syntax :
while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The
loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the
loop.
Example:
#include <stdio.h>
int main ()
{
int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
return 0;
}
#include <stdio.h>
int main()
{
int i=1, j=1;
while (i <= 4 || j <= 3)
{
printf("%d %d\n",i, j);
i++;
j++;
}
return 0;
}
DO-While Loop:
A do while loop is similar to while loop with one exception that it executes the statements inside the body of do-while before checking the
condition, if a condition is false at the first place then the do while would run once, however the while loop would not run at all.
Syntax:
do
{
//Statements
} while(condition test);
Example:
#include <stdio.h>
int main()
{
int j=0;
do
{
printf("Value of variable j is: %d\n", j);
j++;
}
while (j<=3);
return 0;
}
#include <stdio.h>
int main()
{
int i=0;
while(i==1)
{
printf("while vs do-while");
}
printf("Out of loop");
return 0;
}
Difference between While and Do-while Loop:

While Loop DO-While Loop

Condition is checked first then statement(s) is Statement(s) is executed at least once, thereafter
executed. condition is checked.
No semicolon at the end of while. Semicolon at the end of while.
while(condition) while(condition);
If there is a single statement, brackets are not Brackets are always required.
required.
while loop is entry controlled loop. do-while loop is exit controlled loop.

It might occur statement(s) is executed zero At least once the statement(s) is executed.
times, If condition is false.
while(condition) do { statement(s); }
{ statement(s); } while(condition);
For Loop:
A for loop is a repetition control structure that allows us to efficiently write a loop that needs to execute a specific number of times.
Syntax :
for ( initialization; condition; increment /decrement)
{
statement(s);
}
 The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
 The condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of
control jumps to the next statement just after the 'for' loop.
 After the body of the 'for' loop executes, the flow of control jumps back up to the increment/decrement statement. This statement allows you
to update any loop control variables.
Example:

#include <stdio.h>
int main ()
{
int a;
for( a = 10; a < 20; a = a + 1 )
{
printf("value of a: %d\n", a);
}
return 0;
}
Break and Continue Statement :
In any loop break is used to jump out of loop skipping the code below it without caring about the test condition.
It interrupts the flow of the program by breaking the loop and continues the execution of code which is outside the loop.
The common use of break statement is in switch case where it is used to skip remaining part of the code.
Syntax:
In while loop In do…while loop In for loop

while (test_condition) Do for (int-exp; test-exp; update-


{ { exp)
statement1; statement1; {
if (condition ) if (condition) statement1;
break; break; if (condition)
statement2; statement2; break;
} }while (test_condition); statement2;
}

break statement is always used with if statement inside a loop and loop will be terminated whenever break statement is encountered.
Example of Break Statement:
#include <stdio.h>
int main ()
{
int a;
while (1)
{
printf("enter the number:");
scanf("%d", &a);
if ( a == 0 )
break;
}
return 0;
}
Continue Statement:
Like a break statement, continue statement is also used with if condition inside the loop to alter the flow of control.
When used in while, for or do...while loop, it skips the remaining statements in the body of that loop and performs the next iteration of the loop.
Unlike break statement, continue statement when encountered doesn’t terminate the loop, rather interrupts a particular iteration.

Syntax:
In while loop In do…while loop In for loop

while (test_condition) Do for (int-exp; test-exp; update-


{ { exp)
statement1; statement1; {
if (condition ) if (condition) statement1;
continue; continue; if (condition)
statement2; statement2; continue;
} }while (test_condition); statement2;
}
Example:
#include <stdio.h>
int main ()
{
int a,sum = 0;
for (a = 0; a < 10; a++)
{
if ( a % 2 == 0 )
continue;
sum = sum + a;
}
printf("sum = %d",sum);
return 0;
}
Practice programs:
While loop & do…..while loop
WAP to find reverse of any number.
WAP to check whether a given number is palindrome or not.
WAP to check whether a given number is Armstrong number or not.
WAP to check whether a given number is perfect number or not.
WAP to check whether a given number is a perfect square number or not.
WAP to print sum of digits of a number using while loop.

For loop
WAP to print series of all prime numbers within the range 10-20.
WAP to find the sum of n natural numbers.
WAP to find factorial of any number.
WAP to print multiplication table of any number.
WAP to print the sum of Fibonacci series: 0,1,12,3,5,8,13,21…. upto n numbers.
WAP to print even numbers evenly within the range 1-20.
WAP to print sum of : S= ½+2/3+3/4+…….. upto n terms.
WAP to print sum of: S=1+1/4+1/9+….. upto n terms.
S=(1*2)+(2*3)+…..+(19*20)
S=1+(1+2)+(1+2+3)+…… upto n terms.
S=1-1/2!+1/3!-1/4!+…. upto n terms.
S=1/2+1/4+1/8+1/16+….. upto n terms.
S=a+a²/2+a³/3+…….+a^n/n
Print the pattern :
1 12345 54321 1234 *
12 1234 5432 567 **
123 123 543 89 ***
1234 12 54 10 ****
12345 1 5 *****

***** *****
**** ****
*** ***
** **
* *

You might also like