Top Banner
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU 1
22

CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Dec 14, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

1

CSC1201: Programming Language 2

Lecture 1

Level 2 Course

Page 2: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

2

C++ OVERVIEW

Page 3: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

“Hello World” program#include <iostream>

using namespace std;

int main ()

{

cout << “Hello World\n”;

Return 0;

}

include information from the standard input/output library, iostream.

The instruction is more properly called a “preprocessor” instruction

The # hash character starts the line to denote a preprocessor instruction.

library name must be enclosed by < and > angled brackets.

3

Page 4: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

“Hello World” program using namespace std; The second line of the program makes all the functions within the

iostream library available for use by their standard names, which are in the namespace std. One of these is a function named cout that is used to write the output from a program.

int main () In the function declaration the data type is specified as int, meaning

integer. This means that after executing its statements this function must return an integer value to the operating system.

The braces { } contain the statements to be executed by the program.

The final statement in the main function Return a value of zero to the operating system to indicate that the program executed correctly.

4

Page 5: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

5

DATA TYPES

Page 6: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

DATA TYPES

6

Page 7: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

RESERVED WORDSKey Words

7

Page 8: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

ESCAPE SEQUENCES

8

Page 9: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Declaring and Initializing Variables

To declare variables for example◦ int first, second;

◦ char ch;

◦ double x;

◦ bool flage;

To declare and initialize variables for example◦ int first = 13, second = 10;

◦ char ch = ‘A’;

◦ double x = 12.6;

◦ bool flage = true;

9

Page 10: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Input (Read) Statement

The syntax of cin together with >> is:

cin >> variable >> variable >> ……….;

◦For example:

cin >> first >> second;

10

Page 11: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

OutputThe syntax of cout together with << is:

cout << expression or manipulator << expression or manipulator ……….;

The expression is evaluated and its value is printed at the current insertion point on the output device.

A manipulator is used to format the output. The simplest manipulator is endl.

11

Page 12: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Working with String The C++ <string> class provides methods to manipulate

strings of text. This is an example of Declaring and initializing a string

variable

#include <string>#include <iostream>using namespace std;

int main(){ string str = "C++ is fun";

…………}

12

Page 13: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Control Structures

IF statement:if (condition){ statement(s);}

IF Else statement:if (condition){ statement(s); ← True Branch}else{ statement(s); ← False Branch}

13

Page 14: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Example : If statement#include <iostream>

using namespace std;

int main()

{

int num=2;

bool flag=0;

if( (num==2) && (flag) )

{

cout << "The first test is true\n";

}

else if( (num==2) && (!flag) )

{

cout << "The second test is true\n";

}

return 0;

}

14

Page 15: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Switch Structures

15

Page 16: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Example: Switch Structures#include <iostream>

using namespace std;

int main()

{

}

16

char letter;

cout << "Enter any a-z character: "; cin >> letter;

switch(letter) { case 'a' :

cout << "Letter \'a\' found\n"; break;

case 'b' : cout << "Letter \'b\' found\n"; break;

case 'c' : cout << "Letter \'c\' found\n"; break;

default : cout << "Letter is not a, b or c\n"; } return 0;

Page 17: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Relational Operators in C++

17

Page 18: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

Logical Operators in C++

18

Page 19: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

While looping Structure

While (condition)

{

statement(s);

}

19

Page 20: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

20

Example: While loop#include <iostream>

using namespace std;

int main()

{

int i=0;

while( i<=20 )

{

cout << i << " ";

i = i + 5 ;

}

cout << endl;

return 0;

}

output: 0 5 10 15 20

Page 21: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

For looping Structure for (expression1; condition;

expression2) { statement(s) }

21

Page 22: CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU1.

Nouf Aljaffan (C) 2012 - CSC 1201 Course at KSU

22

Example: For Loop#include <iostream>using namespace std;

int main(){int i,num;

cout<<"enter any number: ";cin>>num;for ( i=1 ; i<=10 ; i++ ){cout << endl << num << "*“ << i << "=“ << num*i << endl;}

return 0;}

Output : enter any number : 11*1=11*2=21*3=3..