Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Computer Systems & Programming I

Comp-611

Department of Computer Studies


UNIVERSITY OF YANGON
Programming I

Chapter 4

Conditional Branching
Statements
4.1 The if statement

if (condition) statement;

(or)

if (condition)
{
<block of statements>;
}
Example 4.1
/*Test a given integer is positive or negative*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number:";
cin>>num;
if (num>=0) cout<<"The number is positive.\n";
if (num<0) cout<<"The number is negative.\n";
getch();
}
Example 4.2
/*Test a given integer is even number or odd number*/
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int num;
printf("Enter the number: ");
scanf("%d",&num);
if (num%2==0) printf("The number is even number.\n");
if (num%2!=0) printf("The number is odd number.\n");
getch();
return 0;
}
Example 4.3
/*Test an exam mark is pass or fail*/
#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int mark;
cout<<"Enter an exam mark:";
cin>>mark;
if (mark>=40) cout<<"Pass"<<"\n";
if (mark<40) cout<<"Fail"<<"\n";
getch();
return 0;
}
4.2 The if -- else statements
if (condition) statement1;
else statement2;
(or)
if (condition)
{ <block of statement;>
 
}
else
{ <block of statement;>
 
}
Example 4.4
/*Test a given integer is positive or negative*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if (num>=0) cout<<"The number is positive.\n";
else cout<<"The number is negative.\n";
getch();
}
Example 4.5
/*Test a given integer is even number or odd number*/
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int num;
printf("Enter the number: ");
scanf("%d",&num);
if (num%2==0) printf("The number is even number.\n");
else printf("The number is odd number.\n");
getch();
return 0;
}
4.3 The if --- else --- if statement
if (condition1) statement1; (or) { block of statements }
else if (condition2) statement2; (or) { block of statements }
.
.
.
else if (conditionN) statementN; (or) {block of statements}
Example 4.6(a)
/*Test a given integer is zero or positive or negative*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter a number: ";
cin>>num;
if (num==0) cout<<"The number is zero.\n";
else if(num>0) cout<<"The number is positive.\n";
else cout<<"The number is negative.\n";
getch();
}
Example 4.6(b)
/*Test a given integer is zero or positive or negative*/
#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int num;
cout<<"Enter a number: ";
cin>>num;
clrscr( );
if (num==0)
{
cout <<"The number is zero.\n";
cou <<num;
}
else if(num>0)
{
cout<<"The number is positive.\n";
cout<<num;
}
else
{
cout<<"The number is negative.\n";
cout<<num;
}
getch();
}
4.4 Multiple Condition
1. if (condition1 && condition2) statement1;
else statement2;
Both condition1 and condition2 are true, then perform statement1, else
perform statement2.
 
2. if (condition1 || condition2) statement1;
else statement2;
Condition1 is true (or) condition2 is true, then perform statement1, else
both two conditions are not true perform statement2.
Example 4.7
/*Test the two subjcet marks are credit, pass or fail*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sub1, sub2;
cout<<"Enter the marks : ";
cin>>sub1>>sub2;
if (sub1>=65 && sub2>=65) cout<<"credit";
else if (sub1>=40 && sub2>=40) cout<<"pass")
else cout<<"fail";
getch();
}
Example 4.8
/*Check the given character is vowel-a,e,i,o,u */
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
char ch;
cout<<"Enter the character: ");
ch=getchar();
ch=toupper(ch);
if (ch=='A' || ch=="E" || ch=="I" || ch=='O'|| ch=='U')
cout<<"The given character is vowel"
else cout<<"not vowel";
getch();
}
4.5 The switch statement
switch (variable)
{
case variable condition1 : command sequence1; break;
case variable condition2 : command sequence2; break;
.
case variable conditionN : command sequenceN; break;
default : command sequence; break;
}
Example 4.9
/*define the number between 3 to 9*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter the number between 3 to 9: ";
cin>>num;
switch (num)
{
case 3 : cout<<"The number is three.\n"; break;
case 4 : cout<<"The number is four.\n"; break;
case 5 : cout<<"The number is five.\n"; break;
case 6 : cout<<"The number is six.\n"; break;
case 7 : cout<<"The number is seven.\n"; break;
case 8 : cout<<"The number is eight.\n"; break;
case 9 : cout<<"The number is nine.\n"; break;
default : cout<<"It is one of the unfined value.\n";break;
} //switch end
getch();
} //main end

You might also like