انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

lesson 6

Share |
الكلية كلية العلوم للبنات     القسم قسم الحاسبات     المرحلة 1
أستاذ المادة ماجد جبار جواد الفنهراوي       10/11/2018 06:51:26
1. Selection Statements:

Conditional expressions are mainly used for decision making. C++ provides multiple selection structures: if, if/else, else if, nested if and switch.


2. The Single If Statement Structure:
The IF statement is used to express conditional expression. If the given condition is true then it will execute the statements; otherwise it will execute the optional statements.

Example 1:
- Program to print positive number entered by the user If user enters negative
number, it is skipped

{
int number;
cout << "Enter an integer: ";
cin >> number;
if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
}

Output 1

Enter an integer: 5
You entered a positive number: 5 This statement is always executed.

Output 2

Enter a number: -5
This statement is always executed.


Example 2:
- Write a C++ program to read any two numbers and print the largest value of it:
{
Float x,y;
cout<<”Enter any two numbers\n”;
cin>>x>>y;
}

3. The Single Block If Statement Structure :

The block IF statement are enclosed in ({) and (}) to group declaration and statements into a compound statement or a block. These blocks are always considered as a single statement.


Example:
- Write a C++ program to read a number and check if it’s positive, if it’s so print it, add it to a total, and decrement it by 2:
void main( )
{
int num, total=0; cin
>> num;
if ( num >= 0 )
{cout << num <<” is a positive”; total += num;
num = num – 2;
}
}

4. The If/else Statement Structure:

In this case, either of the two statements are executed depending upon the value of the expression. Note that there is a semicolon after each of the statement but not after the IF expression.

Example 1:
cin >> value;
if ( value >= 0 )
cout << “positive”;

else
cout << “negative”;
Example 2:
cin >> num1 >> num2; if ( num1 > num2 )
cout << num1;

else
cout << num2;
Example 3:
- Write a C++ program to read a student degree, and check if it’s degree greater than or equal to 50, then print pass, otherwise print fail:

void main( )
{
int degree; cin >> degree;
if (degree >= 50 )
cout << ”pass”;

else

}

out << “fail”;

Example 4:
- Write a C++ program to read a number, and check if it’s even or odd:
void main( )
{
int num; cin >> num;
if ( num % 2 == 0 )
cout << ”even”;

else


}

cout << “odd”;



5. Else if Statements:
Example 1:



if ( value == 0 ) cout << “grade is A”;
else if ( value == 1 ) cout << “grade is B”;
else if ( value == 2 ) cout << “grade is C”;
else cout << “grade is X”;


المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
الرجوع الى لوحة التحكم