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

Cse 122

This document discusses conditional logic statements in C programming, including if and if-else statements, describing their syntax and providing examples of code using these statements to check conditions and execute different code blocks based on whether the conditions are true or false, such as programs to find the larger of two numbers and determine if a number is even or odd. It also discusses nested if-else statements and the if-else if-else construct for checking multiple conditions.

Uploaded by

Shamima Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Cse 122

This document discusses conditional logic statements in C programming, including if and if-else statements, describing their syntax and providing examples of code using these statements to check conditions and execute different code blocks based on whether the conditions are true or false, such as programs to find the larger of two numbers and determine if a number is even or odd. It also discusses nested if-else statements and the if-else if-else construct for checking multiple conditions.

Uploaded by

Shamima Hossain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Daffodil International University

Department of Electrical and Electronic Engineering


CSE 122: Computer Programming Lab

Experiment Name 4: Programming with Conditional Logic Statements

Objectives:

I. The C conditional statement, a selection.


II. The if constructs

Theory:

If statement is a conditional branching statement. In conditional branching statement a condition is


evaluated, if it is evaluate true a group of statement is executed. The simple format of an if statement is
as follows:
if (expression)
statement;
If the expression is evaluated and found to be true, the single statement following the "if" is executed. If
false, the following statement is skipped. Here a compound statement composed of several statements
bounded by braces can replace the single statement.
If else statement:
This feature permits the programmer to write a single comparison, and then execute one of
the two statements depending upon whether the test expression is true or false. The general form of
the if-else statement is
if(expression)
statement1
else
statement2
Here also expression in parentheses must evaluate to (a boolean) true or false. Typically you're testing
something to see if it's true, and then running a code block(one or more statements) if it is true, and
another block of code if it isn't. The statement1 or statement2 can be either simple or compound
statement.
The following program demonstrates a legal if else statement:

#include <stdio.h>
int main ()
{/* local variable definition */
int a = 100;
/* check the boolean condition */
if( a < 20 ) {
/* if condition is true then print the following */
printf("a is less than 20\n" ); }
else {

/* if condition is false then print the following */

printf("a is not less than 20\n" );

}
printf("value of a is : %d\n", a);

You can set up an if-else statement to test for multiple conditions. The following example uses two
conditions so that if the first test fails, we want to perform a second test before deciding what to do:
if (x%2==0)
{
printf(“x is an even number”);
}
else
{
if (x>10)
{
printf(“x is an odd number and greater than 10”);
}
else
{
printf(“x is an odd number and less than 10”);
}
}
This brings up the other if-else construct, the if, else if, else. This construct is useful where
two or more alternatives are available for selection.
The syntax is
if(condition)
statement 1;
else if (condition)
statement 2;
.....................
.....................
else if(condition)
statement n-1;
else
statemens n ;
The various conditions are evaluated one by one starting from top to bottom, on reaching a
condition evaluating to true the statement group associated with it are executed and skip other
statements. If none of expression is evaluate to true, then the statement or group of statement
associated with the final else is executed.

Code 1: Write a program that will read two numbers and it will find the largest number between them

#include <stdio.h>
#include<conio.h>

main ()
{
int a,b;
printf("Enter an integer: \n");
scanf("%d%d", &a,&b);
{
if (a>b)
printf("The a=%d is bigger!\n",a);
else
printf ("b=%d is bigger ",b);
}
}
Code 2: Write a code in C to check if a number is even or odd

#include <stdio.h>

int main()
{
int n;
printf("Enter an integer\n");
scanf("%d", &n);
if (n%2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}

Exercise 2:

1. Write a program that will read three numbers and it will find the largest number among them.
2. Write Algorithm of the program
3. Draw the flowchart of the program

You might also like