Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Handayan, Jahzeel Joyvie G. BSCS-11M1 CONDITIONAL STATEMENTS 1.

If Statement -specifies that a statement (or block of code) will be executed if and only Boolean statement is true. Example:
#include <iostream> using namespace std; int main () { int a = 100; if( a < 20 ) { cout << "a is less than 20;" << endl; }

#include <iostream> using namespace std; int main () { int a; cin>>a; if ( a < 10 ); cout<<a<< is less than ten, that's a big surprise";

#include <iostream> using namespace std; int main () { int grade; cin>>a; if ( grade>65 ); cout<<Congratulations! You passed!";

2. If-else Statement - Is used when we want to execute a certain statement if a condition is true and a different statement if the condition is false. Example:
#include <iostream> using namespace std; int main () { int a = 100; if( a < 20 ) { cout << "a is less than 20;" << endl; } else { cout << "a is not less than 20;" << endl; }

#include <iostream> using namespace std; int main() { int age; cout<<"Please input your age: "; cin>> age; cin.ignore(); if ( age < 100 ) { cout<<"You are pretty young!\n"; } else if ( age == 100 ) { cout<<"You are old\n"; } else { cout<<"You are really old\n"; }

3. If-else-if-else Statement use this statement to select one of many blocks of code to be executed Example:
#include <iostream> using namespace std; int main () { if (time<10); { cout<<"Good morning"; } else if (time<20); { cout<<"Good day"; } else { cout<<="Good evening"; }

#include <iostream> using namespace std; int main () { int a = 100; if( a == 10 ) { cout << "Value of a } else if( a == 20 ) { cout << "Value of a } else if( a == 30 ) { cout << "Value of a } else { cout << "Value of a } cout << "Exact value of return 0; }

is 10" << endl;

is 20" << endl;

is 30" << endl;

is not matching" << endl; a is : " << a << endl;

4. Nested If - you can use one if or else if statement inside another if or else if statement(s). Example:
#include <iostream> using namespace std; int main () { int a = 100; int b = 200; if( a == 100 ) { if( b == 200 ) { cout << "Value of a is 100 and b is 200" << endl; } } cout << "Exact value of a is : " << a << endl; cout << "Exact value of b is : " << b << endl; return 0; }

You might also like