Lecture 7 Computer Programming -1-
Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
A- If …… statement - The if statement is used to check a condition and if the condition is true, we run a block of statements Syntax : 1\ If ( condition ) { statement } example: if (x>y) cout<<"X is greater than Y“ ;
- program to enter number, and print message if the number >=100 #include int main( ) { int x; cout<<"enter x:"; cin>>x; if ( x>=100) cout<<"large value"; return 0; }
2\ if (condition) { Statement 1; Statement 2; …..………… Statement n; } example: if (x>y) { cout<<"big number is:" <<x; cout<<"\n small number is:" <<y; }
- This program to increase the salary of the employee 10% if its salary less than or equal 300 or its rank greater than or equal 4 #include int main() { int rank; float salary; cout<<"Enter the salary and rank:"; cin>>salary>>rank; if ((salary =4)) { salary=salary*0.010; cout<<"salary after increasing \n"; cout<<salary<<endl; } return 0; }
B- if…….. else Statement used to check a condition and if the condition is true, we run a block of statements, else we process another block of statements. Syntax : if (condition) statement; else statement; example: if(x==100) cout<<"x is 100"; else cout <<"x is not 100";
This program determine if the entered number is even or odd.. #include int main() { int a; cout<<"Enter the number:"; cin>>a; if (a%2= =0) cout<<" the number is even"; else cout<<"the number is odd"; return 0; }
- Write a program read tow numbers and determine which number is the greatest.. #include int main() { int a,b; cout<<"Enter tow different number:"; cin>>a>>b; if (a>b) cout<<a<<" number is greater "<<b; else cout<<b<<" number is greater "<<a; return 0; }
(Nested if) complex if statement تعود else الى if(number>5) if (number > 0) { if (number > 5) cout<<"the number is greater than 5"; else out<<" the number is less than 5"; } if ( (number > 0) && if (number > 5) ) { cout<<"the number is greater than 5"; else cout<<" the number is less than 5"; }
- This program determine if the entered number is even or odd.. #include int main() { int number; cin>> number; if (number>0) if (number%2==0) cout<<"the number is positive even"; else cout<<" the number is positive Odd"; else cout<<"the number is less than zero"; } تعود else الأخيرة الى if(number>0)
if – else – if Syntax : if (condition1) statement1; else if(condition2) statement2; else if(condition3) statement3; …… else if(condition n) statement n; else statement;
Example #include int main() { int n; cout << " Enter the number: "; cin >> n; if (n== 1) cout << " Sunday : Programming(1) " <<endl ; else if (n== 2) cout << " Monday : English " << endl ; else if (n== 3) cout << " Tuesday : Introduction" << endl; else if (n== 4) cout << " Wednesda y : Math " << endl; else if (n== 5) cout << " Thursday : Islamic " << endl; else cout << " nothing " <<endl ; return 0; }
Write a C++ program by using if statement To simulate the calculator such that when you enter tow numbers and the mathematical operation(+,-,*,%,\) you get a result ? #include int main() { double a,b ; char x ; cout << "enter the operation : " ; cin >> a >>x >> b ; if (x=='+') cout << " a+b = " << a+b << endl ; else if (x=='-') cout<< "a-b = " << a-b << endl ; else if (x=='*') cout << "a*b = " << a*b << endl ; else if (x=='/') cout << "a/b = " << a/b << endl ; else cout << "Unknown operation" << endl ; return 0; }
Write a C++ program by using if statement to calculate the grades of students ? #include int main() { float score ; cout << "Enter your score : "; cin >> score ; if (score >= 90) cout << "Your Grade is A " << endl; else if (score >= 80) cout << "Your Grade is B " << endl ; else if (score >= 70) cout << "Your Grade is C " << endl ; else if (score >= 60) cout << "Your Grade is D " << endl ; else if (score >= 0) cout << "Your Grade is F " << endl ; else cout << "Error: Score can't be less than 0 " << endl ; return 0; }
Switch 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 case.
Switch statement SyntaxExample Switch (Expression or variable) { case constant 1 : statement 1; break; case constant 2 : statement 2; break; ……. case constant x : statement x; break; default : statement; } switch (x) { case 0 : cout << "Zero !" << endl; break; case 1 : cout << "One !" << endl; break; case 2 : cout << "Two !" << endl; break; default : cout << "Error !" << endl; }
- Write a C++ program by using switch statement To simulate the calculator such that when you enter tow numbers and the mathematical operation (+,-,*,\) you get a result ? #include int main() { float x, y ; char op ; cout<< " enter first number and math operator and second number : " ; cin >> x >> op >> y ; switch (op) { case '+' : cout<< "x+y = " << x+y << endl ; break ; case '-' : cout<< "x-y = " << x-y << endl ; break ; case '*' : cout<< "x*y = " << x*y << endl ; break ; case '/ ' : cout<< "x/y = " << x/y << endl ; break ; default : cout << " incorrect operation " << endl ; } return 0 ; }
- Write a C++ program by using switch statement to print the name of the color depending on the character input. #include int main() { char c ; cout<< " enter character : " ; cin >> c ; switch (c) { case 'b' : cout << "blue" << endl ; break ; case 'g' : cout << "green" << endl ; break ; case 'r' : cout << "red" << endl ; break ; case 'w' : cout << "white" << endl ; break ; default : cout << " unknown char " << endl ; } return 0 ; }
- Write a C++ program by using switch statement to print the days of the week. #include int main() { int weekday ; cout<< " enter number : " ; cin >> weekday ; switch (weekday) { case 1 : cout<< "Saturday" << endl ; break ; case 2 : cout<< "Sunday" << endl ; break ; case 3 : cout<< "Monday" << endl ; break ; case 4 : cout<< "Tuesday" << endl ; break ; case 5 : cout<< "Wednesday" << endl ; break ; case 6 : cout<< "Thursday" << endl ; break ; case 7 : cout<< "Friday" << endl ; break ; default : cout << " unknown char " << endl ; } return 0 ; }