270 likes | 354 Views
ㅎㅎ. 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.
E N D
ㅎㅎ 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 • 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 );
[practice1] do while [예제 1]
제어문 제어문은 프로그래머가 특정 문장을 건너 띄게도 하고 특정 문장을 반복 실행하게도 하면서 프로그램의 흐름을 조절하는 역할을 한다. Section 01제어문의 개념
if 문 if 문은 주어진 조건을 만족하는 경우에만 특정 문장을 수행하도록 하는 제어문이다. Section 02선택문 문장 1; if(조건문) { 문장 2; } 문장 3;
if else문 if~esle 문은 두 가지 경우 중 한 가지만 선택할 때 사용한다. 조건에 만족할 때 처리할 문장이 하나일 경우에는 중괄호를 생략할 수 있지만 여러 문장을 수행해야 할 경우에는 반드시 중괄호로 묶어주어야 한다. Section 02선택문 문장 1; if(조건문) { 문장 2; } else{ 문장 3; } 문장4;
다중 if~else문 if~else 문을 여러 번 사용하는 다중 if~else 문은 두 가지 이상의 조건문을 둘 수 있으며 각각의 조건에 맞는 블록을 수행할 수 있다. Section 02선택문 if(조건문1){ 문장 1; } else if(조건문2){ 문장 2; } else if(조건문3){ 문장 3; } else { 문장 n; }
[practice2] if~else 문을 사용하여 숫자 맞추기-1 [예제 2-1]
[practice2] if~else 문을 사용하여 숫자 맞추기-2 [예제 2-2]
다중선택 switch 문 여러 가지 중 하나를 선택할 때 사용하는 다중 선택문이다. 다중 if else 문은 조건문을 검사해 참이나 거짓의 결과로 문장을 수행하지만 switch 문은 참이나 거짓의 결과를 반환하는 논리식이 아닌 상수식에 따라 분기한다. Section 02선택문 switch(정수식) { case 정수값1 : 문장 1;[break;] case 정수값2 : 문장 2;[break;] … case 정수값n : 문장n;[break;] [default:] 문장n+1; }
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
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
“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
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.
Sample function int add2ints(int a, int b) { return(a+b); } parameters Return type Function name Function body
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); }
[practice4] Two functions [예제 4]
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);
[practice6] Square Loot [예제 6]
[practice7] Factorial [예제 7]