WK04 - 14:440:127 C+ FUNCTIONS & ARRAYS REVIEW: CONTROL...

Info iconThis preview shows pages 1–16. Sign up to view the full content.

View Full Document Right Arrow Icon
14:440:127 C++ FUNCTIONS & ARRAYS
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
REVIEW: CONTROL STATEMENTS AND LOOPS …… if( condition) else( another condition) { statement 1; statement 2; ……. . } ……. If the condition is true Execute this
Background image of page 2
REVIEW: CONTROL STATEMENTS AND LOOPS …… if( condition) { statement 1; statement 2; ……. . } else( another condition) ……. If the condition is true Execute this or else execute this
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
REVIEW: CONTROL STATEMENTS AND LOOPS …… if( condition) { statement 1; statement 2; ……. . } else ……. If the condition is true Execute this or else execute this
Background image of page 4
REVIEW:SWITCH – STACK EM’ switch (variable) { case val1 : case val2 : case val3: case val4: statement -1 statement 2 break; ….. default: statement -1 statement 2 break; Execute these statements if variable == val1
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
REVIEW:SWITCH – STACK EM’ switch (variable) { case val1 : statement -1 statement 2 break; case val2 : case val3: case val4: ….. default: statement -1 statement 2 break; Execute this if variable = val2 or val3 or val4
Background image of page 6
REVIEW: For loop #include<iostream> using namespace std; int main() { int x ; for(x=0 , x<=3 , x++) { cout <<“The value of x is “<<x<<“ \ n”; } return 0; }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
REVIEW: For loop #include<iostream> using namespace std; int main() { int x ; for(x=0 ; x<=3 ; x++) { cout <<“The value of x is “<<x<<“ \ n”; } return 0; }
Background image of page 8
ARRAYS Accessing array elements variable_name[index]. Index starts from 0 and runs till size-1 Ex: int x[20]; x[0] = 10; x[2] = 200; . . . . . x[19] = 13; x[20] = 15; When storing values into arrays index should be specified
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ARRAYS Accessing array elements variable_name[index]. Index starts from 0 and runs till size-1 Ex: int x[20]; x=25; x[0] = 10; x[2] = 200; . . . . . x[19] = 13; x[20] = 15;
Background image of page 10
FUNCTIONS Programs written till now had only one function main() Every C++ program should have main() function, because that’s where the actual execution starts. You can have your own(user defined) functions. Once you’ve defined the functions they can be invoked anywhere in your program.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
FUNCTIONS #include<iostream> using namespace std; void function1(); int main() { cout <<“In main function… Calling ‘function1’ to get the input and display it to screen \ ”; function1(); cout <<“In main function \ ”; return 0; } void function1() { int x; cout <<“Enter a value:”; cin>>x; cout <<“You entered:”<<x<< endl; }
Background image of page 12
FUNCTIONS #include<iostream> using namespace std; int main() { cout <<“In main function… Calling ‘function1’ to get the input and display it to screen \ ”; function1(); cout <<“In main function \ ”; return 0; } void function1() { int x; cout <<“Enter a value:”; cin>>x; cout <<“You entered:”<<x<< endl; } Function Prototype.
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
FUNCTIONS #include<iostream> using namespace std; int main() { cout <<“In main function… Calling ‘function1’ to get the input and display it to screen \ ”; function1(); cout <<“In main function \ ”; return 0; } { int x; cout <<“Enter a value:”; cin>>x; cout <<“You entered:”<<x<< endl; } Function Prototype. Same as. But has a semicolon
Background image of page 14
FUNCTIONS #include<iostream> using namespace std; void function1(); int main() { function1(); cout <<“In main function \ ”; return 0; } void function1() { int x; cout <<“Enter a value:”; cin>>x; cout <<“You entered:”<<x<< endl; } If we trace the execution
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 16
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 50

WK04 - 14:440:127 C+ FUNCTIONS &amp; ARRAYS REVIEW: CONTROL...

This preview shows document pages 1 - 16. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online