Home > Backend Development > C++ > body text

CS- Week 1

WBOY
Release: 2024-07-17 10:07:20
Original
737 people have browsed it

Machines only understand binary. When we write a list of human-readable instructions for a computer, machines understand only what we now call machine code. This machine code consists of only 1's and 0's.
With a special program called Compiler
, we can turn the original code into machine code. We can judge good code according to 3 criteria:

correctness (
    does the code produce the desired result?
  • ), design (
  • is the code design or structure well structured?
  • ), style (
  • How nicely is the code written?
  • ).
  • Hello World!

If we want to print some text on the screen in the C programming language, we use the printf function:


The

printf function prints the text
#include <stdio.h>

int main(void)
{
    printf("salom, dunyo\n")
}
Copy after login
hello, world

. The special character in it tells the compiler that the next character is a special instruction. And the symbol n after it means "new line" (new line). The expression on the first line of code is a very special command that says we want to use the capabilities of a library called stdio.h. This library allows us to use the function printf.
Libraries
is a set of ready-made functions that we can use in our code. Variables

Let's write some code in C that asks the user what his name is and greets him:


The capabilities of the cs50.h library, specially developed for the CS50 course, are used throughout this course. One of them is the get_string function. The get_string function is used to retrieve the text entered by the user.
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    string answer = get_string("Ismingiz nima? ");
    printf("Assalomu alaykum, %s\n", answer);
}
Copy after login
answer is a place reserved to remember a special user-entered text, which we call a variable. answer is of type

string
. Also, there are many other data types such as int, bool, char, etc. %s is a placeholder called format code
that tells the printf function to prepare to accept some string variable. There are also format codes for other data types, for example: %i - for
int
(integers). Conditional operators

Let's ask the user to input x and y variables of type int and compare the input numbers against each other:


Here we are creating two variables of type int (
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int x = get_int("x ni kiriting: ");
    int y = get_int("y ni kiriting: ");

    if (x < y)
    {
        printf("x soni y sonidan kichik\n");
    }
}
Copy after login
integer

), variables x and y. Their values ​​are filled using the get_int function of the cs50.h library. Using the conditional operator, we compare the values ​​of x and y and display a message on the screen depending on the result.

Block diagram

is a way we can check how a computer program works. With this method we can check the efficiency of our code. Let's see the block diagram of our code above:

Conditional 1We can improve the program by coding as follows:


All possible cases are now considered. Let's see its block diagram:
#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int x = get_int("x ni kiriting: ");
    int y = get_int("y ni kiriting: ");

    if (x < y)
    {
        printf("x soni y sonidan kichik\n");
    }
    else if (x > y)
    {
        printf("x soni y sonidan katta\n");
    }
    else
    {
        printf("x soni y soniga teng\n");
    }
}
Copy after login


Conditional 2 Repetition operators

Let's output

"meow"

to the screen 3 times:

The code we wrote works correctly, but we can improve our program by avoiding repetitions:
#include <stdio.h>

int main(void)
{
    printf("meow\n");
    printf("meow\n");
    printf("meow\n");
}
Copy after login


In this, the variable i of type int is created and given the value 3. Then i < A while loop is created that lasts for a time of 3. Each time using the i++ expression, we increment i by one, and the loop stops when i = 3.
#include <stdio.h>

int main(void)
{
    int i = 0;
    while (i < 3)
    {
        printf("meow\n");
        i++;
    }
}
Copy after login
We can further improve the design of our program by using the for loop:



The

for loop takes three arguments.
#include <stdio.h>

int main(void)
{
    for (int i = 0; i < 3; i++)
    {
        printf("meow\n");
    }
}
Copy after login
The first argument: int i = 0 initializes our counter.

Second argument: i < 3 - checked condition.
Finally, the i++ argument means that every time our number i increases by one.
We can also create our own function:


void means that the function does not return any value. In parentheses (void) - means that the function does not accept any parameters.
void meow(void)
{
    printf("meow\n");
}
Copy after login
We will use this created meow function inside the main function:


#include 

void meow(void);

int main(void)
{
    for (int i = 0; i < 3; i++)
    {
        meow();
    }
}

void meow(void)
{
    printf("meow\n");
}
Copy after login

meow funksiyasini asosiy funksiya ichida chaqira olishimiz uchun funksiya prototipi kodning yuqori qismida void meow(void) sifatida berilgan.

Arifmetik operatorlar va akstraksiya

Keling, C tilida kalkulyator yasaymiz:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // x qiymati kiritilsin
    int x = get_int("x: ");

    // y qiymati kiritilsin
    int y = get_int("y: ");

    // Qo'shish amalini bajarish
    printf("%i\n", x + y);
}
Copy after login

get_int funktsiyasi yordamida foydalanuvchidan butun son bo'lgan x va y o'zgaruvchilariga qiymat berishi so'ralyabdi. Keyin printf funksiyasi butun son uchun format kodi - %i belgisi yordamida x + y qiymatini chop etadi.

Arifmetik operatorlar kompilyator tomonidan qo'llab-quvvatlanadigan matematik operatsiyalardir. C tilida arifmetik operatorlarga quyidagilar kiradi:

  • + - qo'shish uchun;
  • - - ayirish uchun;
  • * - ko'paytirish uchun;
  • / - bo'linish uchun;
  • % - bir sonni ikkinchi songa bo'lgandagi qoldiqni hisoblash uchun.

Abstraksiya - bu muammoni kichik-kichik bo'laklarga bo'lib hal qilish orqali kodimizni soddalashtirish san'ati.
Biz yuqoridagi kodimizni quyidagicha abstraktlashimiz mumkin:

#include <cs50.h>
#include <stdio.h>

int add(int a, int b);

int main(void)
{
    // x qiymati kiritilsin
    int x = get_int("x: ");

    // y qiymati kiritilsin
    int y = get_int("y: ");

    // Qo'shish amalini bajarish
    printf("%i\n", add(x, y));
}

int add(int a, int b)
{
    return a + b;
}
Copy after login

Bunda parametr sifatida a va b butun sonlarini qabul qilib oladigan va ularning yig'indisini qaytaradigan alohida add funksiyasi e'lon qilingan va asosiy funksiya ichida argument sifatida x va y butun sonlarini olib add(x, y) funksiyamiz chaqirilyabdi.

Kommentlar (izohlar)

Kommentlar - kompyuter dasturining asosiy qismlari bo'lib, yozgan kodimiz nima vazifa bajarayotganini ifodalovchi, boshqa dasturchilarga, shuningdek o'zimizga tushunarli hamda qisqa qilib qoldirgan izohlarimizdir. Kommentni yozish uchun shunchaki ikkita // beligisidan foydalanamiz:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    // Musbat butun son kiritilsin
    int n;
    do
    {
        n = get_int("Musbat butun son kiriting: ");
    }
    while (n < 1);
}
Copy after login

Ma'lumot turlari

Ma'lumotlar turlari o'zgaruvchida saqlanishi mumkin bo'lgan ma'lumotlar turini belgilaydi. Misol uchun, o'zgaruvchilar raqamlar, belgilar yoki mantiqiy qiymatlarni saqlashi mumkin. O'zgaruvchining turi kompyuterga ushbu ma'lumotlarni qanday boshqarishni aytadi.
C tilidagi umumiy maʼlumotlar turlari:

  • bool: rost (true) yoki yolg'on (false) kabi mantiqiy qiymatlarni saqlashi mumkin.
  • char: faqat bitta belgini saqlashi mumkin.
  • float: o'nlik qiymatlari bo'lgan haqiqiy son.
  • int: kasrsiz butun son.
  • long: int dan kattaroq butun sonni saqlashi mumkin, chunki u ko'proq bit ishlatadi.
  • string: belgilar ketma-ketligini saqlashi mumkin (masalan, so'z).

Har bir turning o'ziga xos chegaralari bor. Misol uchun, xotiradagi cheklovlar tufayli int ning eng yuqori qiymati 4294967295 bo'lishi mumkin. Agar biz int ni uning eng yuqori qiymatidan o'tkazib sanashga harakat qilsak, bu o'zgaruvchida noto'g'ri qiymat saqlanishiga (integer overflow) olib keladi.
Xotirani noto'g'ri ishlatish kodimizdagi xatolar yoki muammolarga olib kelishi mumkin. Muammolarni oldini olish uchun biz to'g'ri ma'lumot turidan foydalanayotganimizga ishonch hosil qilishimiz kerak.

Ushbu maqolada CS50x 2024 manbasidan foydalanilgan.

The above is the detailed content of CS- Week 1. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template