1 / 27

control statement review namespace function

ㅎㅎ. Function. control statement review namespace function. Math Operator Quiz. What are the values printed? const int five = 5; int i = 15 ; float x = 15.0 ; cout << “1 :” << five + i/2 << endl; cout << “2 :” << five + x/2 << endl;. do while.

rimona
Download Presentation

control statement review namespace function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ㅎㅎ Function control statement review namespace function

  2. Math Operator Quiz What are the values printed? const int five = 5; int i = 15; float x = 15.0; cout << “1 :” << five + i/2 << endl; cout << “2 :” << five + x/2 << endl;

  3. do while • The do while control structure also provides repetition, this time the condition is at the bottom of the loop. • the body is always executed at least once do somestuff; while ( condition );

  4. [practice1] do while [예제 1]

  5. 제어문 제어문은 프로그래머가 특정 문장을 건너 띄게도 하고 특정 문장을 반복 실행하게도 하면서 프로그램의 흐름을 조절하는 역할을 한다. Section 01제어문의 개념

  6. if 문 if 문은 주어진 조건을 만족하는 경우에만 특정 문장을 수행하도록 하는 제어문이다. Section 02선택문 문장 1; if(조건문) { 문장 2; } 문장 3;

  7. if else문 if~esle 문은 두 가지 경우 중 한 가지만 선택할 때 사용한다. 조건에 만족할 때 처리할 문장이 하나일 경우에는 중괄호를 생략할 수 있지만 여러 문장을 수행해야 할 경우에는 반드시 중괄호로 묶어주어야 한다. Section 02선택문 문장 1; if(조건문) {    문장 2; } else{    문장 3; } 문장4;

  8. 다중 if~else문 if~else 문을 여러 번 사용하는 다중 if~else 문은 두 가지 이상의 조건문을 둘 수 있으며 각각의 조건에 맞는 블록을 수행할 수 있다. Section 02선택문 if(조건문1){    문장 1; } else if(조건문2){    문장 2; } else if(조건문3){    문장 3; } else {    문장 n; }

  9. [practice2] if~else 문을 사용하여 숫자 맞추기-1 [예제 2-1]

  10. [practice2] if~else 문을 사용하여 숫자 맞추기-2 [예제 2-2]

  11. 다중선택 switch 문 여러 가지 중 하나를 선택할 때 사용하는 다중 선택문이다. 다중 if else 문은 조건문을 검사해 참이나 거짓의 결과로 문장을 수행하지만 switch 문은 참이나 거짓의 결과를 반환하는 논리식이 아닌 상수식에 따라 분기한다. Section 02선택문 switch(정수식) {    case 정수값1 : 문장 1;[break;]    case 정수값2 : 문장 2;[break;]    …    case 정수값n : 문장n;[break;]    [default:] 문장n+1; }

  12. C++ Namespaces • What is this strange ‘std::’ in std::cout ? • - Concept of Namespaces - Why does it exist? We want to use the same identifier in several different contexts • Occurs in XML as well

  13. Namespaces • Example for Defintion: namespace myNamespace { int a, b; } • Example for use of namespace:myNamespace::a myNamespace::b a and b occur in the namespace myNamespace

  14. “using” a namespace • Example:#include <iostream> using namespace std;int main () { cout << 5.0 << “\n”; } from here on we use the namespace std std:: is not necessary now

  15. C++ Functions • In other languages called subroutines or procedures. • C++ functions all have a type. • Sometimes we don’t need to have a function return anything – in this case the function can have type void.

  16. Sample function int add2ints(int a, int b) { return(a+b); } parameters Return type Function name Function body

  17. C++ Programming Sample Function int add2nums( int firstnum, int secondnum ) { int sum; sum = firstnum + secondnum; // just to make a point firstnum = 0; secondnum = 0; return(sum); }

  18. [practice3] Two functions, Recursion-1 [예제 3-1]

  19. [practice3] Two functions, Recursion-2 [예제 3-2]

  20. [practice4] Two functions [예제 4]

  21. C++ Programming double sqrt( double ) • When callingsqrt, we have to give it a double. • The sqrt function returns a double. • We have to give it a double. x = sqrt(y); x = sqrt(100);

  22. [practice5] Two functions, Recursion-1 [예제 5-1]

  23. [practice5] Two functions, Recursion-2 [예제 5-2]

  24. [practice6] Square Loot [예제 6]

  25. [practice7] Factorial [예제 7]

More Related