4
THE if STATEMENT Its syntax is : if (condition) statement; +++ condition is an integral expression. +++ statement is any executable statement. +++ The statement will be executed if the value of integral expression is nonzero . Example : int main () { int n, d; cout << “Enter two positive integers: “; cin >> n >> d; if (n%d) cout << n << “ is not divisible by “ << d << endl; } Enter two positive integers: 66 7 66 is not divisible by 7 66 % 7 is 3. Since integral value is not zero , the expression is interpreted as a true condition. Enter two positive integers: 66 7 56 % 7 is 0, which interpreted to mean false, so divisibility message is not printed. In C++, whenever an integral expression is used as a condition, the value 0 means “false” and all other value mean “true.”

Ring Kasan

Embed Size (px)

DESCRIPTION

Ringkasan c++

Citation preview

Page 1: Ring Kasan

THE if STATEMENT

Its syntax is :

if (condition) statement;

+++ condition is an integral expression.+++ statement is any executable statement.+++ The statement will be executed if the value of integral expression is nonzero.

Example :

int main () {int n, d;cout << “Enter two positive integers: “;cin >> n >> d;if (n%d)

cout << n << “ is not divisible by “ << d << endl;}

Enter two positive integers: 66 7

66 is not divisible by 7

66 % 7 is 3. Since integral value is not zero, the expression is interpreted as a true condition.

Enter two positive integers: 66 7

56 % 7 is 0, which interpreted to mean false, so divisibility message is not printed.

In C++, whenever an integral expression is used as a condition, the value 0 means “false” and all other value mean “true.”

Page 2: Ring Kasan

THE if...else STATEMENT

Its syntax is:

if (condition) statement1;

else statement2;

+++ condition is an integral expression.+++ statement1 and statement2 are executable expression.+++ If the value of the condition is nonzero then statement1 will execute ;

otherwise statement2 will execute +++ Note that the if..else is only one statement, even though it requires two semicolons.

Example :

int main ()

{

int n, d;

cout << “Enter two positive integers: “ ;

cin >> n >> d;

if (n%d)

cout << n << “ is not divisible by “ << d << endl;

else

cout << n << “ is divisible by “ << d << endl;

}

Enter two positive integers: 56 7

56 is divisible by 7

56 % 7 is zero. The expression is a false condition, so the statement after the else is executed.

The if..else statement causes one of two alternative statements to execute depending upon whether the condition is true

Page 3: Ring Kasan

KEYWORDS

A keyword in a programming language is a word that is already defined and is reserved for a

unique purpose in programs written in that language. Standard C++ now has 74 keywords:

Two kinds of keywords.

A reserved word is a keyword that serves as a structure marker, used to define the syntax of the language. The keywords if and else are reserved words.

A standard identifier is a keyword that names a specific element of the language. The keywords bool and int are standard identifiers because they are names of standard types in C++.